WXWorkRobotProperty
* @@ -125,13 +127,16 @@ public FormValidation doTest(@QueryParameter("id") String id, @QueryParameter("n RobotProperty property = new WXWorkRobotProperty(id, name, webhook); RobotRequest message = TextMessage.builder().content("企业微信机器人测试成功!").atAll(true).build(); RobotResponse robotResponse = WXWorkRobotMessageSender.instance().send(property, message); - if (robotResponse != null && robotResponse.isOk()) { - // ok - String rootUrl = Jenkins.get().getRootUrl(); - return FormValidation.respond(FormValidation.Kind.OK, "" + "测试成功"); - } else { - return FormValidation.error(robotResponse.errorMessage()); + if (Objects.nonNull(robotResponse)) { + if (robotResponse.isOk()) { + // ok + String rootUrl = Jenkins.get().getRootUrl(); + return FormValidation.respond(FormValidation.Kind.OK, "" + "测试成功"); + } else { + return FormValidation.error(robotResponse.errorMessage()); + } } + return FormValidation.error("企业微信机器人测试出现错误!"); } } } diff --git a/src/main/java/io/jenkins/plugins/wxwork/WXWorkPipelineBuilder.java b/src/main/java/io/jenkins/plugins/wxwork/WXWorkStep.java similarity index 52% rename from src/main/java/io/jenkins/plugins/wxwork/WXWorkPipelineBuilder.java rename to src/main/java/io/jenkins/plugins/wxwork/WXWorkStep.java index a867590..f87f6e7 100644 --- a/src/main/java/io/jenkins/plugins/wxwork/WXWorkPipelineBuilder.java +++ b/src/main/java/io/jenkins/plugins/wxwork/WXWorkStep.java @@ -4,45 +4,36 @@ import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; -import hudson.Launcher; -import hudson.model.AbstractProject; import hudson.model.Run; import hudson.model.TaskListener; -import hudson.tasks.BuildStepDescriptor; -import hudson.tasks.Builder; import io.jenkins.plugins.wxwork.bo.RobotPipelineVars; import io.jenkins.plugins.wxwork.bo.RunUser; +import io.jenkins.plugins.wxwork.contract.RobotMessageSender; import io.jenkins.plugins.wxwork.contract.RobotProperty; import io.jenkins.plugins.wxwork.contract.RobotRequest; import io.jenkins.plugins.wxwork.contract.RobotResponse; -import io.jenkins.plugins.wxwork.contract.RobotMessageSender; import io.jenkins.plugins.wxwork.enums.MessageType; import io.jenkins.plugins.wxwork.factory.RobotMessageFactory; import io.jenkins.plugins.wxwork.robot.WXWorkRobotMessageSender; import io.jenkins.plugins.wxwork.utils.JenkinsUtils; -import jenkins.tasks.SimpleBuildStep; import lombok.Getter; import lombok.Setter; -import org.jenkinsci.Symbol; +import org.jenkinsci.plugins.workflow.steps.Step; +import org.jenkinsci.plugins.workflow.steps.StepContext; +import org.jenkinsci.plugins.workflow.steps.StepDescriptor; +import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** - *Pipeline支持
- * - * @author nekoimi 2022/07/12 + * @author nekoimi 2024/8/6 23:56 */ @Getter @Setter @SuppressWarnings("unused") -public class WXWorkPipelineBuilder extends Builder implements SimpleBuildStep { - +public class WXWorkStep extends Step { /** * 机器人ID */ @@ -84,7 +75,7 @@ public class WXWorkPipelineBuilder extends Builder implements SimpleBuildStep { private final RobotMessageSender robotSender = WXWorkRobotMessageSender.instance(); @DataBoundConstructor - public WXWorkPipelineBuilder(String robot) { + public WXWorkStep(String robot) { this.robot = robot; } @@ -120,43 +111,100 @@ public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } - @Override - public void perform( - @NonNull Run, ?> run, - @NonNull FilePath workspace, - @NonNull EnvVars envVars, - @NonNull Launcher launcher, - @NonNull TaskListener listener) throws InterruptedException, IOException { + /** + *发送机器人消息
+ * + * @param run + * @param workspace + * @param envVars + * @param listener + */ + public void send(Run, ?> run, FilePath workspace, EnvVars envVars, TaskListener listener) { RobotProperty property = WXWorkGlobalConfig.instance().getRobotPropertyById(robot); - if (property == null) { + if (Objects.isNull(property)) { listener.error("机器人[%s]配置找不到!", robot); return; } RunUser runUser = JenkinsUtils.getRunUser(run, listener); RobotPipelineVars pipelineVars = RobotPipelineVars.builder() - .robot(envVars.expand(this.robot)).type(this.type).atMe(this.atMe).atAll(this.atAll) - .at(this.at).text(this.text).imageUrl(envVars.expand(this.imageUrl)).runUser(runUser) - .envVars(envVars).workspace(workspace).listener(listener).build(); + .run(run).envVars(envVars).workspace(workspace).listener(listener) + .runUser(runUser) + .robot(JenkinsUtils.expandAll(run, workspace, listener, this.robot)) + .type(this.type).atMe(this.atMe).atAll(this.atAll).at(this.at) + .text(this.text) + .imageUrl(JenkinsUtils.expandAll(run, workspace, listener, this.imageUrl)) + .build(); RobotRequest robotRequest = RobotMessageFactory.makeRobotRequest(pipelineVars); if (robotRequest == null) { listener.error("不支持的消息!"); return; } RobotResponse robotResponse = robotSender.send(property, robotRequest); - if (robotResponse != null && robotResponse.isOk()) { - listener.getLogger().println("WXWORK: 微信机器人[" + property.name() + "]推送消息成功!"); - } else { - listener.error(robotResponse.errorMessage()); + if (Objects.nonNull(robotResponse)) { + if (robotResponse.isOk()) { + listener.getLogger().println("WXWORK: 微信机器人[" + property.name() + "]推送消息成功!"); + } else { + listener.error("WXWORK: 微信机器人[" + property.name() + "]推送消息失败:" + robotResponse.errorMessage()); + } + } + } + + @Override + public StepExecution start(StepContext context) throws Exception { + return new WXWorkStepExecution(this, context); + } + + @Override + public StepDescriptor getDescriptor() { + return new WXWorkStepDescriptor(); + } + + /** + * 执行 + */ + static class WXWorkStepExecution extends StepExecution { + private static final long serialVersionUID = 1L; + private final transient WXWorkStep step; + + public WXWorkStepExecution(WXWorkStep step, @NonNull StepContext context) { + super(context); + this.step = step; + } + + @Override + public boolean start() throws Exception { + StepContext context = this.getContext(); + + Run, ?> run = context.get(Run.class); + FilePath workspace = context.get(FilePath.class); + EnvVars envVars = context.get(EnvVars.class); + TaskListener listener = context.get(TaskListener.class); + + try { + this.step.send(run, workspace, envVars, listener); + context.onSuccess(true); + } catch (Exception e) { + context.onFailure(e); + } + + return true; } } - @Symbol({"wxwork", "wxWork"}) @Extension - public final static class WXWorkDescriptorImpl extends BuildStepDescriptorRobotMessageTransfer
@@ -23,4 +29,20 @@ public interface RobotMessageTransfer { * @param pipelineVars Pipeline参数 */ RobotRequest transferRobotRequest(RobotPipelineVars pipelineVars); + + /** + *处理文本列表消息
+ * + * @param pipelineVars Pipeline参数 + * @return + */ + default String transferTextList(RobotPipelineVars pipelineVars) { + ListMarkdownMessageTransfer
@@ -25,9 +21,7 @@ public boolean supports(MessageType messageType) { @Override public RobotRequest transferRobotRequest(RobotPipelineVars pipelineVars) { MarkdownMessage.Builder builder = MarkdownMessage.builder(); - ListTextMessageTransfer
@@ -33,10 +28,7 @@ public RobotRequest transferRobotRequest(RobotPipelineVars pipelineVars) { if (Boolean.TRUE.equals(pipelineVars.getAtAll())) { builder.atAll(true); } - List获取执行用户
* - * @param run {@link Run} + * @param run {@link Run} * @param listener {@link TaskListener} */ public static RunUser getRunUser(Run, ?> run, TaskListener listener) { @@ -46,7 +93,7 @@ public static RunUser getRunUser(Run, ?> run, TaskListener listener) { listener.error("用户【%s】暂未设置手机号码,请前往 %s 添加。", executorName, user.getAbsoluteUrl() + "/configure"); } else { executorMobile = executorProperty.getMobile(); - if (StrUtils.isBlank(executorMobile)){ + if (StrUtils.isBlank(executorMobile)) { listener.error("用户【%s】暂未设置手机号码,请前往 %s 添加。", executorName, user.getAbsoluteUrl() + "/configure"); } } diff --git a/src/main/resources/io/jenkins/plugins/wxwork/WXWorkGlobalConfig/config.jelly b/src/main/resources/io/jenkins/plugins/wxwork/WXWorkGlobalConfig/config.jelly index bab4ee3..60d9db7 100644 --- a/src/main/resources/io/jenkins/plugins/wxwork/WXWorkGlobalConfig/config.jelly +++ b/src/main/resources/io/jenkins/plugins/wxwork/WXWorkGlobalConfig/config.jelly @@ -1,6 +1,6 @@