Search in sources :

Example 11 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent 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 12 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class AbstractBitbucketScmContentProvider method getContent.

@Override
protected Object getContent(ScmGetRequest request) {
    BitbucketApi api = BitbucketServerScm.getApi(request.getApiUrl(), this.getScmId(), request.getCredentials());
    BbBranch branch = null;
    String branchName = request.getBranch();
    BbBranch defaultBranch = api.getDefaultBranch(request.getOwner(), request.getRepo());
    if (defaultBranch == null) {
        // empty repo
        throw new ServiceException.NotFoundException(request.getPath() + " not found. This is empty and un-initialized repository");
    }
    if (branchName == null) {
        branch = defaultBranch;
    }
    if (branchName != null) {
        branch = api.getBranch(request.getOwner(), request.getRepo(), branchName);
        // Given branchName create this branch
        if (branch == null) {
            throw new ServiceException.BadRequestException("branch: " + branchName + " not found");
        }
    }
    String content = api.getContent(request.getOwner(), request.getRepo(), request.getPath(), branch.getLatestCommit());
    try {
        final GitContent gitContent = new GitContent.Builder().base64Data(Base64.encodeBase64String(content.getBytes("UTF-8"))).branch(request.getBranch()).size(content.length()).path(request.getPath()).owner(request.getOwner()).repo(request.getRepo()).name(request.getPath()).sha(branch.getLatestCommit()).commitId(branch.getLatestCommit()).build();
        return new ScmFile<GitContent>() {

            @Override
            public GitContent getContent() {
                return gitContent;
            }
        };
    } catch (UnsupportedEncodingException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to base64 encode content: " + e.getMessage(), e);
    }
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) BbBranch(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbBranch) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)

Example 13 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class AbstractBitbucketScmContentProvider method saveContent.

@Override
public Object saveContent(@Nonnull StaplerRequest staplerRequest, @Nonnull Item item) {
    JSONObject body;
    try {
        body = JSONObject.fromObject(IOUtils.toString(staplerRequest.getReader()));
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to read request body");
    }
    BitbucketScmSaveFileRequest scmSaveFileRequest = staplerRequest.bindJSON(BitbucketScmSaveFileRequest.class, body);
    if (scmSaveFileRequest == null) {
        throw new ServiceException.BadRequestException(new ErrorMessage(400, "Failed to bind request"));
    }
    GitContent gitContent = scmSaveFileRequest.getContent();
    BitbucketScmParams scmParamsFromItem = new BitbucketScmParams(item);
    String owner = scmParamsFromItem.getOwner();
    String repo = scmParamsFromItem.getRepo();
    String commitId = StringUtils.isNotBlank(gitContent.getCommitId()) ? gitContent.getCommitId() : gitContent.getSha();
    BitbucketApi api = BitbucketServerScm.getApi(scmParamsFromItem.getApiUrl(), this.getScmId(), scmParamsFromItem.getCredentials());
    String content;
    try {
        content = new String(Base64.decodeBase64(gitContent.getBase64Data()), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
    String message = gitContent.getMessage();
    if (message == null) {
        message = gitContent.getPath() + " created with BlueOcean";
    }
    BbSaveContentResponse response = api.saveContent(owner, repo, gitContent.getPath(), content, message, gitContent.getBranch(), gitContent.getSourceBranch(), commitId);
    final GitContent respContent = new GitContent.Builder().branch(gitContent.getBranch()).path(gitContent.getPath()).owner(gitContent.getOwner()).repo(gitContent.getRepo()).sha(response.getCommitId()).name(gitContent.getPath()).commitId(response.getCommitId()).build();
    return new ScmFile<GitContent>() {

        @Override
        public GitContent getContent() {
            return respContent;
        }
    };
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BbSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)

Example 14 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class GitReadSaveService method saveContent.

@Override
public Object saveContent(@Nonnull StaplerRequest req, @Nonnull Item item) {
    item.checkPermission(Item.CONFIGURE);
    User user = User.current();
    if (user == null) {
        throw new ServiceException.UnauthorizedException("Not authenticated");
    }
    try {
        // parse json...
        JSONObject json = JSONObject.fromObject(IOUtils.toString(req.getReader()));
        GitReadSaveRequest r = makeSaveRequest(item, json);
        r.save();
        return new GitFile(new GitContent(r.filePath, user.getId(), r.gitSource.getRemote(), r.filePath, 0, "sha", null, "", r.branch, r.sourceBranch, true, ""));
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Unable to save file content", e);
    }
}
Also used : User(hudson.model.User) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) IOException(java.io.IOException) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)

Example 15 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class GithubScmContentProviderTest method saveContentToOrgFolder.

@Test
public void saveContentToOrgFolder() throws UnirestException, IOException {
    String credentialId = createGithubCredential(user);
    StaplerRequest staplerRequest = mockStapler();
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test1").message("another commit").owner("cloudbeers").path("Jankinsfile").repo("PR-demo").sha("e23b8ef5c2c4244889bf94db6c05cc08ea138aef").build();
    when(staplerRequest.bindJSON(Mockito.eq(GithubScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new GithubScmSaveFileRequest(content));
    MultiBranchProject mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME);
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"test1\",\n" + "    \"repo\" : \"PR-demo\",\n" + "    \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + "    \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    GithubFile file = (GithubFile) new GithubScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("Jenkinsfile", file.getContent().getName());
    assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", file.getContent().getSha());
    assertEquals("PR-demo", file.getContent().getRepo());
    assertEquals("cloudbeers", file.getContent().getOwner());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) Test(org.junit.Test)

Aggregations

GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)21 MultiBranchProject (jenkins.branch.MultiBranchProject)17 JSONObject (net.sf.json.JSONObject)17 Test (org.junit.Test)17 StaplerRequest (org.kohsuke.stapler.StaplerRequest)17 BufferedReader (java.io.BufferedReader)15 StringReader (java.io.StringReader)15 ServiceException (io.jenkins.blueocean.commons.ServiceException)10 User (hudson.model.User)8 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Mailer (hudson.tasks.Mailer)6 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)5 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BbBranch (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbBranch)1 BbSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1