-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from flowlogix/perms
Breaking: Limit permissions (optionally) to collaborators when triggering builds
- Loading branch information
Showing
12 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions
51
src/main/java/com/adobe/jenkins/github_pr_comment_build/GithubHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.adobe.jenkins.github_pr_comment_build; | ||
|
||
import com.cloudbees.plugins.credentials.common.StandardCredentials; | ||
import hudson.model.Job; | ||
import java.io.IOException; | ||
import javax.annotation.Nonnull; | ||
import jenkins.scm.api.SCMSource; | ||
import org.jenkinsci.plugins.github_branch_source.Connector; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GitHub; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* client utility methods | ||
*/ | ||
public class GithubHelper { | ||
|
||
private final static Logger LOG = LoggerFactory.getLogger(GithubHelper.class); | ||
|
||
private GithubHelper() { | ||
// private | ||
} | ||
|
||
public static boolean isAuthorized(final Job<?, ?> job, final String author) { | ||
try { | ||
GHRepository ghRepository = getGHRepository(job); | ||
boolean authorized = ghRepository.getCollaboratorNames().contains(author); | ||
LOG.debug("User {} authorized: {}", author, authorized); | ||
return authorized; | ||
} catch (final IOException e) { | ||
LOG.debug("Received an exception while trying to check if user {} is a collaborator for repo of job {}", | ||
author, job.getFullName()); | ||
LOG.debug("isAuthorized() - Exception", e); | ||
return false; | ||
} | ||
} | ||
|
||
private static GHRepository getGHRepository(@Nonnull final Job<?, ?> job) throws IOException { | ||
SCMSource scmSource = SCMSource.SourceByItem.findSource(job); | ||
if (scmSource instanceof GitHubSCMSource) { | ||
GitHubSCMSource gitHubSource = (GitHubSCMSource) scmSource; | ||
StandardCredentials credentials = Connector.lookupScanCredentials( | ||
job, gitHubSource.getApiUri(), gitHubSource.getCredentialsId()); | ||
GitHub github = Connector.connect(gitHubSource.getApiUri(), credentials); | ||
return github.getRepository(gitHubSource.getRepoOwner() + "/" + gitHubSource.getRepository()); | ||
} | ||
throw new IllegalArgumentException("Job's SCM is not GitHub."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...rces/com/adobe/jenkins/github_pr_comment_build/TriggerPRReviewBranchProperty/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> | ||
<f:entry title="Allow untrusted users to trigger the build" field="allowUntrusted"> | ||
<f:checkbox default="false"/> | ||
</f:entry> | ||
</j:jelly> |
8 changes: 8 additions & 0 deletions
8
...rces/com/adobe/jenkins/github_pr_comment_build/TriggerPRUpdateBranchProperty/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> | ||
<f:entry title="Allow untrusted users to trigger the build" field="allowUntrusted"> | ||
<f:checkbox default="false"/> | ||
</f:entry> | ||
</j:jelly> |