GithubScmContentProvider extends AbstractScmContentProvider { @Override protected Object getContent(ScmGetRequest request) { GithubScm.validateUserHasPushPermission(request.getApiUrl(), request.getCredentials().getPassword().getPlainText(), request.getOwner(), request.getRepo()); String url = String.format("%s/repos/%s/%s/contents/%s", request.getApiUrl(), request.getOwner(), request.getRepo(), request.getPath()); if(request.getBranch() != null){ url += "?ref="+request.getBranch(); } try { Map ghContent = HttpRequest.get(url) .withAuthorizationToken(request.getCredentials().getPassword().getPlainText()) .to(Map.class); if(ghContent == null){ throw new ServiceException.UnexpectedErrorException("Failed to load file: "+request.getPath()); } String base64Data = (String)ghContent.get("content"); base64Data = base64Data == null ? null : base64Data.replace("\n", ""); return new GithubFile(new GitContent.Builder() .sha((String)ghContent.get("sha")) .name((String)ghContent.get("name")) .repo(request.getRepo()) .owner(request.getOwner()) .path(request.getPath()) .base64Data(base64Data) .build()); } catch (IOException e) { throw new ServiceException.UnexpectedErrorException(String.format("Failed to load file %s: %s", request.getPath(),e.getMessage()), e); } }  @Nonnull @Override String getScmId(); @Override String getApiUrl(@Nonnull Item item); @Override Object saveContent(@Nonnull StaplerRequest staplerRequest, @Nonnull Item item); @SuppressWarnings("unchecked") @Override boolean support(@Nonnull Item item);  }
@Test public void getContentForOrgFolder() throws UnirestException { String credentialId = createGithubCredential(user); StaplerRequest staplerRequest = mockStapler(); MultiBranchProject mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME); GithubFile content = (GithubFile) new GithubScmContentProvider().getContent(staplerRequest, mbp); assertEquals("Jenkinsfile", content.getContent().getName()); assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", content.getContent().getSha()); assertEquals("PR-demo", content.getContent().getRepo()); assertEquals("cloudbeers", content.getContent().getOwner()); }
@Test public void getContentForOrgFolderGHE() throws UnirestException { String credentialId = createGithubEnterpriseCredential(); StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID); MultiBranchProject mbp = mockMbp(credentialId, user, GithubEnterpriseScm.DOMAIN_NAME); GithubFile content = (GithubFile) new GithubScmContentProvider().getContent(staplerRequest, mbp); assertEquals("Jenkinsfile", content.getContent().getName()); assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", content.getContent().getSha()); assertEquals("PR-demo", content.getContent().getRepo()); assertEquals("cloudbeers", content.getContent().getOwner()); }
@Test public void getContentForMbp() throws UnirestException { String credentialId = createGithubCredential(user); StaplerRequest staplerRequest = mockStapler(); MultiBranchProject mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME); GithubFile content = (GithubFile) new GithubScmContentProvider().getContent(staplerRequest, mbp); assertEquals("Jenkinsfile", content.getContent().getName()); assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", content.getContent().getSha()); assertEquals("PR-demo", content.getContent().getRepo()); assertEquals("cloudbeers", content.getContent().getOwner()); }
@Test public void getContentForMbpGHE() throws UnirestException { String credentialId = createGithubEnterpriseCredential(); StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID); MultiBranchProject mbp = mockMbp(credentialId, user, GithubEnterpriseScm.DOMAIN_NAME); GithubFile content = (GithubFile) new GithubScmContentProvider().getContent(staplerRequest, mbp); assertEquals("Jenkinsfile", content.getContent().getName()); assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", content.getContent().getSha()); assertEquals("PR-demo", content.getContent().getRepo()); assertEquals("cloudbeers", content.getContent().getOwner()); }
@Test public void unauthorizedAccessToContentForOrgFolderShouldFail() throws UnirestException, IOException { User alice = User.get("alice"); alice.setFullName("Alice Cooper"); alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org")); String aliceCredentialId = createGithubCredential(alice); StaplerRequest staplerRequest = mockStapler(); MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubScm.DOMAIN_NAME); try { new GithubScmContentProvider().getContent(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"); }
@Test public void unauthorizedAccessToContentForOrgFolderGHEShouldFail() 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); MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubEnterpriseScm.DOMAIN_NAME); try { new GithubScmContentProvider().getContent(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"); }
@Test public void unauthorizedAccessToContentForMbpShouldFail() throws UnirestException, IOException { User alice = User.get("alice"); alice.setFullName("Alice Cooper"); alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org")); String aliceCredentialId = createGithubCredential(alice); StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID); MultiBranchProject mbp = mockMbp(aliceCredentialId, alice, GithubEnterpriseScm.DOMAIN_NAME); try { new GithubScmContentProvider().getContent(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"); }
@Test public void unauthorizedAccessToContentForMbpGHEShouldFail() 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); MultiBranchProject mbp = mockMbp(aliceCredentialId, alice, GithubEnterpriseScm.DOMAIN_NAME); try { new GithubScmContentProvider().getContent(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"); }