-
Notifications
You must be signed in to change notification settings - Fork 39
/
ChangeRequestBuildStrategyImpl.java
203 lines (186 loc) · 7.2 KB
/
ChangeRequestBuildStrategyImpl.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* The MIT License
*
* Copyright (c) 2018, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.branch.buildstrategies.basic;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Functions;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import hudson.util.LogTaskListener;
import jenkins.branch.BranchBuildStrategy;
import jenkins.branch.BranchBuildStrategyDescriptor;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
import jenkins.scm.api.mixin.ChangeRequestSCMRevision;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.ProtectedExternally;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* A {@link BranchBuildStrategy} that builds change requests.
*
* @since 1.0.0
*/
public class ChangeRequestBuildStrategyImpl extends BranchBuildStrategy {
/**
* Our logger.
*/
private static final Logger LOGGER = Logger.getLogger(ChangeRequestBuildStrategyImpl.class.getName());
private final boolean ignoreTargetOnlyChanges;
private final boolean ignoreUntrustedChanges;
/**
* Our constructor.
*
* @param ignoreTargetOnlyChanges {@code true} to ignore merge revision changes where the only difference is the
* target branch revision.
* @deprecated use {@link #ChangeRequestBuildStrategyImpl(boolean, boolean)}
* @since 1.2.0
*/
@Deprecated
public ChangeRequestBuildStrategyImpl(boolean ignoreTargetOnlyChanges) {
this(ignoreTargetOnlyChanges, false);
}
/**
* Our constructor.
*
* @param ignoreTargetOnlyChanges {@code true} to ignore merge revision changes where the only difference is
* the target branch revision.
* @param ignoreUntrustedChanges {@code true} to check the trusted revision and ignore if different, which
* would have the effect of ignoring change requests that originate from an untrusted source.
*/
@DataBoundConstructor
public ChangeRequestBuildStrategyImpl(boolean ignoreTargetOnlyChanges, boolean ignoreUntrustedChanges) {
this.ignoreTargetOnlyChanges = ignoreTargetOnlyChanges;
this.ignoreUntrustedChanges = ignoreUntrustedChanges;
}
public boolean isIgnoreTargetOnlyChanges() {
return ignoreTargetOnlyChanges;
}
public boolean isIgnoreUntrustedChanges() {
return ignoreUntrustedChanges;
}
/**
* {@inheritDoc}
*/
@Deprecated
@Override
public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision,
@CheckForNull SCMRevision prevRevision) {
return isAutomaticBuild(source, head, currRevision, prevRevision, new LogTaskListener(Logger.getLogger(getClass().getName()), Level.INFO));
}
/**
* {@inheritDoc}
*/
@Restricted(ProtectedExternally.class)
@Deprecated
@Override
public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision,
@CheckForNull SCMRevision prevRevision, @NonNull TaskListener taskListener) {
return isAutomaticBuild(source,head, currRevision, prevRevision, prevRevision, taskListener);
}
/**
* {@inheritDoc}
*/
@Restricted(ProtectedExternally.class)
@Override
public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision,
@CheckForNull SCMRevision lastBuiltRevision, @CheckForNull SCMRevision lastSeenRevision, @NonNull TaskListener listener) {
if (!(head instanceof ChangeRequestSCMHead)) {
return false;
}
if (ignoreTargetOnlyChanges
&& currRevision instanceof ChangeRequestSCMRevision
&& lastBuiltRevision instanceof ChangeRequestSCMRevision) {
ChangeRequestSCMRevision<?> curr = (ChangeRequestSCMRevision<?>) currRevision;
if (curr.isMerge() && curr.equivalent((ChangeRequestSCMRevision<?>) lastBuiltRevision)) {
return false;
}
}
try {
if (ignoreUntrustedChanges && !currRevision.equals(source.getTrustedRevision(currRevision, listener))) {
return false;
}
} catch (IOException | InterruptedException e) {
LogRecord lr = new LogRecord(Level.WARNING,
"Could not determine trust status for revision {0} of {1}, assuming untrusted");
lr.setParameters(new Object[] {currRevision, head});
lr.setThrown(e);
Functions.printLogRecord(lr);
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ChangeRequestBuildStrategyImpl that = (ChangeRequestBuildStrategyImpl) o;
return ignoreTargetOnlyChanges == that.ignoreTargetOnlyChanges;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return (ignoreTargetOnlyChanges ? 1 : 0) + (ignoreUntrustedChanges ? 2 : 0);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "ChangeRequestBuildStrategyImpl{" +
"ignoreTargetOnlyChanges=" + ignoreTargetOnlyChanges +
"ignoreUntrustedChanges=" + ignoreUntrustedChanges +
'}';
}
/**
* Our descriptor.
*/
@Symbol("buildChangeRequests")
@Extension
public static class DescriptorImpl extends BranchBuildStrategyDescriptor {
/**
* {@inheritDoc}
*/
@NonNull
@Override
public String getDisplayName() {
return Messages.ChangeRequestBuildStrategyImpl_displayName();
}
}
}