Search in sources :

Example 1 with MultiBranchProject

use of jenkins.branch.MultiBranchProject 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 MultiBranchProject

use of jenkins.branch.MultiBranchProject in project blueocean-plugin by jenkinsci.

the class GitReadSaveService method makeSaveRequest.

static GitReadSaveRequest makeSaveRequest(Item item, String branch, String commitMessage, String sourceBranch, String filePath, byte[] contents) {
    String defaultBranch = "master";
    GitSCMSource gitSource = null;
    if (item instanceof MultiBranchProject<?, ?>) {
        MultiBranchProject<?, ?> mbp = (MultiBranchProject<?, ?>) item;
        for (SCMSource s : mbp.getSCMSources()) {
            if (s instanceof GitSCMSource) {
                gitSource = (GitSCMSource) s;
            }
        }
    }
    return new GitBareRepoReadSaveRequest(gitSource, StringUtils.defaultIfEmpty(branch, defaultBranch), commitMessage, StringUtils.defaultIfEmpty(sourceBranch, defaultBranch), filePath, contents);
}
Also used : MultiBranchProject(jenkins.branch.MultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource)

Example 3 with MultiBranchProject

use of jenkins.branch.MultiBranchProject in project blueocean-plugin by jenkinsci.

the class BitbucketServerScmContentProviderTest method unauthorizedAccessToContentShouldFail.

@Test
public void unauthorizedAccessToContentShouldFail() throws UnirestException, IOException {
    User alice = User.get("alice");
    alice.setFullName("Alice Cooper");
    alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
    String aliceCredentialId = createCredential(BitbucketServerScm.ID, alice);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(aliceCredentialId, alice);
    try {
        // Bob trying to access content but his credential is not setup so should fail
        new BitbucketServerScmContentProvider().getContent(staplerRequest, mbp);
    } catch (ServiceException.PreconditionRequired e) {
        assertEquals("Can't access content from Bitbucket: no credential found", e.getMessage());
        return;
    }
    fail("Should have failed with PreConditionException");
}
Also used : User(hudson.model.User) ServiceException(io.jenkins.blueocean.commons.ServiceException) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) MultiBranchProject(jenkins.branch.MultiBranchProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with MultiBranchProject

use of jenkins.branch.MultiBranchProject in project blueocean-plugin by jenkinsci.

the class BitbucketServerScmContentProviderTest method updateContent.

@Test
public void updateContent() throws UnirestException, IOException {
    String credentialId = createCredential(BitbucketServerScm.ID);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(credentialId);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("another commit").owner("TESTP").path("Jenkinsfile").repo("pipeline-demo-test").commitId("0bae0ddbed2e897d3b44abc3aca9ba26e2f61710").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"another commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"pipeline-demo-test\",\n" + "    \"commitId\" : \"0bae0ddbed2e897d3b44abc3aca9ba26e2f61710\",\n" + "    \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    ScmFile<GitContent> respContent = (ScmFile<GitContent>) new BitbucketServerScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("Jenkinsfile", respContent.getContent().getName());
    assertEquals("e587b620844b1b230783976f00cfb8383488aeca", respContent.getContent().getCommitId());
    assertEquals("pipeline-demo-test", respContent.getContent().getRepo());
    assertEquals("TESTP", respContent.getContent().getOwner());
    assertEquals("master", respContent.getContent().getBranch());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with MultiBranchProject

use of jenkins.branch.MultiBranchProject in project blueocean-plugin by jenkinsci.

the class BitbucketServerScmContentProviderTest method newContent.

@Test
public void newContent() throws UnirestException, IOException {
    String credentialId = createCredential(BitbucketServerScm.ID);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(credentialId);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"new commit\",\n" + "    \"path\" : \"README.md\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"pipeline-demo-test\",\n" + "    \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    ScmFile<GitContent> respContent = (ScmFile<GitContent>) new BitbucketServerScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("README.md", respContent.getContent().getName());
    assertEquals("a77840d4108db2befe6c616723eb3f4485af5d24", respContent.getContent().getCommitId());
    assertEquals("pipeline-demo-test", respContent.getContent().getRepo());
    assertEquals("TESTP", respContent.getContent().getOwner());
    assertEquals("master", respContent.getContent().getBranch());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MultiBranchProject (jenkins.branch.MultiBranchProject)52 Test (org.junit.Test)36 StaplerRequest (org.kohsuke.stapler.StaplerRequest)28 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 JSONObject (net.sf.json.JSONObject)16 ServiceException (io.jenkins.blueocean.commons.ServiceException)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 User (hudson.model.User)13 Mailer (hudson.tasks.Mailer)12 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 Job (hudson.model.Job)5 DescribableList (hudson.util.DescribableList)5 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 Map (java.util.Map)5 GitSCMSource (jenkins.plugins.git.GitSCMSource)5 GitHubSCMSource (org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)5 BitbucketSCMSource (com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource)4 Item (hudson.model.Item)4