Skip to content

Commit 2197764

Browse files
committed
LibraryConfiguration: extend with allowBRANCH_NAME_PR to let CHANGE_BRANCH and CHANGE_TARGET be considered as fallbacks [JENKINS-69731]
1 parent 58f2591 commit 2197764

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryConfiguration.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class LibraryConfiguration extends AbstractDescribableImpl<LibraryConfigu
6161
private boolean implicit;
6262
private boolean allowVersionOverride = true;
6363
private boolean allowBRANCH_NAME = false;
64+
private boolean allowBRANCH_NAME_PR = false;
6465
private boolean includeInChangesets = true;
6566
private LibraryCachingConfiguration cachingConfiguration = null;
6667

@@ -129,6 +130,14 @@ public boolean isAllowBRANCH_NAME() {
129130
this.allowBRANCH_NAME = allowBRANCH_NAME;
130131
}
131132

133+
public boolean isAllowBRANCH_NAME_PR() {
134+
return allowBRANCH_NAME_PR;
135+
}
136+
137+
@DataBoundSetter public void setAllowBRANCH_NAME_PR(boolean allowBRANCH_NAME_PR) {
138+
this.allowBRANCH_NAME_PR = allowBRANCH_NAME_PR;
139+
}
140+
132141
/**
133142
* Whether to include library changes in reported changes in a job {@link #getIncludeInChangesets}.
134143
*/
@@ -202,6 +211,47 @@ public LibraryCachingConfiguration getCachingConfiguration() {
202211
if (fv != null && fv.kind == FormValidation.Kind.OK) {
203212
return runVersion;
204213
}
214+
215+
if (runVersion.startsWith("PR-") && allowBRANCH_NAME_PR) {
216+
// MultiBranch Pipeline support for pull requests
217+
// sets BRANCH_NAME="PR-123" and keeps source
218+
// and target branch names in CHANGE_BRANCH and
219+
// CHANGE_TARGET respectively.
220+
221+
// First check for possible PR-source branch of
222+
// pipeline coordinated with a PR of trusted
223+
// shared library (if branch exists in library,
224+
// after repo protections involved, it is already
225+
// somewhat trustworthy):
226+
try {
227+
runVersion = run.getEnvironment(listener).get("CHANGE_BRANCH", null);
228+
} catch (Exception x) {
229+
runVersion = null;
230+
}
231+
if (runVersion != null && !("".equals(runVersion))) {
232+
fv = retriever.validateVersion(name, runVersion, runParent);
233+
234+
if (fv != null && fv.kind == FormValidation.Kind.OK) {
235+
return runVersion;
236+
}
237+
}
238+
239+
// Next check for possible PR-target branch of
240+
// pipeline coordinated with existing version of
241+
// trusted shared library:
242+
try {
243+
runVersion = run.getEnvironment(listener).get("CHANGE_TARGET", null);
244+
} catch (Exception x) {
245+
runVersion = null;
246+
}
247+
if (runVersion != null && !("".equals(runVersion))) {
248+
fv = retriever.validateVersion(name, runVersion, runParent);
249+
250+
if (fv != null && fv.kind == FormValidation.Kind.OK) {
251+
return runVersion;
252+
}
253+
}
254+
}
205255
}
206256

207257
// No retriever, or its validateVersion() did not confirm
@@ -236,7 +286,7 @@ public FormValidation doCheckName(@QueryParameter String name) {
236286
}
237287

238288
@RequirePOST
239-
public FormValidation doCheckDefaultVersion(@AncestorInPath Item context, @QueryParameter String defaultVersion, @QueryParameter boolean implicit, @QueryParameter boolean allowVersionOverride, @QueryParameter boolean allowBRANCH_NAME, @QueryParameter String name) {
289+
public FormValidation doCheckDefaultVersion(@AncestorInPath Item context, @QueryParameter String defaultVersion, @QueryParameter boolean implicit, @QueryParameter boolean allowVersionOverride, @QueryParameter boolean allowBRANCH_NAME, @QueryParameter boolean allowBRANCH_NAME_PR, @QueryParameter String name) {
240290
if (defaultVersion.isEmpty()) {
241291
if (implicit) {
242292
return FormValidation.error("If you load a library implicitly, you must specify a default version.");
@@ -247,6 +297,9 @@ public FormValidation doCheckDefaultVersion(@AncestorInPath Item context, @Query
247297
if (!allowVersionOverride) {
248298
return FormValidation.error("If you deny overriding a default version, you must define that version.");
249299
}
300+
if (allowBRANCH_NAME_PR) {
301+
return FormValidation.warning("This setting has no effect when you do not allow use of literal '@${BRANCH_NAME}' for overriding a default version");
302+
}
250303
return FormValidation.ok();
251304
} else {
252305
if ("${BRANCH_NAME}".equals(defaultVersion)) {

src/main/resources/org/jenkinsci/plugins/workflow/libs/LibraryConfiguration/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ THE SOFTWARE.
4040
<f:entry field="allowBRANCH_NAME" title="${%Allow literal use of @Library 'libname@$${BRANCH_NAME}' in pipeline scripts from SCM; falls back to default version if BRANCH_NAME is not available or not found}">
4141
<f:checkbox default="false"/>
4242
</f:entry>
43+
<f:entry field="allowBRANCH_NAME_PR" title="${%If you allow literal use of @Library 'libname@$${BRANCH_NAME}' above, you can also allow fallback to CHANGE_BRANCH and/or CHANGE_TARGET for pull request builds}">
44+
<f:checkbox default="false"/>
45+
</f:entry>
4346
<f:entry field="includeInChangesets" title="${%Include @Library changes in job recent changes}">
4447
<f:checkbox default="true"/>
4548
</f:entry>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
If checked, and if scripts are allowed to select a custom version
3+
of the library by appending literally <code>@${BRANCH_NAME}</code>
4+
in the <code>@Library</code> annotation, then for pull request
5+
builds additional fall-back library branches would include the
6+
names used as <code>CHANGE_BRANCH</code> (name of source branch
7+
of pipeline pull request) and <code>CHANGE_TARGET</code> (name
8+
of target branch of pipeline pull request), if such branch names
9+
already exist in the trusted shared library repository.
10+
</div>

0 commit comments

Comments
 (0)