diff --git a/pom.xml b/pom.xml index 2baae4b53e69bc25a1fc6d3cf7cf92842874772a..cc3a31d6cf9bebdb77e1e60b40bc5ae3b87b692a 100644 --- a/pom.xml +++ b/pom.xml @@ -189,6 +189,35 @@ + + + com.huaweicloud.sdk + huaweicloud-sdk-cloudpipeline + 3.1.38 + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-databind + + + com.huaweicloud.sdk + huaweicloud-sdk-core + + + org.openeuler + bgmprovider + + + org.slf4j + slf4j-api + + + + diff --git a/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineService.java b/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineService.java new file mode 100644 index 0000000000000000000000000000000000000000..2d9af92d673170144ce3ef0c7be94e59518f44df --- /dev/null +++ b/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineService.java @@ -0,0 +1,71 @@ +package com.huawei.jenkins.cloudpipeline; + +import com.google.common.base.Preconditions; +import com.huaweicloud.sdk.cloudpipeline.v2.CloudPipelineClient; +import com.huaweicloud.sdk.cloudpipeline.v2.model.RunPipelineDTO; +import com.huaweicloud.sdk.cloudpipeline.v2.model.RunPipelineRequest; +import com.huaweicloud.sdk.cloudpipeline.v2.model.RunPipelineResponse; +import com.huaweicloud.sdk.cloudpipeline.v2.region.CloudPipelineRegion; +import com.huaweicloud.sdk.core.auth.BasicCredentials; +import com.huaweicloud.sdk.core.auth.ICredential; +import com.huaweicloud.sdk.core.exception.ConnectionException; +import com.huaweicloud.sdk.core.exception.RequestTimeoutException; +import com.huaweicloud.sdk.core.exception.ServiceResponseException; +import hudson.model.TaskListener; +import org.apache.commons.lang.StringUtils; + +import java.io.Serializable; + + +public class CodeArtsPipelineService implements Serializable { + private static final long serialVersionUID = 1; + + public static String runPipeline(TaskListener listener, CustomInput customInput) { + inputValidate(customInput); + ICredential auth = new BasicCredentials() + .withAk(customInput.getAk()) + .withSk(customInput.getSk()); + + CloudPipelineClient client = CloudPipelineClient.newBuilder() + .withCredential(auth) + .withRegion(CloudPipelineRegion.valueOf(customInput.getRegion())) + .build(); + RunPipelineRequest request = new RunPipelineRequest(); + request.withProjectId(customInput.getProject_id()); + request.withPipelineId(customInput.getPipeline_id()); + RunPipelineDTO body = new RunPipelineDTO(); + request.withBody(body); + String message = ""; + try { + RunPipelineResponse response = client.runPipeline(request); + message = response.getPipelineRunId(); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (RequestTimeoutException e) { + e.printStackTrace(); + } catch (ServiceResponseException e) { + e.printStackTrace(); + listener.getLogger().println("ErrorMsg:" + e.getErrorMsg()); + } + return message; + } + + private static void inputValidate(CustomInput customInput) { + final String ak = customInput.getAk(); + final String sk = customInput.getSk(); + final String region = customInput.getRegion(); + final String pipelineId= customInput.getPipeline_id(); + final String projectId = customInput.getProject_id(); + Preconditions.checkArgument(StringUtils.isNotBlank(pipelineId), "pipeline Id can not be blank"); + Preconditions.checkArgument(StringUtils.isNotBlank(projectId), "project 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"); + } + + + + + + + +} diff --git a/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep.java b/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep.java new file mode 100644 index 0000000000000000000000000000000000000000..a4cec48fc332c9a0ddd4f0e6c33fd0381bf2df31 --- /dev/null +++ b/src/main/java/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep.java @@ -0,0 +1,148 @@ +/* + * Copyright 2022. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huawei.jenkins.cloudpipeline; + +import hudson.EnvVars; +import hudson.Extension; +import hudson.FilePath; +import hudson.model.TaskListener; +import org.jenkinsci.plugins.workflow.steps.*; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * pipeline语法支持obs文件上传 + */ +public class CodeArtsPipelineStep extends Step { + + private String ak; + + private String sk; + + private String region; + + private String project_id; + + private String pipeline_id; + + public String getAk() { + return ak; + } + + @DataBoundSetter + public void setAk(String ak) { + this.ak = ak; + } + + public String getSk() { + return sk; + } + + @DataBoundSetter + public void setSk(String sk) { + this.sk = sk; + } + + public String getRegion() { + return region; + } + + @DataBoundSetter + public void setRegion(String region) { + this.region = region; + } + + public String getProject_id() { + return project_id; + } + + @DataBoundSetter + public void setProject_id(String project_id) { + this.project_id = project_id; + } + + public String getPipeline_id() { + return pipeline_id; + } + + @DataBoundSetter + public void setPipeline_id(String pipeline_id) { + this.pipeline_id = pipeline_id; + } + + @DataBoundConstructor + public CodeArtsPipelineStep(String region) { + this.region = region; + } + + @Override + public StepExecution start(StepContext context) throws Exception { + return new Execution(this, context); + } + + @Extension + public static class DescriptorImpl extends StepDescriptor { + + @Override + public Set> getRequiredContext() { + return requires(TaskListener.class, EnvVars.class, FilePath.class); + } + + @Override + public String getFunctionName() { + return "CodeArtsPipeline"; + } + + @Override + public String getDisplayName() { + return "华为云CodeArts流水线"; + } + } + + public static > Set requires(T... classes) { + return new HashSet<>(Arrays.asList(classes)); + } + + public static class Execution extends SynchronousNonBlockingStepExecution { + + protected static final long serialVersionUID = 1L; + + protected final transient CodeArtsPipelineStep step; + + public Execution(CodeArtsPipelineStep step, StepContext context) { + super(context); + this.step = step; + + } + + @Override + public String run() throws Exception { + CustomInput customInput = new CustomInput(); + customInput.setAk(step.getAk()); + customInput.setSk(step.getSk()); + customInput.setRegion(step.getRegion()); + customInput.setProject_id(step.getProject_id()); + customInput.setPipeline_id(step.getPipeline_id()); + TaskListener listener = Execution.this.getContext().get(TaskListener.class); + return CodeArtsPipelineService.runPipeline(listener, customInput); + } + } +} diff --git a/src/main/java/com/huawei/jenkins/cloudpipeline/CustomInput.java b/src/main/java/com/huawei/jenkins/cloudpipeline/CustomInput.java new file mode 100644 index 0000000000000000000000000000000000000000..7907b6549486eff2e0d957019c5828b0a946f2b5 --- /dev/null +++ b/src/main/java/com/huawei/jenkins/cloudpipeline/CustomInput.java @@ -0,0 +1,60 @@ +package com.huawei.jenkins.cloudpipeline; + +import java.io.Serializable; + +/** + * 用户自定义输出参数 + */ +public class CustomInput implements Serializable { + private static final long serialVersionUID = 1L; + + private String ak; + + private String sk; + + private String region; + + private String project_id; + + private String pipeline_id; + + public String getAk() { + return ak; + } + + public void setAk(String ak) { + this.ak = ak; + } + + public String getSk() { + return sk; + } + + public void setSk(String sk) { + this.sk = sk; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + public String getProject_id() { + return project_id; + } + + public void setProject_id(String project_id) { + this.project_id = project_id; + } + + public String getPipeline_id() { + return pipeline_id; + } + + public void setPipeline_id(String pipeline_id) { + this.pipeline_id = pipeline_id; + } +} diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/config.jelly b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/config.jelly new file mode 100644 index 0000000000000000000000000000000000000000..fceab9bd28f1c68d4f823a289d837b6410221406 --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/config.jelly @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-ak.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-ak.html new file mode 100644 index 0000000000000000000000000000000000000000..7d9c6d8413cb53da24850b648c762d2a1e27b895 --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-ak.html @@ -0,0 +1,19 @@ + +
+ HuaweiCloud Access Key, used only when the credential id is null
+ 去配置 +
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-pipeline_id.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-pipeline_id.html new file mode 100644 index 0000000000000000000000000000000000000000..b67b3e9f4d0c357a744b7f8ace4b77da5f99dafa --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-pipeline_id.html @@ -0,0 +1,18 @@ + +
+ pipeline id +
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-project_id.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-project_id.html new file mode 100644 index 0000000000000000000000000000000000000000..fbb0c4d1f6b115d22887566350d822e0183450cf --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-project_id.html @@ -0,0 +1,18 @@ + +
+ project id +
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-region.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-region.html new file mode 100644 index 0000000000000000000000000000000000000000..3ccb0ee1cc8e5c5011f7848477d9879d62772f6c --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-region.html @@ -0,0 +1,18 @@ + +
+ The currently callable territory +
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-sk.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-sk.html new file mode 100644 index 0000000000000000000000000000000000000000..59444304796d1c9468fe6e0762d5ec7d03ec388f --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help-sk.html @@ -0,0 +1,19 @@ + +
+ HuaweiCloud Secret Key, used only when the credential id is null
+ config +
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help.html b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help.html new file mode 100644 index 0000000000000000000000000000000000000000..8205b30ee148ab4cd785cf78c07c726e1e944d1a --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/CodeArtsPipelineStep/help.html @@ -0,0 +1,20 @@ + +
+

+ CodeArts pipeline +

+
diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages.properties b/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages.properties new file mode 100644 index 0000000000000000000000000000000000000000..8d738f67924ae42f4f61c9097fe6e6c7f860f853 --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages.properties @@ -0,0 +1,7 @@ +codeCheckPublish.DisplayName=HuaweiCloud code Check + +codeCheckPublish.MissingAccessKey=Please set AccessKey +codeCheckPublish.MissingSecretKey=Please set SecretKey +codeCheckPublish.MissingRegion=Please set Region +codeCheckPublish.MissingProjectId=Please set ProjectId +codeCheckPublish.MissingTaskId=Please set TaskId diff --git a/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages_zh_CN.properties b/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages_zh_CN.properties new file mode 100644 index 0000000000000000000000000000000000000000..8dd6885b5c07089c3e7887931f991e0b4630131f --- /dev/null +++ b/src/main/resources/com/huawei/jenkins/cloudpipeline/Messages_zh_CN.properties @@ -0,0 +1,11 @@ +OBSPublish.DisplayName=\u534e\u4e3a\u4e91OBS\u4e0a\u4f20 + +OBSPublish.MissingEndpoint=\u8bf7\u8bbe\u7f6eEndpoint +OBSPublish.MissingAccessKeyId=\u8bf7\u8bbe\u7f6eAccessKeyId +OBSPublish.MissingAccessKeySecret=\u8bf7\u8bbe\u7f6eAccessKeySecret +OBSPublish.MissingBucketName=\u8bf7\u8bbe\u7f6eBucketName +OBSPublish.MissingLocalPath=\u8bf7\u8bbe\u7f6e\u672c\u5730\u8def\u5f84 +OBSPublish.MissingPath=\u8bf7\u8bbe\u7f6e\u8fdc\u7a0b\u8def\u5f84 +OBSPublish.MaxRetiesMustBeNumbers=\u6700\u5927\u91cd\u8bd5\u6b21\u6570\u5fc5\u987b\u4e3a\u6570\u5b57 +OBSPublish.MustBeginWithSlash=\u5fc5\u987b\u4ee5`/`\u5f00\u5934 +OBSPublish.IncludeException=\u89e3\u6790\u5f02\u5e38 \ No newline at end of file