Skip to content

Commit

Permalink
[JENKINS-36445] Show first 7 characters of SHA1 in change log hyperli…
Browse files Browse the repository at this point in the history
…nk (#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.
  • Loading branch information
MarkEWaite authored and lanwen committed Jul 11, 2016
1 parent e9caf00 commit ca75bde
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import hudson.scm.ChangeLogAnnotator;
import hudson.scm.ChangeLogSet.Entry;

import static java.lang.String.format;

import java.util.regex.Pattern;

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

if (change instanceof GitChangeSet) {
GitChangeSet cs = (GitChangeSet) change;
text.wrapBy("", " (<a href='" + url.commitId(cs.getId()) + "'>commit: " + cs.getId() + "</a>)");
final String id = cs.getId();
text.wrapBy("", format(" (<a href='%s'>commit: %s</a>)",
url.commitId(id),
id.substring(0, Math.min(id.length(), 7))));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,91 @@
package com.coravy.hudson.plugins.github;

import static org.junit.Assert.assertEquals;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import hudson.MarkupText;

import hudson.plugins.git.GitChangeSet;
import java.util.ArrayList;
import java.util.Random;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@RunWith(DataProviderRunner.class)
public class GithubLinkAnnotatorTest {

private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools/";
private final static String GITHUB_URL = "http://github.com/juretta/iphone-project-tools";
private final static String SHA1 = "badbeef136cd854f4dd6fa40bf94c0c657681dd5";
private final static Random RANDOM = new Random();
private final String expectedChangeSetAnnotation = " ("
+ "<a href='" + GITHUB_URL + "/commit/" + SHA1 + "'>"
+ "commit: " + SHA1.substring(0, 7)
+ "</a>)";
private static GitChangeSet changeSet;

@Before
public void createChangeSet() throws Exception {
ArrayList<String> lines = new ArrayList<String>();
lines.add("commit " + SHA1);
lines.add("tree 66236cf9a1ac0c589172b450ed01f019a5697c49");
lines.add("parent e74a24e995305bd67a180f0ebc57927e2b8783ce");
lines.add("author Author Name <[email protected]> 1363879004 +0100");
lines.add("committer Committer Name <[email protected]> 1364199539 -0400");
lines.add("");
lines.add(" Committer and author are different in this commit.");
lines.add("");
changeSet = new GitChangeSet(lines, true);
}

private static Object[] genActualAndExpected(String keyword) {
int issueNumber = RANDOM.nextInt(1000000);
final String innerText = keyword + " #" + issueNumber;
final String startHREF = "<a href='" + GITHUB_URL + "/issues/" + issueNumber + "/find'>";
final String endHREF = "</a>";
final String annotatedText = startHREF + innerText + endHREF;
return new Object[]{
// Input text to the annotate method
format("An issue %s link", innerText),
// Expected result from the annotate method
format("An issue %s link", annotatedText)
};
}

@DataProvider
public static Object[][] annotations() {
return new Object[][]{
genActualAndExpected("Closes"),
genActualAndExpected("Close"),
genActualAndExpected("closes"),
genActualAndExpected("close")
};
}

@Test
@UseDataProvider("annotations")
public void inputIsExpected(String input, String expected) throws Exception {
assertThat(format("For input '%s'", input),
annotate(input, null),
is(expected));
}

@Test
public final void testAnnotateStringMarkupText() {
assertAnnotatedTextEquals("An issue Closes #1 link",
"An issue <a href='" + GITHUB_URL
+ "issues/1/find'>Closes #1</a> link");
assertAnnotatedTextEquals("An issue Close #1 link",
"An issue <a href='" + GITHUB_URL
+ "issues/1/find'>Close #1</a> link");
assertAnnotatedTextEquals("An issue closes #123 link",
"An issue <a href='" + GITHUB_URL
+ "issues/123/find'>closes #123</a> link");
assertAnnotatedTextEquals("An issue close #9876 link",
"An issue <a href='" + GITHUB_URL
+ "issues/9876/find'>close #9876</a> link");
@UseDataProvider("annotations")
public void inputIsExpectedWithChangeSet(String input, String expected) throws Exception {
assertThat(format("For changeset input '%s'", input),
annotate(input, changeSet),
is(expected + expectedChangeSetAnnotation));
}

private void assertAnnotatedTextEquals(final String originalText,
final String expectedAnnotatedText) {
private String annotate(final String originalText, GitChangeSet changeSet) {
MarkupText markupText = new MarkupText(originalText);

GithubLinkAnnotator annotator = new GithubLinkAnnotator();
annotator.annotate(new GithubUrl(GITHUB_URL), markupText, null);
annotator.annotate(new GithubUrl(GITHUB_URL), markupText, changeSet);

assertEquals(expectedAnnotatedText, markupText.toString());
return markupText.toString(true);
}
}

0 comments on commit ca75bde

Please sign in to comment.