diff --git a/ljqc_test.py b/ljqc_test.py new file mode 100644 index 0000000000000000000000000000000000000000..7e8b7be6e088f153ec7093a051fe234e4d549550 --- /dev/null +++ b/ljqc_test.py @@ -0,0 +1,123 @@ +#!/usr/bin/python +import json +import requests +import os + + +incremental_url = "http://123.58.109.35:9894/api/ljqc/check" +#f = open("few.patch","r") +f = open('more.patch','r') +content_patch=f.read() + +#print(content_patch) +gitUrl="https://gitee.com/xqtest/third_party_weston" +branch= "master" +prId= "7" +headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'} +data = { + "gitUrl": gitUrl, + "tag": "", + "branch": branch, + "prId": prId, + "fileContext": [ + { + "content": content_patch, + "type":"", + } + + ] +} +f.close() +in_data = json.dumps(data) +#print(in_data) +t_data = requests.post(incremental_url,data=in_data,headers=headers) +#print(t_data.text) +increment_data = json.loads(t_data.text) +code = increment_data['code'] +comment_incremental = F'Compliance testing sucess' +if code != 200: + comment_incremental = F'Compliance testing failed' +print(comment_incremental) + # return comment_incremental +giturl = increment_data['data']['gitUrl'] +tag = increment_data['data']['tag'] +branch = increment_data['data']['branch'] +prid = increment_data['data']['prId'] +num_LicenseExceptionFile = increment_data['data']['hasLicenseExceptionFileNum'] +num_CopyrightExceptionFile = increment_data['data']['hasCopyrightExceptionFileNum'] +num_CompatibleExceptionFile = increment_data['data']['hasCompatibleExceptionFileNum'] +# num_fileLicenseDistortRiskList = increment_data['data']['fileLicenseDistortRiskList'] +# num_CompatibleExceptionFile = increment_data['data']['fileCopyrightDistortRiskList'] + +comment_incremental = F'所传文件有许可证的文件个数: {num_LicenseExceptionFile},所传文件有版权的文件个数: {num_CopyrightExceptionFile},所传文件有兼容的文件个数: {num_CompatibleExceptionFile}' +#comment_incremental = F'所传文件有许可证的文件个数: {num_LicenseExceptionFile},所传文件有版权的文件个数: {num_CopyrightExceptionFile},所传文件有兼容的文件个数: {num_CompatibleExceptionFile},匹配到且许可证不一致的开源文件:{num_fileLicenseDistortRiskList},匹配到且版权不一致的开源文件:{num_CompatibleExceptionFile}' +print(comment_incremental) +file_compatiblerisk = increment_data['data']['file2fileCompatibleRiskList'] +warehouse_compatiblerisk = increment_data['data']['base2fileCompatibleRiskList'] +#con = typeof(file_compatiblerisk) +#print(con) +if not file_compatiblerisk: + print('文件与文件之间无兼容风险') +else: + print('文件存在兼容风险,请重新确认文件协议') +if not warehouse_compatiblerisk: + print('文件与仓库之间无兼容风险') +else: + print('文件与仓库之间有兼容风险') + print (warehouse_compatiblerisk) +# print(file_compatiblerisk) + + +#for i in (file_compatiblerisk): +#for i in in_data: + +#if len(file_compatiblerisk) or len(warehouse_compatiblerisk): + +# print(len(file_compatiblerisk)) +# print(len(warehouse_compatiblerisk)) +# for i in file_compatiblerisk: + +# i = 'GPL' +# print(i) + +# baseInfo = t_data("data"); +# System.out.println("base:"+baseInfo); + + +public class recursive { + public static void json(JSONArray value32) { + + JSONArray value3 = null; + // 对数组【】 遍历 + for (int i = 0; i < ((JSONArray) value32).size(); i++) { + // 获取数组的 数据 + JSONObject house = (JSONObject) ((JSONArray) value32).get(i); + + // 遍历出数组的键值对 + for (Map.Entry entry : house.entrySet()) { + System.out.println(entry.getKey() + " : " + entry.getValue()); + // 如果键==children + if(entry.getKey().equals("children")) { + // 将children 的值给value3 + value3 = (JSONArray) entry.getValue(); //[{"children":[],"name":"hh"}] + + } + + } + // 判断 children 的值 是不是【】 + if(("[]".equals(value3.toString()))) { + System.out.println("为空"); + // 删除 children键值对 此处删除 json原数据 也删除 + house.remove("children"); + + + }else { + // 如果不是空,说明children还有值,递归 自己调用自己,再次遍历下一层的children + json(value3); + } + } + + + + + } diff --git a/qc_test.py b/qc_test.py new file mode 100644 index 0000000000000000000000000000000000000000..4ec8398b8b46e0b359ca3c439d814e49d67f77f4 --- /dev/null +++ b/qc_test.py @@ -0,0 +1,143 @@ +# coding:utf-8 +import urllib +import urllib.request +import re +import requests +import json + + +def check_liense(): + url = "http://123.58.109.35:38181/file/risk/analysis" + response1 = urllib.request.urlopen(url) + print(response1.getcode()) + print(len(response1.read())) + +check_liense() + + +gitee_url = "https://gitee.com/xqtest/third_party_weston" +#data = {"key":"value"} +res = requests.post(gitee_url) +print(res.text) +print(res.status_code) + +''' + + + + + +''' +url = "http://gitee.com/xqtest/third_party_weston" +payload= {"username":'xqtest', "password":'wrtnode123'} +response = requests.post(url=url,data=payload) + +print(response.text) + + +''' + +''' +""" +# 增量扫描 + +​#/api/ljqc/check + +# 请求方式: + +​# POST + +#参数: + + + +""" +{ + "gitUrl": "", + "tag": "master", + "branch": "master", + "prId": "123123", + "fileContext": [ + { + "content": "", + "type":0, + } + + ] +} +''' +{ + "code": 200, + "msg": "操作成功", + "data": { + "prId": "123123", + "tag": "master", + "gitUrl": "https://gitee.com/xqtest/third_party_weston", + "branch": "master", + "time":1645689132135465, + "hasCopyrightExceptionFileNum": 1, + "hasLicenseExceptionFileNum": 2, + "hasCompatibleExceptionFileNum": 0, + "fileList": [ + { + "path": "", + "licenseList": [ + { + "name": "MIT", + "riskLevel": 1, + } + ...... + ], + "copyrightList": [ + { + "context": "Copyright © 2012 Intel Corporation", + "author": "Intel Corporation", + } + ...... + ], + "fileLicenseDistortRiskList": [], + "fileCopyrightDistortRiskList": [] + }, + ...... + ], + "base2fileCompatibleRiskList": [ + { + "licenseUp": "", + "licenseDown": "", + "riskDesc": "", + "containLicenseUpfilePathList": ["",…], + "containLicenseDownfilePathList": ["",…], + } + ...... + + ], + "file2fileCompatibleRiskList": [ + { + "licenseUp": "", + "licenseDown": "", + "riskDesc": "", + "containLicenseUpfilePathList": ["",…], + "containLicenseDownfilePathList": ["",…], + } + ...... + ] + } +} + +""" +''' + +""" + +url = "http://123.58.109.35:38181/file/risk/analysis" # 接口地址 + +# 消息头数据 +headers = { + + "giturl":'https://gitee.com/xqtest/third_party_weston', + "tag":"master", + "branch":"master", + "prld":"123123", +} + +""" \ No newline at end of file diff --git a/views-test.py b/views-test.py new file mode 100644 index 0000000000000000000000000000000000000000..3d2484c02e74c2277e3f489321fb13e4cd0deebd --- /dev/null +++ b/views-test.py @@ -0,0 +1,108 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.views import View +import json +import os +import urllib.parse +import urllib.request +import requests +from jobhook.models import JenkinsJob +from os import path +import shortuuid +from . import patch_util +import gitee as gt + +gitee = gt.Gitee() +gitee.set_access_token_v5('95db5d9af74495f40fba528906924b7a') +dco_comment_error_head = patch_util.dco_comment_head +compliance_comment_head = patch_util.compliance_comment_head + +def generate_jobid(prefix): + return F'job-{prefix}-{shortuuid.uuid()}' + +def jenkins_start_build(url, username, password, token, params = None): + if params == None: + params = {} + params['token'] = token + url_params = urllib.parse.urlencode(params) + trigger_url = F'{url}/buildWithParameters?{url_params}' + print(trigger_url) + response = requests.get(trigger_url, auth=(username, password)) + +class WebHook(View): + def pull_request(self, request, project_group): + j_body=json.loads(request.body.decode()) + action = j_body['action'] + if action != 'test': + if action == 'comment': + comment_date = j_body['comment']['body'] + if comment_date == 'check dco': + patch_url = j_body['pull_request']['patch_url'] + prid = j_body['pull_request']['number'] + project_name = j_body['repository']['path_with_namespace'] + patch_data = requests.get(patch_url).text + dco_check, dco_message = patch_util.check_signoff_from_patch(patch_data) + if dco_check: + dco_labels = ['dco检查成功'] + comments = '**dco通过,开始编译**\n' + dco_message + else: + dco_labels = ['dco检查失败'] + comments = dco_comment_error_head+'\n' + dco_message + # comments = '**dco检查失败**\n' + dco_message + + gitee.post_pr_comments(project_name, prid, comments) + gitee.post_pr_labels(project_name, prid, dco_labels) + return HttpResponse(F'nothing to do for action: {action}') + base_user = j_body['pull_request']['base']['user']['username'] + base_project = j_body['pull_request']['base']['repo']['path'] + base_ref = j_body['pull_request']['base']['ref'] + patch_url = j_body['pull_request']['patch_url'] + git_url = j_body['pull_request']['base']['repo']['html_url'] + prid = j_body['pull_request']['number'] + project_name = F'{base_user}/{base_project}' + + + # dco检测 + patch_data = requests.get(patch_url).text + dco_check, dco_message = patch_util.check_signoff_from_patch(patch_data) + if dco_check: + dco_labels = ['dco通过'] + comments = '**dco通过,开始编译**\n' + dco_message + # jenkins_start_build(job['url'], job['username'], job['password'], job['apitoken'], build_params) + else: + dco_labels = ['dco未通过'] + comments = dco_comment_error_head+'\n' + dco_message + gitee.post_pr_comments(project_name, prid, comments) + gitee.post_pr_labels(project_name, prid, dco_labels) + + # 合规检测 + reg_comment = patch_util.incremental_compliance(git_url, base_ref, prid, patch_data) + compliance_comment = compliance_comment_head +'\n'+reg_comment + gitee.post_pr_comments(project_name, prid, compliance_comment) + + # 触发jenkins流水线 + with open(path.join('/home/openharmany/giteebot-7-22/project_group', F'{project_group}.json'), 'r') as reader: + project_group_info = json.loads(reader.read()) + print(project_group_info) + project_info = project_group_info['projects'][project_name] + print(project_info) + if base_ref != project_info['branch']: + return HttpResponse(F'I do not care about the branch: {base_ref}') + project_path = project_info['path'] + patch_info = [{'path': project_path, 'patch': patch_url}] + print(patch_info) + job = project_group_info['job'] + jobid = generate_jobid(job['name']) + build_params = {'patch_info': json.dumps(patch_info), 'jobid': jobid} + context = { + 'project_group': project_group, + 'project': project_name, + 'pull_request': prid, + } + jenkins_job = JenkinsJob(jobid=jobid, context=json.dumps(context)) + jenkins_job.save() + jenkins_start_build(job['url'], job['username'], job['password'], job['apitoken'], build_params) + return HttpResponse(F'start {jobid}') + + def post(self, request, project_group): + return self.pull_request(request, project_group)