Skip to content

Commit

Permalink
Merge pull request #170 from rsandell/JENKINS-48035
Browse files Browse the repository at this point in the history
[JENKINS-48035] Add a GitHubRepositoryNameContributor that recognises…
  • Loading branch information
stephenc committed Dec 18, 2017
2 parents 267e41c + 99d72b0 commit 3bf8714
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -23,7 +23,7 @@
<properties>
<jenkins.version>1.642.3</jenkins.version>
<workflow.version>1.14.2</workflow.version>
<scm-api.version>2.2.0</scm-api.version>
<scm-api.version>2.2.3</scm-api.version>
</properties>

<scm>
Expand Down
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright (c) 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package org.jenkinsci.plugins.github_branch_source;

import com.cloudbees.jenkins.GitHubRepositoryName;
import com.cloudbees.jenkins.GitHubRepositoryNameContributor;
import hudson.Extension;
import hudson.model.Item;
import jenkins.scm.api.SCMSourceOwner;

import java.util.Collection;

/**
* Finds the repository name(s) associated with a {@link SCMSourceOwner}'s {@link GitHubSCMSource}s.
*
* @see GitHubRepositoryNameContributor#parseAssociatedNames(Item)
* @see org.jenkinsci.plugins.github.webhook.WebhookManager#registerFor(Item)
*/
@Extension
public class GitHubSCMSourceRepositoryNameContributor extends GitHubRepositoryNameContributor {

@Override
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
if (item instanceof SCMSourceOwner) {
SCMSourceOwner mp = (SCMSourceOwner) item;
for (Object o : mp.getSCMSources()) {
if (o instanceof GitHubSCMSource) {
GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) o;
result.add(new GitHubRepositoryName(
RepositoryUriResolver.hostnameFromApiUri(gitHubSCMSource.getApiUri()),
gitHubSCMSource.getRepoOwner(),
gitHubSCMSource.getRepository()));

}
}
}
}
}
Expand Up @@ -25,6 +25,8 @@

package org.jenkinsci.plugins.github_branch_source;

import com.cloudbees.jenkins.GitHubRepositoryName;
import com.cloudbees.jenkins.GitHubRepositoryNameContributor;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
Expand All @@ -34,7 +36,9 @@
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.model.Action;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.model.User;
Expand All @@ -45,8 +49,13 @@
import hudson.util.LogTaskListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import jenkins.branch.BranchSource;
import jenkins.branch.MultiBranchProject;
import jenkins.model.Jenkins;
import jenkins.plugins.git.GitSCMSource;
Expand All @@ -67,14 +76,19 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.MockFolder;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
Expand Down Expand Up @@ -146,6 +160,50 @@ public void prepareMockGitHub() throws Exception {
source = new GitHubSCMSource(null, "http://localhost:" + githubApi.port(), GitHubSCMSource.DescriptorImpl.SAME, null, "cloudbeers", "yolo");
}

@Test
@Issue("JENKINS-48035")
public void testGitHubRepositoryNameContributor() throws IOException {
WorkflowMultiBranchProject job = r.createProject(WorkflowMultiBranchProject.class);
job.setSourcesList(Arrays.asList(new BranchSource(source)));
Collection<GitHubRepositoryName> names = GitHubRepositoryNameContributor.parseAssociatedNames(job);
assertThat(names, contains(allOf(
hasProperty("userName", equalTo("cloudbeers")),
hasProperty("repositoryName", equalTo("yolo"))
)));
//And specifically...
names = new ArrayList<>();
ExtensionList.lookup(GitHubRepositoryNameContributor.class).get(GitHubSCMSourceRepositoryNameContributor.class).parseAssociatedNames(job, names);
assertThat(names, contains(allOf(
hasProperty("userName", equalTo("cloudbeers")),
hasProperty("repositoryName", equalTo("yolo"))
)));
}

@Test
@Issue("JENKINS-48035")
public void testGitHubRepositoryNameContributor_When_not_GitHub() throws IOException {
WorkflowMultiBranchProject job = r.createProject(WorkflowMultiBranchProject.class);
job.setSourcesList(Arrays.asList(new BranchSource(new GitSCMSource("file://tmp/something"))));
Collection<GitHubRepositoryName> names = GitHubRepositoryNameContributor.parseAssociatedNames(job);
assertThat(names, Matchers.<GitHubRepositoryName>empty());
//And specifically...
names = new ArrayList<>();
ExtensionList.lookup(GitHubRepositoryNameContributor.class).get(GitHubSCMSourceRepositoryNameContributor.class).parseAssociatedNames(job, names);
assertThat(names, Matchers.<GitHubRepositoryName>empty());
}

@Test
@Issue("JENKINS-48035")
public void testGitHubRepositoryNameContributor_When_not_MultiBranch() throws IOException {
FreeStyleProject job = r.createProject(FreeStyleProject.class);
Collection<GitHubRepositoryName> names = GitHubRepositoryNameContributor.parseAssociatedNames((Item) job);
assertThat(names, Matchers.<GitHubRepositoryName>empty());
//And specifically...
names = new ArrayList<>();
ExtensionList.lookup(GitHubRepositoryNameContributor.class).get(GitHubSCMSourceRepositoryNameContributor.class).parseAssociatedNames((Item) job, names);
assertThat(names, Matchers.<GitHubRepositoryName>empty());
}

@Test
public void fetchSmokes() throws Exception {
SCMHeadObserver.Collector collector = SCMHeadObserver.collect();
Expand Down

0 comments on commit 3bf8714

Please sign in to comment.