Skip to content

Commit 64a6147

Browse files
authoredOct 25, 2024
Merge pull request #476 from janfaracik/show-pr-title
[JENKINS-60082] Show pull request title in name column
2 parents ae67548 + 4a3ebec commit 64a6147

File tree

5 files changed

+36
-57
lines changed

5 files changed

+36
-57
lines changed
 

‎src/main/java/jenkins/branch/ItemColumn.java

-25
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,6 @@ public boolean isOrphaned(Object item) {
100100
return false;
101101
}
102102

103-
/**
104-
* Gets the tool-tip title of a job.
105-
*
106-
* @param job the job.
107-
* @return the tool-tip title unescaped for use in an attribute.
108-
*/
109-
@SuppressWarnings("unused") // used via Jelly EL binding
110-
public String getTitle(Object job) {
111-
// Jelly will take care of quote and ampersand escaping for us
112-
if (job instanceof Actionable) {
113-
Actionable actionable = (Actionable) job;
114-
ObjectMetadataAction action = actionable.getAction(ObjectMetadataAction.class);
115-
if (action != null) {
116-
String displayName = action.getObjectDisplayName();
117-
if (StringUtils.isBlank(displayName) || displayName.equals(actionable.getDisplayName())) {
118-
// if the display name is the same, then the description is more useful
119-
String description = action.getObjectDescription();
120-
return description != null ? description : displayName;
121-
}
122-
return displayName;
123-
}
124-
}
125-
return null;
126-
}
127-
128103
/**
129104
* Our extension.
130105
*/

‎src/main/java/jenkins/branch/MultiBranchProject.java

+9-14
Original file line numberDiff line numberDiff line change
@@ -2104,22 +2104,16 @@ private void observeNew(@NonNull SCMHead head, @NonNull SCMRevision revision, @N
21042104
throw new IllegalStateException(
21052105
"Name of created project " + project + " did not match expected " + encodedName);
21062106
}
2107-
// HACK ALERT
2108-
// ==========
2109-
// We don't want to trigger a save, so we will do some trickery to ensure that observer.created(project)
2110-
// performs the save
2111-
BulkChange bc = new BulkChange(project);
2112-
try {
2113-
project.setDisplayName(getProjectDisplayName(project, rawName));
2114-
} catch (IOException e) {
2115-
// ignore even if it does happen we didn't want a save
2116-
} finally {
2117-
bc.abort();
2118-
}
21192107
// decorate contract is to ensure it does not trigger a save
21202108
_factory.decorate(project);
21212109
// ok it is now up to the observer to ensure it does the actual save.
2122-
observer.created(project);
2110+
try (BulkChange bc = new BulkChange(project);) {
2111+
observer.created(project);
2112+
project.setDisplayName(getProjectDisplayName(project, rawName));
2113+
bc.commit();
2114+
} catch (IOException e) {
2115+
// Ignored
2116+
}
21232117
doAutomaticBuilds(head, revision, rawName, project, revisionActions, null, null);
21242118
}
21252119

@@ -2137,8 +2131,9 @@ private String getProjectDisplayName(@NonNull P project, @NonNull String rawName
21372131
.orElse(null);
21382132
}
21392133

2134+
// Default to displaying the project's display name if a trait hasn't been provided
21402135
if (naming == null) {
2141-
return rawName;
2136+
naming = MultiBranchProjectDisplayNamingStrategy.RAW_AND_OBJECT_DISPLAY_NAME;
21422137
}
21432138

21442139
final ObjectMetadataAction action = naming.needsObjectDisplayName()

‎src/main/java/jenkins/branch/MultiBranchProjectDisplayNamingStrategy.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ public String generateName(@NonNull final String rawName, final String displayNa
6060
return rawName;
6161
}
6262

63-
return format("%s - %s", rawName, displayName);
63+
// The raw name provided here in the context of pull requests is the pull request ID
64+
// We tidy up the ID so that they display consistently between SCMs
65+
String cleanedUpBranchName = rawName;
66+
if (cleanedUpBranchName.startsWith("MR-") || cleanedUpBranchName.startsWith("PR-")) {
67+
cleanedUpBranchName = "#" + cleanedUpBranchName.substring(3);
68+
}
69+
70+
return format("%s (%s)", displayName, cleanedUpBranchName);
6471
}
6572
},
6673
;

‎src/main/resources/jenkins/branch/ItemColumn/column.jelly

