use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.
the class GitReadSaveService method getContent.
@Override
public Object getContent(@Nonnull StaplerRequest req, @Nonnull Item item) {
item.checkPermission(Item.READ);
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
GitReadSaveRequest r = makeSaveRequest(item, req);
try {
String encoded = Base64.encode(r.read());
return new GitFile(new GitContent(r.filePath, user.getId(), r.gitSource.getRemote(), r.filePath, 0, "sha", encoded, "", r.branch, r.sourceBranch, true, ""));
} catch (ServiceException.UnauthorizedException e) {
// if (r.gitSource.getRemote().matches("([^@:]+@.*|ssh://.*)"))
throw new ServiceException.PreconditionRequired("Invalid credential", e);
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException("Unable to get file content", e);
}
}
use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method saveContentToOrgFolderGHE.
@Test
public void saveContentToOrgFolderGHE() throws UnirestException, IOException {
String credentialId = createGithubEnterpriseCredential();
StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID);
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, GithubEnterpriseScm.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());
}
use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method saveContentToMbp.
@Test
public void saveContentToMbp() 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());
}
use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method saveContentToMbpMissingBranch.
@Test
public void saveContentToMbpMissingBranch() throws UnirestException, IOException {
String credentialId = createGithubCredential(user);
StaplerRequest staplerRequest = mockStapler();
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test2").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\" : \"test2\",\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());
}
use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method unauthorizedSaveContentToMbpGHEShouldFail.
@Test
public void unauthorizedSaveContentToMbpGHEShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createGithubEnterpriseCredential(alice);
StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID);
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(aliceCredentialId, user, GithubEnterpriseScm.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()));
try {
// Bob trying to access content but his credential is not setup so should fail
new GithubScmContentProvider().saveContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from github: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
Aggregations