New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for GHE pre-receive hook configuration #1584
Open
michaelpigg
wants to merge
3
commits into
hub4j:main
Choose a base branch
from
michaelpigg:ghe-pre-receive-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.kohsuke.github; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class GHOrgPreReceiveHook extends GHPreReceiveHook { | ||
|
||
transient GHOrganization organization; | ||
|
||
boolean allowDownstreamConfiguration; | ||
|
||
public GHOrgPreReceiveHook() { | ||
} | ||
|
||
public GHOrgPreReceiveHook(long id, GHPreReceiveHookEnforcement enforcement, boolean allowDownstreamConfiguration) { | ||
this.id = id; | ||
this.enforcement = enforcement.toParameterValue(); | ||
this.allowDownstreamConfiguration = allowDownstreamConfiguration; | ||
} | ||
|
||
@Override | ||
public URL getHtmlUrl() throws IOException { | ||
return null; | ||
} | ||
|
||
GHOrgPreReceiveHook wrap(GHOrganization organization) { | ||
this.organization = organization; | ||
return this; | ||
} | ||
|
||
static GHOrgPreReceiveHook makeHook(long id, | ||
GHPreReceiveHookEnforcement enforcement, | ||
boolean allowDownstreamConfiguration) { | ||
final GHOrgPreReceiveHook hook = new GHOrgPreReceiveHook(); | ||
hook.id = id; | ||
hook.enforcement = enforcement.toParameterValue(); | ||
hook.allowDownstreamConfiguration = allowDownstreamConfiguration; | ||
return hook; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.kohsuke.github; | ||
|
||
import java.util.Locale; | ||
|
||
public abstract class GHPreReceiveHook extends GHObject { | ||
long id; | ||
|
||
String name; | ||
|
||
String enforcement; | ||
|
||
@Override | ||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getEnforcement() { | ||
return enforcement; | ||
} | ||
|
||
public GHPreReceiveHookEnforcement getEnforcementType() { | ||
return Enum.valueOf(GHPreReceiveHookEnforcement.class, this.enforcement.toUpperCase(Locale.ENGLISH)); | ||
} | ||
public GHPreReceiveHook() { | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/org/kohsuke/github/GHPreReceiveHookEnforcement.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,9 @@ | ||
package org.kohsuke.github; | ||
|
||
public enum GHPreReceiveHookEnforcement { | ||
DISABLED, ENABLED, TESTING; | ||
|
||
public String toParameterValue() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also handle this as well. |
||
return this.name().toLowerCase(); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/main/java/org/kohsuke/github/GHPreReceiveHooks.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,142 @@ | ||
package org.kohsuke.github; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class GHPreReceiveHooks { | ||
|
||
static abstract class Context extends GitHubInteractiveObject { | ||
|
||
private Context(GitHub root) { | ||
super(root); | ||
} | ||
|
||
/** | ||
* Collection. | ||
* | ||
* @return the string | ||
*/ | ||
abstract String collection(); | ||
|
||
/** | ||
* Collection class. | ||
* | ||
* @return the class<? extends GH pre-receive hook[]> | ||
*/ | ||
abstract Class<? extends GHPreReceiveHook[]> collectionClass(); | ||
|
||
/** | ||
* Clazz. | ||
* | ||
* @return the class<? extends GH pre-receive hook> | ||
*/ | ||
abstract Class<? extends GHPreReceiveHook> clazz(); | ||
|
||
/** | ||
* Wrap. | ||
* | ||
* @param hook | ||
* the hook | ||
* @return the GH hook | ||
*/ | ||
abstract GHPreReceiveHook wrap(GHPreReceiveHook hook); | ||
|
||
abstract Map<String, Object> hookExtraParameters(GHPreReceiveHook hook); | ||
|
||
public GHPreReceiveHook configurePreReceiveHook(GHPreReceiveHook hook) throws IOException { | ||
GHPreReceiveHook updatedHook = root().createRequest() | ||
.method("PATCH") | ||
.with("enforcement", hook.getEnforcement()) | ||
.with(hookExtraParameters(hook)) | ||
.withUrlPath(collection(), Long.toString(hook.getId())) | ||
.fetch(clazz()); | ||
|
||
return wrap(updatedHook); | ||
} | ||
|
||
} | ||
|
||
private static class OrgContext extends Context { | ||
private final GHOrganization organization; | ||
|
||
private OrgContext(GHOrganization organization) { | ||
super(organization.root()); | ||
this.organization = organization; | ||
} | ||
|
||
@Override | ||
String collection() { | ||
return String.format("/orgs/%s/pre-receive-hooks", organization.login); | ||
} | ||
|
||
@Override | ||
Class<? extends GHPreReceiveHook[]> collectionClass() { | ||
return GHOrgPreReceiveHook[].class; | ||
} | ||
|
||
@Override | ||
Class<? extends GHPreReceiveHook> clazz() { | ||
return GHOrgPreReceiveHook.class; | ||
} | ||
|
||
@Override | ||
GHPreReceiveHook wrap(GHPreReceiveHook hook) { | ||
return ((GHOrgPreReceiveHook) hook).wrap(organization); | ||
} | ||
|
||
@Override | ||
Map<String, Object> hookExtraParameters(GHPreReceiveHook hook) { | ||
final Map<String, Object> params = new HashMap<>(); | ||
if (hook instanceof GHOrgPreReceiveHook) { | ||
params.put("allow_downstream_configuration", | ||
Boolean.valueOf(((GHOrgPreReceiveHook) hook).allowDownstreamConfiguration)); | ||
} | ||
return params; | ||
} | ||
} | ||
|
||
private static class RepositoryContext extends Context { | ||
private final GHRepository repository; | ||
private final GHUser owner; | ||
|
||
private RepositoryContext(GHRepository repository, GHUser owner) { | ||
super(repository.root()); | ||
this.repository = repository; | ||
this.owner = owner; | ||
} | ||
|
||
String collection() { | ||
return String.format("/repos/%s/%s/pre-receive-hooks", owner.getLogin(), repository.getName()); | ||
} | ||
|
||
@Override | ||
Class<? extends GHPreReceiveHook[]> collectionClass() { | ||
return GHRepoPreReceiveHook[].class; | ||
} | ||
|
||
@Override | ||
Class<? extends GHPreReceiveHook> clazz() { | ||
return GHRepoPreReceiveHook.class; | ||
} | ||
|
||
@Override | ||
GHPreReceiveHook wrap(GHPreReceiveHook hook) { | ||
return ((GHRepoPreReceiveHook) hook).wrap(repository); | ||
} | ||
|
||
@Override | ||
Map<String, Object> hookExtraParameters(GHPreReceiveHook hook) { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
|
||
static Context repoContext(GHRepository repository, GHUser owner) { | ||
return new RepositoryContext(repository, owner); | ||
} | ||
|
||
static Context orgContext(GHOrganization organization) { | ||
return new OrgContext(organization); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/kohsuke/github/GHRepoPreReceiveHook.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,28 @@ | ||
package org.kohsuke.github; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class GHRepoPreReceiveHook extends GHPreReceiveHook { | ||
|
||
transient GHRepository repository; | ||
|
||
public GHRepoPreReceiveHook() { | ||
} | ||
|
||
public GHRepoPreReceiveHook(long id, GHPreReceiveHookEnforcement enforcement) { | ||
this.id = id; | ||
this.enforcement = enforcement.toParameterValue(); | ||
} | ||
|
||
@Override | ||
public URL getHtmlUrl() throws IOException { | ||
return null; | ||
} | ||
|
||
GHRepoPreReceiveHook wrap(GHRepository owner) { | ||
this.repository = owner; | ||
return this; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a helper method for this. Look at other enums.