|
| 1 | +package stashpullrequestbuilder.stashpullrequestbuilder; |
| 2 | + |
| 3 | +import antlr.ANTLRException; |
| 4 | +import hudson.Extension; |
| 5 | +import hudson.model.*; |
| 6 | +import hudson.model.queue.QueueTaskFuture; |
| 7 | +import hudson.triggers.Trigger; |
| 8 | +import hudson.triggers.TriggerDescriptor; |
| 9 | +import net.sf.json.JSONObject; |
| 10 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 11 | +import org.kohsuke.stapler.StaplerRequest; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by Nathan McCarthy |
| 21 | + */ |
| 22 | +public class StashBuildTrigger extends Trigger<AbstractProject<?, ?>> { |
| 23 | + private static final Logger logger = Logger.getLogger(StashBuildTrigger.class.getName()); |
| 24 | + private final String projectPath; |
| 25 | + private final String cron; |
| 26 | + private final String stashHost; |
| 27 | + private final String username; |
| 28 | + private final String password; |
| 29 | + private final String projectCode; |
| 30 | + private final String repositoryName; |
| 31 | + private final String ciSkipPhrases; |
| 32 | + private final boolean checkDestinationCommit; |
| 33 | + |
| 34 | + transient private StashPullRequestsBuilder stashPullRequestsBuilder; |
| 35 | + |
| 36 | + @Extension |
| 37 | + public static final StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor(); |
| 38 | + |
| 39 | + @DataBoundConstructor |
| 40 | + public StashBuildTrigger( |
| 41 | + String projectPath, |
| 42 | + String cron, |
| 43 | + String stashHost, |
| 44 | + String username, |
| 45 | + String password, |
| 46 | + String projectCode, |
| 47 | + String repositoryName, |
| 48 | + String ciSkipPhrases, |
| 49 | + boolean checkDestinationCommit |
| 50 | + ) throws ANTLRException { |
| 51 | + super(cron); |
| 52 | + this.projectPath = projectPath; |
| 53 | + this.cron = cron; |
| 54 | + this.stashHost = stashHost; |
| 55 | + this.username = username; |
| 56 | + this.password = password; |
| 57 | + this.projectCode = projectCode; |
| 58 | + this.repositoryName = repositoryName; |
| 59 | + this.ciSkipPhrases = ciSkipPhrases; |
| 60 | + this.checkDestinationCommit = checkDestinationCommit; |
| 61 | + } |
| 62 | + |
| 63 | + public String getStashHost() { |
| 64 | + return stashHost; |
| 65 | + } |
| 66 | + |
| 67 | + public String getProjectPath() { |
| 68 | + return this.projectPath; |
| 69 | + } |
| 70 | + |
| 71 | + public String getCron() { |
| 72 | + return this.cron; |
| 73 | + } |
| 74 | + |
| 75 | + public String getUsername() { |
| 76 | + return username; |
| 77 | + } |
| 78 | + |
| 79 | + public String getPassword() { |
| 80 | + return password; |
| 81 | + } |
| 82 | + |
| 83 | + public String getProjectCode() { |
| 84 | + return projectCode; |
| 85 | + } |
| 86 | + |
| 87 | + public String getRepositoryName() { |
| 88 | + return repositoryName; |
| 89 | + } |
| 90 | + |
| 91 | + public String getCiSkipPhrases() { |
| 92 | + return ciSkipPhrases; |
| 93 | + } |
| 94 | + |
| 95 | + public boolean getCheckDestinationCommit() { |
| 96 | + return checkDestinationCommit; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void start(AbstractProject<?, ?> project, boolean newInstance) { |
| 101 | + try { |
| 102 | + this.stashPullRequestsBuilder = StashPullRequestsBuilder.getBuilder(); |
| 103 | + this.stashPullRequestsBuilder.setProject(project); |
| 104 | + this.stashPullRequestsBuilder.setTrigger(this); |
| 105 | + this.stashPullRequestsBuilder.setupBuilder(); |
| 106 | + } catch(IllegalStateException e) { |
| 107 | + logger.log(Level.SEVERE, "Can't start trigger", e); |
| 108 | + return; |
| 109 | + } |
| 110 | + super.start(project, newInstance); |
| 111 | + } |
| 112 | + |
| 113 | + public static StashBuildTrigger getTrigger(AbstractProject project) { |
| 114 | + Trigger trigger = project.getTrigger(StashBuildTrigger.class); |
| 115 | + return (StashBuildTrigger)trigger; |
| 116 | + } |
| 117 | + |
| 118 | + public StashPullRequestsBuilder getBuilder() { |
| 119 | + return this.stashPullRequestsBuilder; |
| 120 | + } |
| 121 | + |
| 122 | + public QueueTaskFuture<?> startJob(StashCause cause) { |
| 123 | + Map<String, ParameterValue> values = new HashMap<String, ParameterValue>(); |
| 124 | + values.put("sourceBranch", new StringParameterValue("sourceBranch", cause.getSourceBranch())); |
| 125 | + values.put("targetBranch", new StringParameterValue("targetBranch", cause.getTargetBranch())); |
| 126 | + values.put("projectCode", new StringParameterValue("projectCode", cause.getRepositoryOwner())); |
| 127 | + values.put("repositoryName", new StringParameterValue("repositoryName", cause.getRepositoryName())); |
| 128 | + values.put("pullRequestId", new StringParameterValue("pullRequestId", cause.getPullRequestId())); |
| 129 | + values.put("destinationRepositoryOwner", new StringParameterValue("destinationRepositoryOwner", cause.getDestinationRepositoryOwner())); |
| 130 | + values.put("destinationRepositoryName", new StringParameterValue("destinationRepositoryName", cause.getDestinationRepositoryName())); |
| 131 | + values.put("pullRequestTitle", new StringParameterValue("pullRequestTitle", cause.getPullRequestTitle())); |
| 132 | + return this.job.scheduleBuild2(0, cause, new ParametersAction(new ArrayList(values.values()))); |
| 133 | + } |
| 134 | + |
| 135 | + private Map<String, ParameterValue> getDefaultParameters() { |
| 136 | + Map<String, ParameterValue> values = new HashMap<String, ParameterValue>(); |
| 137 | + ParametersDefinitionProperty definitionProperty = this.job.getProperty(ParametersDefinitionProperty.class); |
| 138 | + |
| 139 | + if (definitionProperty != null) { |
| 140 | + for (ParameterDefinition definition : definitionProperty.getParameterDefinitions()) { |
| 141 | + values.put(definition.getName(), definition.getDefaultParameterValue()); |
| 142 | + } |
| 143 | + } |
| 144 | + return values; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void run() { |
| 149 | + if(this.getBuilder().getProject().isDisabled()) { |
| 150 | + logger.info("Build Skip."); |
| 151 | + } else { |
| 152 | + this.stashPullRequestsBuilder.run(); |
| 153 | + } |
| 154 | + this.getDescriptor().save(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void stop() { |
| 159 | + super.stop(); |
| 160 | + } |
| 161 | + |
| 162 | + public static final class StashBuildTriggerDescriptor extends TriggerDescriptor { |
| 163 | + public StashBuildTriggerDescriptor() { |
| 164 | + load(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public boolean isApplicable(Item item) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String getDisplayName() { |
| 174 | + return "Stash Pull Requests Builder"; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean configure(StaplerRequest req, JSONObject json) throws FormException { |
| 179 | + save(); |
| 180 | + return super.configure(req, json); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments