-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-36445] Show first 7 characters of SHA1 in change log hyperli…
…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
1 parent
e9caf00
commit ca75bde
Showing
2 changed files
with
80 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 74 additions & 20 deletions
94
src/test/java/com/coravy/hudson/plugins/github/GithubLinkAnnotatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |