这个需求源于我最近在推代码扫描,需要开发去使用,而且扫出来的问题,要推着他们确认及修复,但可想而知,在这个部门,什么事都被政治化了,很难推动别人做事。
那一天,有个开发经理向我提出了一个问题:当gitlab有新增应用的时候,你这边怎么能及时加上扫描?
我说:这个问题问的好,我看一下gitlab和sonarqube有没有webhooks之类的东西可以用一用
我找了一圈,还兼带问了gitlab管理员,没有。
最后我回复他:暂时没有这类关联的开关可供使用,目前是口头通知要增加的应用,然后QA手工增加流水线进行扫描。
然后他来了一句:那可真是辛苦你们了~~~
需求分析
- gitlab-api 统计近7天的项目数据,我不知道有没有,但我想一般平台api都应该必须有这个接口
- jenkins-api 自动增加pipline-job
代码
- gitlab-api
说明:gitlab-api要有token,这个在管理帐户-设置-应用 增加一项选择api和scope既可,出来的token是一次性显示的,一定要记好,我这里不写详细了,自己去八八api说明文档。
public class GitlabApi {
/**
* 获取单个项目的各个结果 ,得到JSONArray
* @return
* @throws IOException
*/
public JSONArray getGitProjects(){
SendRuquest sendRuquest=new SendRuquest();
String url="https://git.com.cn/api/v4/projects?private_token=x4S91qR6-4F7VyBa7tdf&per_page=50";
String resultinfo= HttpsUtil.httpsGet(url);
System.out.println(resultinfo);
JSONArray resultlist =null;
try {
resultlist = JSONArray.parseArray(resultinfo);
}catch (Exception e){
e.printStackTrace();
}
return resultlist;
}
}
- 获取信息处理后拼接内容,发邮件用
/**
* 接html格式
* @return
* @throws IOException
*/
public static String getEmailContent() {
StringBuffer content=new StringBuffer();
String title="<table border=\"1\" style=\"border-collapse: collapse;\" style=\"margin: auto;\" " +
" <tr >" +
" <th bgcolor=\"#a9a9a9\">name</th>" +
" <th bgcolor=\"#a9a9a9\">namespace</th>" +
" <th bgcolor=\"#a9a9a9\">created_at</th>" +
" <th bgcolor=\"#a9a9a9\">git_url</th>" +
" <th bgcolor=\"#a9a9a9\">description</th>" +
" </tr>";
content.append(title);
GitlabApi gitlabApi=new GitlabApi();
JSONArray jsonlist=gitlabApi.getGitProjects();
try {
for (Iterator<Object> iterator = jsonlist.iterator(); iterator.hasNext(); ) {
JSONObject next = (JSONObject) iterator.next();
String created_at=next.getString("created_at").split("T")[0];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
IsLatestWeek isLatestWeek=new IsLatestWeek();
boolean flag = isLatestWeek.isLatestWeek(sdf.parse(created_at),new Date());//判断7天内的项目
if (flag){
String name = next.getString("name");
String namespace=next.getString("name_with_namespace");
String git_url=next.getString("http_url_to_repo");
String description=next.getString("description");
content.append("<tr>"+"<td>"+name+"</td>"+"<td>"+namespace+"</td>"+"<td>"+created_at+"</td>"+"<td>"+
"<a href=\""+git_url+"\">git地址</a>"
+"<td>"+description+"</td>"+"</tr>");
}
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println(content.toString());
return content.toString();
}
- 自动创建pipline扫描
[root@dev gitlab]# ll
total 20
-rw-r--r--. 1 root root 847 Nov 5 12:26 create_jenkins_sonarScan_pipline.sh
-rw-r--r--. 1 root root 4471 Nov 5 09:32 multi-module.xml
-rw-r--r--. 1 root root 4239 Nov 5 12:32 simple-module.xml
-----------------
#!/bin/bash
echo '#####开始创建jenkins-job####'
#config=/data/PRG/gitlab/simple-module.xml
cd /data/PRG/gitlab
echo "请输入应用名:"
read appName
echo "请输入git地址"
read appGit
echo "请选择应用类型:"
echo "1.单模块"
echo "2.多模块"
read appModule
case $appModule in
1)
`sed -i 's#<url>.*</url>#<url>$appGit</url>#g' simple-module.xml`
`curl -s -XPOST http://ci.pc.com.cn/jenkins/createItem?name=$appName -u liujuan:11776bf4371ae691caa86d7a8db73e49ab --data-binary @simple-module.xml -H "Content-Type:text/xml"`
;;
2)
`sed -i 's#<url>.*</url>#<url>$appGit</url>#g' file`
`curl -s -XPOST http://ci.pc.com.cn/jenkins/createItem?name=$appName -u liujuan:11776bf4371ae691caa86d7a8db73e49ab --data-binary @multi-module.xml -H "Content-Type:text/xml"`
;;
*)
echo "您选择的操作不存在,请重新执行安装程序,$0"
exit
esac
以上完成!
但很明显,自动化很被动啊,因为我要做报告统计,所以定的扫描规范里有:
sonar.projectKey、sonar.projectName、模块名称、报告路径等每个项目不是一样的,这样导致自动化复用率低。
所以,我做这个只是为了证明,你想要什么东西,我一定会凑出来给你,但是实不实用,得真正执行的人才知道什么方法最合适,为什么一听手工就觉得很low,这个社会了工具那么智能化了,学习成本那么低,谁还不会自动化了。这些话是献给某些不懂技术的开发领导听的。哈哈哈哈哈哈!
`