Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/org/kohsuke/github/GHOrgPreReceiveHook.java
@@ -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;
}

}
7 changes: 7 additions & 0 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Expand Up @@ -784,4 +784,11 @@ public GHHook createWebHook(URL url, Collection<GHEvent> events) throws IOExcept
public GHHook createWebHook(URL url) throws IOException {
return createWebHook(url, null);
}

public GHPreReceiveHook configurePreReceiveHook(long id,
GHPreReceiveHookEnforcement enforcement,
boolean downstreamConfigurable) throws IOException {
return GHPreReceiveHooks.orgContext(this)
.configurePreReceiveHook(new GHOrgPreReceiveHook(id, enforcement, downstreamConfigurable));
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/kohsuke/github/GHPreReceiveHook.java
@@ -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));
Copy link
Member

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.

}
public GHPreReceiveHook() {
}

}
@@ -0,0 +1,9 @@
package org.kohsuke.github;

public enum GHPreReceiveHookEnforcement {
DISABLED, ENABLED, TESTING;

public String toParameterValue() {
Copy link
Member

Choose a reason for hiding this comment

The 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 src/main/java/org/kohsuke/github/GHPreReceiveHooks.java
@@ -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 src/main/java/org/kohsuke/github/GHRepoPreReceiveHook.java
@@ -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;
}

}
7 changes: 7 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Expand Up @@ -2530,6 +2530,13 @@ public GHHook createWebHook(URL url) throws IOException {
return createWebHook(url, null);
}

public GHPreReceiveHook setPreReceiveHookEnforcement(long id, GHPreReceiveHookEnforcement enforcement)
throws IOException {
GHRepoPreReceiveHook hook = new GHRepoPreReceiveHook();
return GHPreReceiveHooks.repoContext(this, owner)
.configurePreReceiveHook(new GHRepoPreReceiveHook(id, enforcement));
}

/**
* Returns a set that represents the post-commit hook URLs. The returned set is live, and changes made to them are
* reflected to GitHub.
Expand Down