forked from jenkinsci/bitbucket-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitBucketTrigger.java
More file actions
154 lines (128 loc) · 4.63 KB
/
BitBucketTrigger.java
File metadata and controls
154 lines (128 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.cloudbees.jenkins.plugins;
import hudson.Extension;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Job;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.util.FormValidation;
import hudson.util.SequentialExecutionQueue;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;
import org.apache.commons.jelly.XMLOutput;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import jakarta.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class BitBucketTrigger extends Trigger<Job<?, ?>> {
private String overrideUrl;
private Boolean buildOnCreatedBranch;
@DataBoundConstructor
public BitBucketTrigger() { }
// notice that the name of the getter must exactly like the parameter
public String getOverrideUrl() {
return overrideUrl;
}
@DataBoundSetter
public void setOverrideUrl(String overrideUrl){
this.overrideUrl = overrideUrl;
}
public Boolean getBuildOnCreatedBranch() {
return buildOnCreatedBranch;
}
@DataBoundSetter
public void setBuildOnCreatedBranch(Boolean buildOnCreatedBranch) {
this.buildOnCreatedBranch = buildOnCreatedBranch;
}
/**
* Called when a POST is made.
*/
public void onPost(String triggeredByUser, final String payload, String branchName) {
getDescriptor().queue.execute(
new BitBucketTriggerRunnable(
payload,
this.job,
LOGGER,
triggeredByUser,
branchName,
buildOnCreatedBranch != null && buildOnCreatedBranch // buildOnCreatedBranch == null ? false : buildOnCreatedBranch to prevent NPE
)
);
}
@Override
public Collection<? extends Action> getProjectActions() {
return Collections.singleton(new BitBucketWebHookPollingAction());
}
/**
* Returns the file that records the last/current polling activity.
*/
public File getLogFile() {
if ( job == null){
throw new RuntimeException("job is null");
} else {
return new File(job.getRootDir(),"bitbucket-polling.log");
}
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl)super.getDescriptor();
}
/**
* Action object. Used to display the polling log.
*/
public final class BitBucketWebHookPollingAction implements Action {
long start = 0;
public Job<?,?> getOwner() {
return job;
}
public String getIconFileName() {
return "clipboard.png";
}
public String getDisplayName() {
return "BitBucket Hook Log";
}
public String getUrlName() {
return "BitBucketPollLog";
}
public String getLog() throws IOException {
return Util.loadFile(getLogFile());
}
/**
* Writes the annotated log to the given output.
*/
public void writeLogTo(XMLOutput out) throws IOException {
start = new AnnotatedLargeText<>(getLogFile(), Charset.defaultCharset(), true, this).writeHtmlTo(start, out.asWriter());
}
}
@Extension @Symbol("bitbucketPush")
public static class DescriptorImpl extends TriggerDescriptor {
private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue(Hudson.MasterComputer.threadPoolForRemoting);
// Must be inside the DescriptorImpl
public FormValidation doCheckOverrideUrl(@QueryParameter String value) throws IOException, ServletException {
return FormValidation.ok();
}
@Override
public boolean isApplicable(Item item) {
return item instanceof Job && SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(item) != null
&& item instanceof ParameterizedJobMixIn.ParameterizedJob;
}
@Override
public String getDisplayName() {
return "Build when a change is pushed to BitBucket";
}
}
private static final Logger LOGGER = Logger.getLogger(BitBucketTrigger.class.getName());
}