Skip to content

Commit c20a8ad

Browse files
committed
[JENKINS-10405] Merge branch 'pull-24'
This pull request relaxes the rule to allow the github URL that does not end with '.git'
2 parents 94f9a56 + b367c61 commit c20a8ad

File tree

3 files changed

+74
-54
lines changed

3 files changed

+74
-54
lines changed

src/main/java/com/cloudbees/jenkins/GitHubRepositoryName.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,27 @@
2626
public class GitHubRepositoryName {
2727

2828
private static final Pattern[] URL_PATTERNS = {
29-
Pattern.compile("git@(.+):([^/]+)/([^/]+).git"),
30-
Pattern.compile("https://[^/]+@([^/]+)/([^/]+)/([^/]+).git"),
31-
Pattern.compile("https://([^/]+)/([^/]+)/([^/]+).git"),
32-
Pattern.compile("git://([^/]+)/([^/]+)/([^/]+).git"),
33-
Pattern.compile("ssh://git@([^/]+)/([^/]+)/([^/]+).git")
29+
/**
30+
* The first set of patterns extract the host, owner and repository names
31+
* from URLs that include a '.git' suffix, removing the suffix from the
32+
* repository name.
33+
*/
34+
Pattern.compile("git@(.+):([^/]+)/([^/]+)\\.git"),
35+
Pattern.compile("https?://[^/]+@([^/]+)/([^/]+)/([^/]+)\\.git"),
36+
Pattern.compile("https?://([^/]+)/([^/]+)/([^/]+)\\.git"),
37+
Pattern.compile("git://([^/]+)/([^/]+)/([^/]+)\\.git"),
38+
Pattern.compile("ssh://git@([^/]+)/([^/]+)/([^/]+)\\.git"),
39+
/**
40+
* The second set of patterns extract the host, owner and repository names
41+
* from all other URLs. Note that these patterns must be processed *after*
42+
* the first set, to avoid any '.git' suffix that may be present being included
43+
* in the repository name.
44+
*/
45+
Pattern.compile("git@(.+):([^/]+)/([^/]+)"),
46+
Pattern.compile("https?://[^/]+@([^/]+)/([^/]+)/([^/]+)"),
47+
Pattern.compile("https?://([^/]+)/([^/]+)/([^/]+)"),
48+
Pattern.compile("git://([^/]+)/([^/]+)/([^/]+)"),
49+
Pattern.compile("ssh://git@([^/]+)/([^/]+)/([^/]+)")
3450
};
3551

3652
/**

src/main/java/com/cloudbees/jenkins/GitHubWebHook.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ public void doIndex(StaplerRequest req) {
156156

157157
public void processGitHubPayload(String payload, Class<? extends Trigger<?>> triggerClass) {
158158
JSONObject o = JSONObject.fromObject(payload);
159-
JSONObject repository = o.getJSONObject("repository");
160-
String repoUrl = repository.getString("url"); // something like 'https://github.com/kohsuke/foo'
161-
String repoName = repository.getString("name"); // 'foo' portion of the above URL
162-
String ownerName = repository.getJSONObject("owner").getString("name"); // 'kohsuke' portion of the above URL
159+
String repoUrl = o.getJSONObject("repository").getString("url"); // something like 'https://github.com/kohsuke/foo'
163160
String pusherName = o.getJSONObject("pusher").getString("name");
164161

165162
LOGGER.info("Received POST for "+repoUrl);
@@ -172,7 +169,7 @@ public void processGitHubPayload(String payload, Class<? extends Trigger<?>> tri
172169
Authentication old = SecurityContextHolder.getContext().getAuthentication();
173170
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
174171
try {
175-
GitHubRepositoryName changedRepository = new GitHubRepositoryName(matcher.group(1), ownerName, repoName);
172+
GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl);
176173
for (AbstractProject<?,?> job : Hudson.getInstance().getAllItems(AbstractProject.class)) {
177174
GitHubTrigger trigger = (GitHubTrigger) job.getTrigger(triggerClass);
178175
if (trigger!=null) {

src/test/java/com/coravy/hudson/plugins/github/GitHubRepositoryNameTest.java

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,75 +12,82 @@
1212
*/
1313
public class GitHubRepositoryNameTest {
1414

15+
private void testURL(String URL, String host, String owner, String repository)
16+
{
17+
GitHubRepositoryName repo = GitHubRepositoryName.create(URL);
18+
assertNotNull(repo);
19+
assertEquals(host, repo.host);
20+
assertEquals(owner, repo.userName);
21+
assertEquals(repository, repo.repositoryName);
22+
}
23+
1524
@Test
1625
public void gitAtUrlGitHub() {
17-
GitHubRepositoryName repo = GitHubRepositoryName
18-
.create("git@github.com:jenkinsci/jenkins.git");
19-
assertNotNull(repo);
20-
assertEquals("jenkinsci", repo.userName);
21-
assertEquals("jenkins", repo.repositoryName);
22-
assertEquals("github.com", repo.host);
26+
testURL("git@github.com:jenkinsci/jenkins.git", "github.com", "jenkinsci", "jenkins");
2327
}
2428

2529
@Test
2630
public void gitAtUrlOtherHost() {
27-
GitHubRepositoryName repo = GitHubRepositoryName
28-
.create("git@gh.company.com:jenkinsci/jenkins.git");
29-
assertNotNull(repo);
30-
assertEquals("jenkinsci", repo.userName);
31-
assertEquals("jenkins", repo.repositoryName);
32-
assertEquals("gh.company.com", repo.host);
31+
testURL("git@gh.company.com:jenkinsci/jenkins.git", "gh.company.com", "jenkinsci", "jenkins");
3332
}
3433

3534
@Test
3635
public void gitColonUrlGitHub() {
37-
GitHubRepositoryName repo = GitHubRepositoryName
38-
.create("git://github.com/jenkinsci/jenkins.git");
39-
assertNotNull(repo);
40-
assertEquals("jenkinsci", repo.userName);
41-
assertEquals("jenkins", repo.repositoryName);
42-
assertEquals("github.com", repo.host);
36+
testURL("git://github.com/jenkinsci/jenkins.git", "github.com", "jenkinsci", "jenkins");
4337
}
4438

4539
@Test
4640
public void gitColonUrlOtherHost() {
47-
GitHubRepositoryName repo = GitHubRepositoryName
48-
.create("git://company.net/jenkinsci/jenkins.git");
49-
assertNotNull(repo);
50-
assertEquals("jenkinsci", repo.userName);
51-
assertEquals("jenkins", repo.repositoryName);
52-
assertEquals("company.net", repo.host);
41+
testURL("git://company.net/jenkinsci/jenkins.git", "company.net", "jenkinsci", "jenkins");
5342
}
5443

5544
@Test
5645
public void httpsUrlGitHub() {
57-
GitHubRepositoryName repo = GitHubRepositoryName
58-
.create("https://user@github.com/jenkinsci/jenkins.git");
59-
assertNotNull(repo);
60-
assertEquals("jenkinsci", repo.userName);
61-
assertEquals("jenkins", repo.repositoryName);
62-
assertEquals("github.com", repo.host);
46+
testURL("https://user@github.com/jenkinsci/jenkins.git", "github.com", "jenkinsci", "jenkins");
6347
}
6448

6549
@Test
6650
public void httpsUrlGitHubWithoutUser() {
67-
//this is valid for anonymous usage
68-
GitHubRepositoryName repo = GitHubRepositoryName
69-
.create("https://github.com/jenkinsci/jenkins.git");
70-
assertNotNull(repo);
71-
assertEquals("jenkinsci", repo.userName);
72-
assertEquals("jenkins", repo.repositoryName);
73-
assertEquals("github.com", repo.host);
51+
testURL("https://github.com/jenkinsci/jenkins.git", "github.com", "jenkinsci", "jenkins");
7452
}
7553

76-
7754
@Test
7855
public void httpsUrlOtherHost() {
79-
GitHubRepositoryName repo = GitHubRepositoryName
80-
.create("https://employee@gh.company.com/jenkinsci/jenkins.git");
81-
assertNotNull(repo);
82-
assertEquals("jenkinsci", repo.userName);
83-
assertEquals("jenkins", repo.repositoryName);
84-
assertEquals("gh.company.com", repo.host);
56+
testURL("https://employee@gh.company.com/jenkinsci/jenkins.git", "gh.company.com", "jenkinsci", "jenkins");
57+
}
58+
59+
@Test
60+
public void gitAtUrlGitHubNoSuffix() {
61+
testURL("git@github.com:jenkinsci/jenkins", "github.com", "jenkinsci", "jenkins");
62+
}
63+
64+
@Test
65+
public void gitAtUrlOtherHostNoSuffix() {
66+
testURL("git@gh.company.com:jenkinsci/jenkins", "gh.company.com", "jenkinsci", "jenkins");
67+
}
68+
69+
@Test
70+
public void gitColonUrlGitHubNoSuffix() {
71+
testURL("git://github.com/jenkinsci/jenkins", "github.com", "jenkinsci", "jenkins");
72+
}
73+
74+
@Test
75+
public void gitColonUrlOtherHostNoSuffix() {
76+
testURL("git://company.net/jenkinsci/jenkins", "company.net", "jenkinsci", "jenkins");
77+
}
78+
79+
@Test
80+
public void httpsUrlGitHubNoSuffix() {
81+
testURL("https://user@github.com/jenkinsci/jenkins", "github.com", "jenkinsci", "jenkins");
82+
}
83+
84+
@Test
85+
public void httpsUrlGitHubWithoutUserNoSuffix() {
86+
testURL("https://github.com/jenkinsci/jenkins", "github.com", "jenkinsci", "jenkins");
87+
}
88+
89+
@Test
90+
public void httpsUrlOtherHostNoSuffix() {
91+
testURL("https://employee@gh.company.com/jenkinsci/jenkins", "gh.company.com", "jenkinsci", "jenkins");
8592
}
8693
}

0 commit comments

Comments
 (0)