Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42 #35 #28 set/get master coverage by repo URL instead of PR URL #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
</issueManagement>

<properties>
<jenkins.version>1.580.1</jenkins.version>
<!--<jenkins.version>2.46.3</jenkins.version>-->
<java.level>6</java.level>
<!--<java.level>7</java.level>-->
<!--<jenkins.version>1.580.1</jenkins.version>-->
<jenkins.version>2.46.3</jenkins.version>
<!--<java.level>6</java.level>-->
<java.level>7</java.level>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@SuppressWarnings("WeakerAccess")
public class GitUtils {

public static final Pattern HTTP_GITHUB_REPO_URL = Pattern.compile("^(http[s]?://[^/]*/[^/]*/[^/]*).*");

public static final Pattern HTTP_GITHUB_USER_REPO_PATTERN = Pattern.compile("^(http[s]?://[^/]*)/([^/]*/[^/]*).*");
public static final Pattern SSH_GITHUB_USER_REPO_PATTERN = Pattern.compile("^.+:(.+)");

Expand All @@ -40,6 +42,33 @@ public static String getRepoName(String gitRepoUrl) {
return userRepo[1];
}

/**
* Extract repo URL part form Git URL. For example <code>https://github.com/terma/test/pull/1</code>
* should be converted to <code>https://github.com/terma/test</code>
*
* @param gitUrl - any type of Git URL
* @return repo URL exclude branches or pull request parts
*/
public static String getRepoUrl(String gitUrl) {
String repoUrl = null;

if (gitUrl != null) {
if (gitUrl.startsWith("git@")) {
repoUrl = gitUrl;
} else {
Matcher m = HTTP_GITHUB_REPO_URL.matcher(gitUrl);
if (m.matches()) repoUrl = m.group(1);
}
}

if (repoUrl == null) {
throw new IllegalArgumentException(String.format("Invalid Git Hub repository URL: %s", gitUrl));
}

if (repoUrl.endsWith(".git")) repoUrl = repoUrl.substring(0, repoUrl.length() - ".git".length());
return repoUrl;
}

/**
* Extract user name and repo name from Git URL.
* For example: <code>https://github.com/terma/jenkins-github-coverage-updater.git</code>
Expand All @@ -62,7 +91,7 @@ public static String getUserRepo(final String gitRepoUrl) {
}

if (userRepo == null) {
throw new IllegalStateException(String.format("Invalid Git Hub repository URL: %s", gitRepoUrl));
throw new IllegalArgumentException(String.format("Invalid Git Hub repository URL: %s", gitRepoUrl));
}

if (userRepo.endsWith(".git")) userRepo = userRepo.substring(0, userRepo.length() - ".git".length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ public static int getPrId(
return id;
}

/**
* @param scmVars - scmVars
* @param build - build
* @param listener - listener
* @return - Git URL always Repo URL even if CHANGE_URL passed which in general PR URL
* @throws IOException inherited
* @throws InterruptedException inherited
*/
public static String getGitUrl(final Map<String, String> scmVars, final Run build, final TaskListener listener) throws IOException, InterruptedException {
Map<String, String> envVars = build.getEnvironment(listener);
final String gitUrl = envVars.get(GIT_URL_PROPERTY);
final String changeUrl = envVars.get(CHANGE_URL_PROPERTY);
if (gitUrl != null) return gitUrl;
else if (changeUrl != null) return changeUrl;
else if (changeUrl != null) return GitUtils.getRepoUrl(changeUrl); // change URL is full path to PR, so we normalize it to repo URL before return
else if (scmVars != null && scmVars.containsKey(GIT_URL_PROPERTY)) return scmVars.get(GIT_URL_PROPERTY);
else throw new UnsupportedOperationException("Can't find " + GIT_URL_PROPERTY
+ " or " + CHANGE_URL_PROPERTY + " in envs: " + envVars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ public void getUserRepo() {
GitUtils.getUserRepo("[email protected]:terma/jenkins-github-coverage-updater"));
}

@Test
public void getRepoUrl() {
try {
GitUtils.getRepoUrl(null);
Assert.fail();
} catch (IllegalArgumentException e) {
// all good
}

Assert.assertEquals(
"https://github.com/terma/jenkins-github-coverage-updater",
GitUtils.getRepoUrl("https://github.com/terma/jenkins-github-coverage-updater"));

Assert.assertEquals("https://github.com/terma/jenkins-github-coverage-updater",
GitUtils.getRepoUrl("https://github.com/terma/jenkins-github-coverage-updater.git"));

Assert.assertEquals("[email protected]:terma/jenkins-github-coverage-updater",
GitUtils.getRepoUrl("[email protected]:terma/jenkins-github-coverage-updater.git"));

Assert.assertEquals("[email protected]:terma/jenkins-github-coverage-updater",
GitUtils.getRepoUrl("[email protected]:terma/jenkins-github-coverage-updater"));

Assert.assertEquals("https://github.com/terma/test",
GitUtils.getRepoUrl("https://github.com/terma/test/pull/1"));

Assert.assertEquals("http://github.com/terma/test",
GitUtils.getRepoUrl("http://github.com/terma/test/pull/1"));

Assert.assertEquals("https://github.com/terma/test",
GitUtils.getRepoUrl("https://github.com/terma/test/tree/branch"));
}

@Test
public void getRepoName() {
Assert.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void throwExceptionWhenExtractCoverageFromJacocoAndNoLineTag() throws IOE
Assert.fail("Where is my exception?");
} catch (Exception e) {
String messageWithoutAbsolutePath = e.getMessage().replace(filePath, "FILE_PATH");
Assert.assertEquals(
assertStringEqualsIgnoreSlashR(
"Strange Jacoco report!\n" +
"File path: FILE_PATH\n" +
"Can't extract float value by XPath: /report/counter[@type='LINE']/@missed\n" +
Expand All @@ -73,7 +73,7 @@ public void throwExceptionWhenExtractCoverageFromJacocoAndMissedNotNumber() thro
Assert.fail("Where is my exception?");
} catch (Exception e) {
String messageWithoutAbsolutePath = e.getMessage().replace(filePath, "FILE_PATH");
Assert.assertEquals(
assertStringEqualsIgnoreSlashR(
"Strange Jacoco report!\n" +
"File path: FILE_PATH\n" +
"Can't extract float value by XPath: /report/counter[@type='LINE']/@missed\n" +
Expand All @@ -97,7 +97,7 @@ public void throwExceptionWhenExtractCoverageFromJacocoAndCoveredNotNumber() thr
Assert.fail("Where is my exception?");
} catch (Exception e) {
String messageWithoutAbsolutePath = e.getMessage().replace(filePath, "FILE_PATH");
Assert.assertEquals(
assertStringEqualsIgnoreSlashR(
"Strange Jacoco report!\n" +
"File path: FILE_PATH\n" +
"Can't extract float value by XPath: /report/counter[@type='LINE']/@covered\n" +
Expand All @@ -121,4 +121,8 @@ public void throwExceptionWhenExtractCoverageFromJacocoAndNoFile() throws IOExce
}
}

private static void assertStringEqualsIgnoreSlashR(String a, String b) {
Assert.assertEquals(a.replaceAll("\\r", ""), b.replaceAll("\\r", ""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class PrIdAndUrlUtilsTest {
private static final int PR_ID_INT = 12;
private static final int CHANGE_ID_INT = 13;
private static final int SCM_ENVS_PR_ID_INT = 14;
private static final String CHANGE_URL = "http://test.github.com/terma/test-repo/pr/123";

private Run build = mock(Run.class);
private EnvVars envVars = mock(EnvVars.class);
Expand Down Expand Up @@ -120,9 +121,9 @@ public void getGitUrlGitUrlHasPriority() throws IOException, InterruptedExceptio
@Test
public void getGitUrlIfGitUrlsNullChangeUrlIsUsed() throws IOException, InterruptedException {
when(envVars.get(PrIdAndUrlUtils.GIT_URL_PROPERTY)).thenReturn(null);
when(envVars.get(PrIdAndUrlUtils.CHANGE_URL_PROPERTY)).thenReturn(CHANGE_ID);
when(envVars.get(PrIdAndUrlUtils.CHANGE_URL_PROPERTY)).thenReturn(CHANGE_URL);

Assert.assertEquals(CHANGE_ID, PrIdAndUrlUtils.getGitUrl(null, build, listener));
Assert.assertEquals("http://test.github.com/terma/test-repo", PrIdAndUrlUtils.getGitUrl(null, build, listener));
}

@Test
Expand Down