Search in sources :

Example 1 with GitHubSCMSource

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMSource in project blueocean-plugin by jenkinsci.

the class GithubScmContentProvider method saveContent.

@SuppressWarnings("unchecked")
private Object saveContent(@Nonnull GithubScmSaveFileRequest githubRequest, @Nonnull Item item) {
    String apiUrl = GithubScm.DEFAULT_API_URI;
    String owner = null;
    String repo = null;
    String accessToken = null;
    String credentialId = null;
    if (item instanceof OrganizationFolder) {
        List<SCMNavigator> navigators = ((OrganizationFolder) item).getSCMNavigators();
        if (!navigators.isEmpty() && navigators.get(0) instanceof GitHubSCMNavigator) {
            GitHubSCMNavigator navigator = (GitHubSCMNavigator) navigators.get(0);
            if (navigator.getApiUri() != null) {
                apiUrl = navigator.getApiUri();
            }
            credentialId = navigator.getScanCredentialsId();
            owner = navigator.getRepoOwner();
        }
    } else if (item instanceof MultiBranchProject) {
        List<SCMSource> sources = ((MultiBranchProject) item).getSCMSources();
        if (!sources.isEmpty() && sources.get(0) instanceof GitHubSCMSource) {
            GitHubSCMSource source = (GitHubSCMSource) sources.get(0);
            if (source.getApiUri() != null) {
                apiUrl = source.getApiUri();
            }
            credentialId = source.getScanCredentialsId();
            owner = owner(source);
            repo = repo(source);
        }
    }
    if (credentialId != null) {
        StandardCredentials credentials = Connector.lookupScanCredentials((SCMSourceOwner) item, apiUrl, credentialId);
        if (credentials instanceof StandardUsernamePasswordCredentials) {
            accessToken = ((StandardUsernamePasswordCredentials) credentials).getPassword().getPlainText();
        } else {
            throw new ServiceException.BadRequestExpception("accessToken not found in pipeline: " + item.getFullName());
        }
    }
    return githubRequest.save(apiUrl, owner, repo, accessToken);
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) SCMNavigator(jenkins.scm.api.SCMNavigator) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) OrganizationFolder(jenkins.branch.OrganizationFolder) MultiBranchProject(jenkins.branch.MultiBranchProject) ArrayList(java.util.ArrayList) List(java.util.List) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 2 with GitHubSCMSource

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMSource in project blueocean-plugin by jenkinsci.

the class GithubScm method getRepository.

/**
 * @param jobName the job name
 * @param apiUrl github api url
 * @return GHRepository used by the job
 */
@GET
@WebMethod(name = "repository")
@TreeResponse
public GithubRepository getRepository(@QueryParameter String jobName, @QueryParameter String apiUrl) {
    Item item = Jenkins.get().getItem(jobName);
    if (item == null) {
        throw new ServiceException.NotFoundException(String.format("Job %s not found", jobName));
    }
    GitHubSCMSource gitHubSCMSource = ((GitHubSCMSource) ((WorkflowMultiBranchProject) item).getSCMSource("blueocean"));
    if (gitHubSCMSource == null) {
        throw new ServiceException.NotFoundException(String.format("GitHubSCMSource for Job %s not found", jobName));
    }
    String repoOwner = gitHubSCMSource.getRepoOwner();
    String repoName = gitHubSCMSource.getRepository();
    StandardUsernamePasswordCredentials credential = getCredential();
    String accessToken = credential.getPassword().getPlainText();
    try {
        String url = String.format("%s/repos/%s/%s", apiUrl, repoOwner, repoName);
        GHRepository ghRepository = HttpRequest.get(url).withAuthorizationToken(accessToken).to(GHRepository.class);
        return new GithubRepository(ghRepository, credential, this);
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) Item(hudson.model.Item) GHRepository(org.kohsuke.github.GHRepository) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) IOException(java.io.IOException) WebMethod(org.kohsuke.stapler.WebMethod) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 3 with GitHubSCMSource

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMSource in project blueocean-plugin by jenkinsci.

the class GithubPipelineCreateRequestTest method createPipeline.

