8000 [JENKINS-36445] Show first 7 characters of SHA1 in change log hyperli… · github-cloud/github-plugin@ca75bde · GitHub
[go: up one dir, main page]

Skip to content

Commit ca75bde

Browse files
MarkEWaitelanwen
authored andcommitted
[JENKINS-36445] Show first 7 characters of SHA1 in change log hyperlink (jenkinsci#128)
Most git repository browsing systems (bitbucket, cgit, github, gitweb) display a short form of the SHA1 (commonly the first 7 characters) with a clickable hyperlink that will take them to a page that includes the full SHA1. This change reduces the visible SHA1 text in the list of changes to the first 7 characters of the SHA1, consistent with those other repository browsers. It also adds assertions that the expected format is used and simplifies the existing annotator tests.
1 parent e9caf00 commit ca75bde

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

src/main/java/com/coravy/hudson/plugins/github/GithubLinkAnnotator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import hudson.scm.ChangeLogAnnotator;
99
import hudson.scm.ChangeLogSet.Entry;
1010

11+
import static java.lang.String.format;
12+
1113
import java.util.regex.Pattern;
1214

1315
/**
@@ -42,7 +44,10 @@ void annotate(final GithubUrl url, final MarkupText text, final Entry change) {
4244

4345
if (change instanceof GitChangeSet) {
4446
GitChangeSet cs = (GitChangeSet) change;
45-
text.wrapBy("", " (<a href='" + url.commitId(cs.getId()) + "'>commit: " + cs.getId() + "</a>)");
47+
final String id = cs.getId();
48+
text.wrapBy("", format(" (<a href='%s'>commit: %s</a>)",
49+
url.commitId(id),
50+
id.substring(0, Math.min(id.length(), 7))));
4651
}
4752
}
4853

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,91 @@
11
package com.coravy.hudson.plugins.github;
22

3-
import static org.junit.Assert.assertEquals;
3+
import com.tngtech.java.junit.dataprovider.DataProvider;
4+
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
5+
import com.tngtech.java.junit.dataprovider.UseDataProvider;
46
import hudson.MarkupText;
5-
7+
import hudson.plugins.git.GitChangeSet;
8+
import java.util.ArrayList;
9+
import java.util.Random;
10+
import org.junit.Before;
611
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import static java.lang.String.format;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.is;
716

17+
@RunWith(DataProviderRunner.class)
818
public class GithubLinkAnnotatorTest {
919

10-
private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools/";
20+
private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools";
21+
private final static String SHA1 = "badbeef136cd854f4dd6fa40bf94c0c657681dd5";
22+
private final static Random RANDOM = new Random();
23+
private final String expectedChangeSetAnnotation = " ("
24+
+ "<a href='" + GITHUB_URL + "/commit/" + SHA1 + "'>"
25+
+ "commit: " + SHA1.substring(0, 7)
26+
+ "</a>)";
27+
private static GitChangeSet changeSet;
28+
29+
@Before
30+
public void createChangeSet() throws Exception {
31+
ArrayList<String> lines = new ArrayList<String>();
32+
lines.add("commit " + SHA1);
33+
lines.add("tree 66236cf9a1ac0c589172b450ed01f019a5697c49");
34+
lines.add("parent e74a24e995305bd67a180f0ebc57927e2b8783ce");
35+
lines.add("author Author Name <author.name@nospam.com> 1363879004 +0100");
36+
lines.add("committer Committer Name <committer.name@nospam.com> 1364199539 -0400");
37+
lines.add("");
38+
lines.add(" Committer and author are different in this commit.");
39+
lines.add("");
40+
changeSet = new GitChangeSet(lines, true);
41+
}
42+
43+
private static Object[] genActualAndExpected(String keyword) {
44+
int issueNumber = RANDOM.nextInt(1000000);
45+
final String innerText = keyword + " #" + issueNumber;
46+
final String startHREF = "<a href='" + GITHUB_URL + "/issues/" + issueNumber + "/find'>";
47+
final String endHREF = "</a>";
48+
final String annotatedText = startHREF + innerText + endHREF;
49+
return new Object[]{
50+
// Input text to the annotate method
51+
format("An issue %s link", innerText),
52+
// Expected result from the annotate method
53+
format("An issue %s link", annotatedText)
54+
};
55+
}
56+
57+
@DataProvider
58+
public static Object[][] annotations() {
59+
return new Object[][]{
60+
genActualAndExpected("Closes"),
61+
genActualAndExpected("Close"),
62+
genActualAndExpected("closes"),
63+
genActualAndExpected("close")
64+
};
65+
}
66+
67+
@Test
68+
@UseDataProvider("annotations")
69+
public void inputIsExpected(String input, String expected) throws Exception {
70+
assertThat(format("For input '%s'", input),
71+
annotate(input, null),
72+
is(expected));
73+
}
1174

1275
@Test
13-
public final void testAnnotateStringMarkupText() {
14-
assertAnnotatedTextEquals("An issue Closes #1 link",
15-
"An issue <a href='" + GITHUB_URL
16-
+ "issues/1/find'>Closes #1</a> link");
17-
assertAnnotatedTextEquals("An issue Close #1 link",
18-
"An issue <a href='" + GITHUB_URL
19-
+ "issues/1/find'>Close #1</a> link");
20-
assertAnnotatedTextEquals("An issue closes #123 link",
21-
"An issue <a href='" + GITHUB_URL
22-
+ "issues/123/find'>closes #123</a> link");
23-
assertAnnotatedTextEquals("An issue close #9876 link",
24-
"An issue <a href='" + GITHUB_URL
25-
+ "issues/9876/find'>close #9876</a> link");
76+
@UseDataProvider("annotations")
77+
public void inputIsExpectedWithChangeSet(String input, String expected) throws Exception {
78+
assertThat(format("For changeset input '%s'", input),
79+
annotate(input, changeSet),
80+
is(expected + expectedChangeSetAnnotation));
2681
}
2782

28-
private void assertAnnotatedTextEquals(final String originalText,
29-
final String expectedAnnotatedText) {
83+
private String annotate(final String originalText, GitChangeSet changeSet) {
3084
MarkupText markupText = new MarkupText(originalText);
3185

3286
GithubLinkAnnotator annotator = new GithubLinkAnnotator();
33-
annotator.annotate(new GithubUrl(GITHUB_URL), markupText, null);
87+
annotator.annotate(new GithubUrl(GITHUB_URL), markupText, changeSet);
3488

35-
assertEquals(expectedAnnotatedText, markupText.toString());
89+
return markupText.toString(true);
3690
}
3791
}

0 commit comments

Comments
 (0)
0