+12-10
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@ THE SOFTWARE.
2525
<?jelly escape-by-default='true'?>
2626
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
2727
xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
28-
<td style="${indenter.getCss(job)}">
28+
<d:taglib uri="local">
29+
<d:tag name="link">
30+
<a href="${jobBaseUrl}${job.shortUrl}" class='model-link jenkins-table__link'>
31+
<l:breakable value="${h.getRelativeDisplayNameFrom(job, itemGroup)}"/>
32+
</a>
33+
</d:tag>
34+
</d:taglib>
35+
36+
<td style="${indenter.getCss(job)}" xmlns:local="local">
2937
<j:choose>
3038
<j:when test="${it.isOrphaned(job)}">
3139
<s>
32-
<a href="${jobBaseUrl}${job.shortUrl}" class='model-link inside jenkins-table_link' title="${it.getTitle(job)}">
33-
<l:breakable value="${h.getRelativeDisplayNameFrom(job, itemGroup)}"/>
34-
</a>
40+
<local:link />
3541
</s>
3642
</j:when>
3743
<j:when test="${it.isPrimary(job)}">
3844
<strong>
39-
<a href="${jobBaseUrl}${job.shortUrl}" class='model-link inside jenkins-table_link' title="${it.getTitle(job)}">
40-
<l:breakable value="${h.getRelativeDisplayNameFrom(job, itemGroup)}"/>
41-
</a>
45+
<local:link />
4246
</strong>
4347
</j:when>
4448
<j:otherwise>
45-
<a href="${jobBaseUrl}${job.shortUrl}" class='model-link inside jenkins-table_link' title="${it.getTitle(job)}">
46-
<l:breakable value="${h.getRelativeDisplayNameFrom(job, itemGroup)}"/>
47-
</a>
49+
<local:link />
4850
</j:otherwise>
4951
</j:choose>
5052
</td>

‎src/test/java/integration/CategorizationTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void given_multibranch_when_changeRequestsWanted_then_onlyUncategorizedAn
136136
assertThat(prj.getItems(),
137137
containsInAnyOrder(
138138
hasProperty("displayName", is("master")),
139-
hasProperty("displayName", is("CR-" + crNum))
139+
hasProperty("displayName", is("Change request #1 (CR-1)"))
140140
)
141141
);
142142
assertThat(prj.getViews(),
@@ -204,7 +204,7 @@ public void given_multibranch_when_noBranchesWanted_then_uncategorizedViewPresen
204204
assertThat(prj.getItems(),
205205
containsInAnyOrder(
206206
hasProperty("displayName", is("master-1.0")),
207-
hasProperty("displayName", is("CR-" + crNum))
207+
hasProperty("displayName", is("Change request #1 (CR-1)"))
208208
)
209209
);
210210
assertThat(prj.getViews(),
@@ -222,7 +222,7 @@ public void given_multibranch_when_noBranchesWanted_then_uncategorizedViewPresen
222222
allOf(
223223
instanceOf(MultiBranchProjectViewHolder.ViewImpl.class),
224224
hasProperty("viewName", is(ChangeRequestSCMHeadCategory.DEFAULT.getName())),
225-
hasProperty("items", contains(hasProperty("displayName", is("CR-" + crNum))))
225+
hasProperty("items", contains(hasProperty("displayName", is("Change request #1 (CR-1)"))))
226226
)
227227
));
228228
}
@@ -253,8 +253,8 @@ public void given_multibranch_when_wantsEverything_then_hasEverything()
253253
hasProperty("displayName", is("feature")),
254254
hasProperty("displayName", is("master-1.0")),
255255
hasProperty("displayName", is("master-1.1")),
256-
hasProperty("displayName", is("CR-" + crNum1)),
257-
hasProperty("displayName", is("CR-" + crNum2))
256+
hasProperty("displayName", is("Change request #1 (CR-1)")),
257+
hasProperty("displayName", is("Change request #2 (CR-2)"))
258258
)
259259
);
260260
assertThat(prj.getViews(),
@@ -279,8 +279,8 @@ public void given_multibranch_when_wantsEverything_then_hasEverything()
279279
instanceOf(MultiBranchProjectViewHolder.ViewImpl.class),
280280
hasProperty("viewName", is(ChangeRequestSCMHeadCategory.DEFAULT.getName())),
281281
hasProperty("items", containsInAnyOrder(
282-
hasProperty("displayName", is("CR-" + crNum2)),
283-
hasProperty("displayName", is("CR-" + crNum1))
282+
hasProperty("displayName", is("Change request #1 (CR-1)")),
283+
hasProperty("displayName", is("Change request #2 (CR-2)"))
284284
))
285285
)
286286
));

0 commit comments

Comments
 (0)