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 autolink reference function #1987

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/main/java/org/kohsuke/github/GHAutolink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;

/**
* Represents a GitHub repository autolink reference.
*
* @author Alaurant
* @see GHAutolinkBuilder
* @see GHRepository#listAutolinks() GHRepository#listAutolinks()
* @see <a href="https://docs.github.com/en/rest/repos/autolinks">Repository autolinks API</a>
*/
public class GHAutolink {

private int id;
private String key_prefix;
private String url_template;
private boolean is_alphanumeric;
private GHRepository owner;

/**
* Instantiates a new Gh autolink.
*/
public GHAutolink() {
}

/**
* Gets the autolink ID
*
* @return the id
*/
public int getId() {
return id;
}

/**
* Gets the key prefix used to identify issues/PR references
*
* @return the key prefix string
*/
public String getKeyPrefix() {
return key_prefix;
}

/**
* Gets the URL template that will be used for matching
*
* @return the URL template string
*/
public String getUrlTemplate() {
return url_template;
}

/**
* Checks if the autolink uses alphanumeric values
*
* @return true if alphanumeric, false otherwise
*/
public boolean isAlphanumeric() {
return is_alphanumeric;
}

/**
* Gets the repository that owns this autolink
*
* @return the repository instance
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHRepository getOwner() {
return owner;
}

/**
* Deletes this autolink
*
* @throws IOException
* if the deletion fails
*/
public void delete() throws IOException {
owner.root()
.createRequest()
.method("DELETE")
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", owner.getOwnerName(), owner.getName(), getId()))
.send();
}

/**
* Wraps this autolink with its owner repository.
*
* @param owner
* the repository that owns this autolink
* @return this instance
*/
GHAutolink lateBind(GHRepository owner) {
this.owner = owner;
return this;
}
}
90 changes: 90 additions & 0 deletions src/main/java/org/kohsuke/github/GHAutolinkBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.kohsuke.github;

import java.io.IOException;

// TODO: Auto-generated Javadoc
/**
* The type Gh autolink builder.
*
* @see GHRepository#createAutolink()
* @see GHAutolink
*/
public class GHAutolinkBuilder {

private final GHRepository repo;
private final Requester req;
private String keyPrefix;
private String urlTemplate;
private Boolean isAlphanumeric;

/**
* Instantiates a new Gh autolink builder.
*
* @param repo
* the repo
*/
GHAutolinkBuilder(GHRepository repo) {
this.repo = repo;
req = repo.root().createRequest();
}

/**
* With key prefix gh autolink builder.
*
* @param keyPrefix
* the key prefix
* @return the gh autolink builder
*/
public GHAutolinkBuilder withKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
return this;
}

/**
* With url template gh autolink builder.
*
* @param urlTemplate
* the url template
* @return the gh autolink builder
*/
public GHAutolinkBuilder withUrlTemplate(String urlTemplate) {
this.urlTemplate = urlTemplate;
return this;
}

/**
* With is alphanumeric gh autolink builder.
*
* @param isAlphanumeric
* the is alphanumeric
* @return the gh autolink builder
*/
public GHAutolinkBuilder withIsAlphanumeric(boolean isAlphanumeric) {
this.isAlphanumeric = isAlphanumeric;
return this;
}

private String getApiTail() {
return String.format("/repos/%s/%s/autolinks", repo.getOwnerName(), repo.getName());
}

/**
* Create gh autolink.
*
* @return the gh autolink
* @throws IOException
* the io exception
*/
public GHAutolink create() throws IOException {
GHAutolink autolink = req.method("POST")
.with("key_prefix", keyPrefix)
.with("url_template", urlTemplate)
.with("is_alphanumeric", isAlphanumeric)
.withHeader("Accept", "application/vnd.github+json")
.withUrlPath(getApiTail())
.fetch(GHAutolink.class);

return autolink.lateBind(repo);
}

}
60 changes: 60 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3375,4 +3375,64 @@ protected Setter(@Nonnull GHRepository repository) {
requester.method("PATCH").withUrlPath(repository.getApiTailUrl(""));
}
}

/**
* Create an autolink gh autolink builder.
*
* @return the gh autolink builder
*/
public GHAutolinkBuilder createAutolink() {
return new GHAutolinkBuilder(this);
}

/**
* List all autolinks of a repo (admin only).
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#get-all-autolinks-of-a-repository)
*
* @return all autolinks in the repo
* @throws IOException
* the io exception
*/
public PagedIterable<GHAutolink> listAutolinks() throws IOException {
return root().createRequest()
.withHeader("Accept", "application/vnd.github+json")
.withUrlPath(String.format("/repos/%s/%s/autolinks", getOwnerName(), getName()))
.toIterable(GHAutolink[].class, item -> item.lateBind(this));
}

/**
* Read an autolink by ID.
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#get-an-autolink-reference-of-a-repository)
*
* @param autolinkId
* the autolink id
* @return the autolink
* @throws IOException
* the io exception
*/
public GHAutolink readAutolink(int autolinkId) throws IOException {
return root().createRequest()
.withHeader("Accept", "application/vnd.github+json")
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", getOwnerName(), getName(), autolinkId))
.fetch(GHAutolink.class)
.lateBind(this);
}

/**
* Delete autolink.
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#delete-an-autolink-reference-from-a-repository)
*
* @param autolinkId
* the autolink id
* @throws IOException
* the io exception
*/
public void deleteAutolink(int autolinkId) throws IOException {
root().createRequest()
.method("DELETE")
.withHeader("Accept", "application/vnd.github+json")
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", getOwnerName(), getName(), autolinkId))
.send();
}

}
Loading
Loading