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);
}
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);
}
}
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);
}
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));
}
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;
}
Aggregations