From 9b14168438c268df72b4e9359cf6edea82af7b1c Mon Sep 17 00:00:00 2001 From: user <8352767+zhegu123@user.noreply.gitee.com> Date: Mon, 19 Jun 2023 09:14:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=89=AB=E6=8F=8F=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../huawei/jenkins/codeCheck/CustomInput.java | 50 ++++++ .../codeCheck/codeCheckPipelineStep.java | 60 +++++++ .../jenkins/codeCheck/codeCheckService.java | 167 ++++++++++++++++-- 3 files changed, 267 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/huawei/jenkins/codeCheck/CustomInput.java b/src/main/java/com/huawei/jenkins/codeCheck/CustomInput.java index a9a4ea6..5cfaf0c 100644 --- a/src/main/java/com/huawei/jenkins/codeCheck/CustomInput.java +++ b/src/main/java/com/huawei/jenkins/codeCheck/CustomInput.java @@ -18,6 +18,16 @@ public class CustomInput implements Serializable { private String project_id; + private String language; + + private String gitBranch; + + private String gitUrl; + + private String passWord; + + private String userName; + private String topicUrn; public String getAk() { @@ -60,6 +70,46 @@ public class CustomInput implements Serializable { this.project_id = project_id; } + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getGitBranch() { + return gitBranch; + } + + public void setGitBranch(String gitBranch) { + this.gitBranch = gitBranch; + } + + public String getGitUrl() { + return gitUrl; + } + + public void setGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + } + + public String getPassWord() { + return passWord; + } + + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + public String getTopicUrn() { return topicUrn; } diff --git a/src/main/java/com/huawei/jenkins/codeCheck/codeCheckPipelineStep.java b/src/main/java/com/huawei/jenkins/codeCheck/codeCheckPipelineStep.java index 749ad29..fd71232 100644 --- a/src/main/java/com/huawei/jenkins/codeCheck/codeCheckPipelineStep.java +++ b/src/main/java/com/huawei/jenkins/codeCheck/codeCheckPipelineStep.java @@ -43,6 +43,16 @@ public class codeCheckPipelineStep extends Step { private String project_id; + private String language; + + private String gitBranch; + + private String gitUrl; + + private String passWord; + + private String userName; + private String topicUrn; public String getAk() { @@ -90,6 +100,51 @@ public class codeCheckPipelineStep extends Step { this.project_id = project_id; } + public String getLanguage() { + return language; + } + + @DataBoundSetter + public void setLanguage(String language) { + this.language = language; + } + + public String getGitBranch() { + return gitBranch; + } + + @DataBoundSetter + public void setGitBranch(String gitBranch) { + this.gitBranch = gitBranch; + } + + public String getGitUrl() { + return gitUrl; + } + + @DataBoundSetter + public void setGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + } + + public String getPassWord() { + return passWord; + } + + @DataBoundSetter + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public String getUserName() { + return userName; + } + + @DataBoundSetter + public void setUserName(String userName) { + this.userName = userName; + } + public String getTopicUrn() { return topicUrn; } @@ -152,6 +207,11 @@ public class codeCheckPipelineStep extends Step { customInput.setRegion(step.getRegion()); customInput.setProject_id(step.getProject_id()); customInput.setTask_id(step.getTask_id()); + customInput.setGitBranch(step.getGitBranch()); + customInput.setLanguage(step.getLanguage()); + customInput.setGitUrl(step.getGitUrl()); + customInput.setPassWord(step.getPassWord()); + customInput.setUserName(step.getUserName()); customInput.setTopicUrn(step.getTopicUrn()); TaskListener listener = codeCheckPipelineStep.Execution.this.getContext().get(TaskListener.class); return codeCheckService.runTask(listener, customInput); diff --git a/src/main/java/com/huawei/jenkins/codeCheck/codeCheckService.java b/src/main/java/com/huawei/jenkins/codeCheck/codeCheckService.java index 604bba9..8dd93c4 100644 --- a/src/main/java/com/huawei/jenkins/codeCheck/codeCheckService.java +++ b/src/main/java/com/huawei/jenkins/codeCheck/codeCheckService.java @@ -17,15 +17,78 @@ import com.huaweicloud.sdk.smn.v2.region.SmnRegion; import hudson.model.TaskListener; import org.apache.commons.lang.StringUtils; +import java.io.IOException; import java.io.Serializable; +import java.util.Arrays; +import java.util.List; public class codeCheckService implements Serializable { private static final long serialVersionUID = 1; + /** + * 新建检查任务 + * + * @param listener 日志 + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ + public static String CreateTask(TaskListener listener, CustomInput customInput) { + + ICredential auth = new BasicCredentials() + .withAk(customInput.getAk()) + .withSk(customInput.getSk()); + + CodeCheckClient client = CodeCheckClient.newBuilder() + .withCredential(auth) + .withRegion(CodeCheckRegion.valueOf(customInput.getRegion())) + .build(); + CreateTaskRequest request = new CreateTaskRequest(); + request.withProjectId(customInput.getProject_id()); + CreateTaskRequestV2 body = new CreateTaskRequestV2(); + List language= Arrays.asList(customInput.getLanguage().split(",")); + body.withLanguage(language); + body.withGitBranch(customInput.getGitBranch()); + body.withGitUrl(customInput.getGitUrl()); + body.withAccessToken(customInput.getPassWord()); + body.withUsername(customInput.getUserName()); + request.withBody(body); + String taskId = ""; + try { + CreateTaskResponse response = client.createTask(request); + taskId = response.getTaskId(); + listener.getLogger().println("taskId=" + taskId); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (RequestTimeoutException e) { + e.printStackTrace(); + } catch (ServiceResponseException e) { + e.printStackTrace(); + listener.getLogger().println("错误信息:" + e.getErrorMsg()); + } + return taskId; + } + + + + /** + * 执行检查任务 + * + * @param listener 日志 + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ public static String runTask(TaskListener listener, CustomInput customInput) { inputValidate(customInput); - ICredential auth = new BasicCredentials().withAk(customInput.getAk()).withSk(customInput.getSk()); + //没有taskId新建检查任务 + if(StringUtils.isBlank(customInput.getTask_id())){ + String taskId = CreateTask(listener,customInput); + customInput.setTask_id(taskId); + } + + ICredential auth = new BasicCredentials().withAk(customInput.getAk()).withSk(customInput.getSk()); CodeCheckClient client = CodeCheckClient.newBuilder().withCredential(auth) .withRegion(CodeCheckRegion.valueOf(customInput.getRegion())).build(); @@ -67,6 +130,16 @@ public class codeCheckService implements Serializable { return message; } + + + /** + * 查看任务状态 + * + * @param listener 日志 + * @param taskId 任务Id + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ public static Integer showTasks(CodeCheckClient client, String taskId, TaskListener listener) { ShowProgressDetailRequest request = new ShowProgressDetailRequest(); request.withTaskId(taskId); @@ -87,6 +160,16 @@ public class codeCheckService implements Serializable { return response.getTaskStatus(); } + + + /** + * 查看任务结果 + * + * @param listener 日志 + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ public static String CheckRecord(CodeCheckClient client, CustomInput customInput, TaskListener listener) { String message = ""; ShowTaskDetailRequest request = new ShowTaskDetailRequest(); @@ -99,17 +182,23 @@ public class codeCheckService implements Serializable { listener.getLogger().println("提示问题数:" + info.getSuggestionCount()); if (info.getIsAccess().equals("0")) { message = "门禁质量不通过"; + String url = "https://console.huaweicloud.com/devcloud/?region=" + customInput.getRegion() + "#/codecheck/list"; + listener.getLogger().println("关于具体详情,可前往:" + url + "\n查看"); + // 发送钉钉微信通知 + if (StringUtils.isNotBlank(customInput.getTopicUrn())) { + // 发送钉钉微信通知 + publishMessage(customInput, info, listener); + } } else { message = "门禁质量通过"; + //删除检查任务,如果任务在执行中,则不删除 + Integer status = showTasks(client, customInput.getTask_id(), listener); + if(status!=0){ + deleteTask(customInput,listener); + } } listener.getLogger().println(message); - String url = "https://console.huaweicloud.com/devcloud/?region=" + customInput.getRegion() + "#/codecheck/list"; - listener.getLogger().println("关于具体详情,可前往:" + url + "\n查看"); - // 发送钉钉微信通知 - if (StringUtils.isNotBlank(customInput.getTopicUrn())) { - // 发送钉钉微信通知 - publishMessage(customInput, info, listener); - } + } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { @@ -121,6 +210,49 @@ public class codeCheckService implements Serializable { return message; } + + + /** + * 删除任务 + * + * @param listener 日志 + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ + public static void deleteTask(CustomInput customInput, TaskListener listener) { + + ICredential auth = new BasicCredentials() + .withAk(customInput.getAk()) + .withSk(customInput.getSk()); + + CodeCheckClient client = CodeCheckClient.newBuilder() + .withCredential(auth) + .withRegion(CodeCheckRegion.valueOf(customInput.getRegion())) + .build(); + DeleteTaskRequest request = new DeleteTaskRequest(); + request.withTaskId(customInput.getTask_id()); + try { + client.deleteTask(request); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (RequestTimeoutException e) { + e.printStackTrace(); + } catch (ServiceResponseException e) { + e.printStackTrace(); + listener.getLogger().println("错误信息:" + e.getErrorMsg()); + } + } + + + /** + * SMN发送消息通知 + * + * @param listener 日志 + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ public static void publishMessage(CustomInput customInput, ShowTaskDetailResponse info, TaskListener listener) { ICredential auth = new BasicCredentials().withAk(customInput.getAk()).withSk(customInput.getSk()); @@ -155,6 +287,15 @@ public class codeCheckService implements Serializable { } } + + + /** + * 参数校验 + * + * @param customInput 用户输入 + * @throws IOException 异常 + * @throws InterruptedException 异常 + */ private static void inputValidate(CustomInput customInput) { final String ak = customInput.getAk(); final String sk = customInput.getSk(); @@ -162,10 +303,16 @@ public class codeCheckService implements Serializable { final String projectId = customInput.getProject_id(); final String taskId = customInput.getTask_id(); Preconditions.checkArgument(StringUtils.isNotBlank(projectId), "projectId id can not be blank"); - Preconditions.checkArgument(StringUtils.isNotBlank(taskId), "task id can not be blank"); Preconditions.checkArgument(StringUtils.isNotBlank(region), "region can not be blank"); Preconditions.checkArgument(StringUtils.isNotBlank(ak) && StringUtils.isNotBlank(sk), "Ak, Sk can not be blank"); + if(StringUtils.isBlank(taskId)){ + final String language = customInput.getLanguage(); + final String gitBranch = customInput.getGitBranch(); + final String gitUrl = customInput.getGitUrl(); + Preconditions.checkArgument(StringUtils.isNotBlank(language), "language can not be blank"); + Preconditions.checkArgument(StringUtils.isNotBlank(gitBranch), "git branch can not be blank"); + Preconditions.checkArgument(StringUtils.isNotBlank(gitUrl), "git Url can not be blank"); + } } - } -- Gitee