@Test
public void createPipeline() throws UnirestException {
    PowerMockito.mockStatic(GitHubWebHook.class);
    GitHubWebHook gitHubWebHookMock = Mockito.spy(GitHubWebHook.class);
    PowerMockito.when(GitHubWebHook.get()).thenReturn(gitHubWebHookMock);
    PowerMockito.when(GitHubWebHook.getJenkinsInstance()).thenReturn(this.j.jenkins);
    String credentialId = createGithubCredential(user);
    Map r = new PipelineBaseTest.RequestBuilder(baseUrl).status(201).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).crumb(this.crumb).post("/organizations/jenkins/pipelines/").data(MapsHelper.of("name", "pipeline1", "$class", "io.jenkins.blueocean.blueocean_github_pipeline.GithubPipelineCreateRequest", "scmConfig", MapsHelper.of("id", GithubScm.ID, "uri", githubApiUrl, "credentialId", credentialId, "config", MapsHelper.of("repoOwner", "cloudbeers", "repository", "PR-demo")))).build(Map.class);
    assertNotNull(r);
    assertEquals("pipeline1", r.get("name"));
    MultiBranchProject mbp = (MultiBranchProject) j.getInstance().getItem("pipeline1");
    GitHubSCMSource source = (GitHubSCMSource) mbp.getSCMSources().get(0);
    List<SCMSourceTrait> traits = source.getTraits();
    Assert.assertNotNull(SCMTrait.find(traits, CleanAfterCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, CleanBeforeCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, LocalBranchTrait.class));
    BranchDiscoveryTrait branchDiscoveryTrait = SCMTrait.find(traits, BranchDiscoveryTrait.class);
    Assert.assertNotNull(branchDiscoveryTrait);
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranch());
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranchesWithPR());
    ForkPullRequestDiscoveryTrait forkPullRequestDiscoveryTrait = SCMTrait.find(traits, ForkPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(forkPullRequestDiscoveryTrait);
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getTrust() instanceof ForkPullRequestDiscoveryTrait.TrustPermission);
    Assert.assertEquals(1, forkPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));
    OriginPullRequestDiscoveryTrait originPullRequestDiscoveryTrait = SCMTrait.find(traits, OriginPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(originPullRequestDiscoveryTrait);
    Assert.assertEquals(1, originPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(originPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));
    Mockito.verify(gitHubWebHookMock, Mockito.times(1)).registerHookFor(mbp);
}
Also used : GitHubWebHook(com.cloudbees.jenkins.GitHubWebHook) OriginPullRequestDiscoveryTrait(org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait) CleanBeforeCheckoutTrait(jenkins.plugins.git.traits.CleanBeforeCheckoutTrait) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) ForkPullRequestDiscoveryTrait(org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait) CleanAfterCheckoutTrait(jenkins.plugins.git.traits.CleanAfterCheckoutTrait) SCMSourceTrait(jenkins.scm.api.trait.SCMSourceTrait) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) MultiBranchProject(jenkins.branch.MultiBranchProject) Map(java.util.Map) LocalBranchTrait(jenkins.plugins.git.traits.LocalBranchTrait) BranchDiscoveryTrait(org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 4 with GitHubSCMSource

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMSource in project blueocean-plugin by jenkinsci.

the class GithubEnterpriseScmContentProviderTest method testScmSourceProperties.

private void testScmSourceProperties(String mockedApiUrl) throws Exception {
    String credentialId = createGithubEnterpriseCredential();
    MultiBranchProject mbp = mockMbp(credentialId, user, GithubEnterpriseScm.DOMAIN_NAME);
    // ensure the enterprise provider works with enterprise org folder
    ScmContentProvider provider = new GithubEnterpriseScmContentProvider();
    assertTrue("github enterprise provider should support github enterprise org folder", provider.support(mbp));
    assertEquals(GithubEnterpriseScm.ID, provider.getScmId());
    assertEquals(githubApiUrl, provider.getApiUrl(mbp));
    // ensure the enterprise provider doesn't support cloud org folder
    credentialId = createGithubCredential(user);
    mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME);
    // unfortunately overriding the GitHub apiUrl for WireMock returns a "localhost" URL here, so we mock the call
    when(((GitHubSCMSource) mbp.getSCMSources().get(0)).getApiUri()).thenReturn(mockedApiUrl);
    assertFalse("github enterprise provider should not support github org folder", provider.support(mbp));
}
Also used : ScmContentProvider(io.jenkins.blueocean.rest.impl.pipeline.ScmContentProvider) MultiBranchProject(jenkins.branch.MultiBranchProject) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)

Example 5 with GitHubSCMSource

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMSource in project blueocean-plugin by jenkinsci.

the class GithubMockBase method mockMbp.

protected MultiBranchProject mockMbp(String credentialId, User user, String credentialDomainName) {
    MultiBranchProject mbp = mock(MultiBranchProject.class);
    when(mbp.getName()).thenReturn("PR-demo");
    when(mbp.getParent()).thenReturn(j.jenkins);
    GitHubSCMSource scmSource = mock(GitHubSCMSource.class);
    when(scmSource.getApiUri()).thenReturn(githubApiUrl);
    when(scmSource.getCredentialsId()).thenReturn(credentialId);
    when(scmSource.getRepoOwner()).thenReturn("cloudbeers");
    when(scmSource.getRepository()).thenReturn("PR-demo");
    when(mbp.getSCMSources()).thenReturn(Collections.singletonList(scmSource));
    BlueOceanCredentialsProvider.FolderPropertyImpl folderProperty = mock(BlueOceanCredentialsProvider.FolderPropertyImpl.class);
    DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> mbpProperties = new DescribableList<>(mbp);
    mbpProperties.add(new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(githubApiUrl)));
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(credentialDomainName);
    when(folderProperty.getDomain()).thenReturn(domain);
    when(mbp.getProperties()).thenReturn(mbpProperties);
    return mbp;
}
Also used : BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) DescribableList(hudson.util.DescribableList) MultiBranchProject(jenkins.branch.MultiBranchProject) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) AbstractFolderPropertyDescriptor(com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor) Domain(com.cloudbees.plugins.credentials.domains.Domain) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)

Aggregations

GitHubSCMSource (org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)7 MultiBranchProject (jenkins.branch.MultiBranchProject)6 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)2 DescribableList (hudson.util.DescribableList)2 ScmContentProvider (io.jenkins.blueocean.rest.impl.pipeline.ScmContentProvider)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)2 Test (org.junit.Test)2 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)1 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)1 AbstractFolderPropertyDescriptor (com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor)1 GitHubWebHook (com.cloudbees.jenkins.GitHubWebHook)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 Item (hudson.model.Item)1 ItemGroup (hudson.model.ItemGroup)1 Job (hudson.model.Job)1 Run (hudson.model.Run)1 ChangeLogSet (hudson.scm.ChangeLogSet)1