8000 Add autolink reference function (#1987) · hub4j/github-api@58dcca1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58dcca1

Browse files
authored
Add autolink reference function (#1987)
* add autolink function * add java doc * change method names * fixed test issues * convert Integer to int and wrap to latebind
1 parent 7565225 commit 58dcca1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2726
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* Represents a GitHub repository autolink reference.
9+
*
10+
* @author Alaurant
11+
* @see GHAutolinkBuilder
12+
* @see GHRepository#listAutolinks() GHRepository#listAutolinks()
13+
* @see <a href="https://docs.github.com/en/rest/repos/autolinks">Repository autolinks API</a>
14+
*/
15+
public class GHAutolink {
16+
17+
private int id;
18+
private String key_prefix;
19+
private String url_template;
20+
private boolean is_alphanumeric;
21+
private GHRepository owner;
22+
23+
/**
24+
* Instantiates a new Gh autolink.
25+
*/
26+
public GHAutolink() {
27+
}
28+
29+
/**
30+
* Gets the autolink ID
31+
*
32+
* @return the id
33+
*/
34+
public int getId() {
35+
return id;
36+
}
37+
38+
/**
39+
* Gets the key prefix used to identify issues/PR references
40+
*
41+
* @return the key prefix string
42+
*/
43+
public String getKeyPrefix() {
44+
return key_prefix;
45+
}
46+
47+
/**
48+
* Gets the URL template that will be used for matching
49+
*
50+
* @return the URL template string
51+
*/
52+
public String getUrlTemplate() {
53+
return url_template;
54+
}
55+
56+
/**
57+
* Checks if the autolink uses alphanumeric values
58+
*
59+
* @return true if alphanumeric, false otherwise
60+
*/
61+
public boolean isAlphanumeric() {
62+
return is_alphanumeric;
63+
}
64+
65+
/**
66+
* Gets the repository that owns this autolink
67+
*
68+
* @return the repository instance
69+
*/
70+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
71+
public GHRepository getOwner() {
72+
return owner;
73+
}
74+
75+
/**
76+
* Deletes this autolink
77+
*
78+
* @throws IOException
79+
* if the deletion fails
80+
*/
81+
public void delete() throws IOException {
82+
owner.root()
83+
.createRequest()
84+
.method("DELETE")
85+
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", owner.getOwnerName(), owner.getName(), getId()))
86+
.send();
87+
}
88+
89+
/**
90+
* Wraps this autolink with its owner repository.
91+
*
92+
* @param owner
93+
* the repository that owns this autolink
94+
* @return this instance
95+
*/
96+
GHAutolink lateBind(GHRepository owner) {
97+
this.owner = owner;
98+
return this;
99+
}
100+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
// TODO: Auto-generated Javadoc
6+
/**
7+
* The type Gh autolink builder.
8+
*
9+
* @see GHRepository#createAutolink()
10+
* @see GHAutolink
11+
*/
12+
public class GHAutolinkBuilder {
13+
14+
private final GHRepository repo;
15+
private final Requester req;
16+
private String keyPrefix;
17+
private String urlTemplate;
18+
private Boolean isAlphanumeric;
19+
20+
/**
21+
* Instantiates a new Gh autolink builder.
22+
*
23+
* @param repo
24+
* the repo
25+
*/
26+
GHAutolinkBuilder(GHRepository repo) {
27+
this.repo = repo;
28+
req = repo.root().createRequest();
29+
}
30+
31+
/**
32+
* With key prefix gh autolink builder.
33+
*
34+
* @param keyPrefix
35+
* the key prefix
36+
* @return the gh autolink builder
37+
*/
38+
public GHAutolinkBuilder withKeyPrefix(String keyPrefix) {
39+
this.keyPrefix = keyPrefix;
40+
return this;
41+
}
42+
43+
/**
44+
* With url template gh autolink builder.
45+
*
46+
* @param urlTemplate
47+
* the url template
48+
* @return the gh autolink builder
49+
*/
50+
public GHAutolinkBuilder withUrlTemplate(String urlTemplate) {
51+
this.urlTemplate = urlTemplate;
52+
return this;
53+
}
54+
55+
/**
56+
* With is alphanumeric gh autolink builder.
57+
*
58+
* @param isAlphanumeric
59+
* the is alphanumeric
60+
* @return the gh autolink builder
61+
*/
62+
public GHAutolinkBuilder withIsAlphanumeric(boolean isAlphanumeric) {
63+
this.isAlphanumeric = isAlphanumeric;
64+
return this;
65+
}
66+
67+
private String getApiTail() {
68+
return String.format("/repos/%s/%s/autolinks", repo.getOwnerName(), repo.getName());
69+
}
70+
71+
/**
72+
* Create gh autolink.
73+
*
74+
* @return the gh autolink
75+
* @throws IOException
76+
* the io exception
77+
*/
78+
public GHAutolink create() throws IOException {
79+
GHAutolink autolink = req.method("POST")
80+
.with("key_prefix", keyPrefix)
81+
.with("url_template", urlTemplate)
82+
.with("is_alphanumeric", isAlphanumeric)
83+
.withHeader("Accept", "application/vnd.github+json")
84+
.withUrlPath(getApiTail())
85+
.fetch(GHAutolink.class);
86+
87+
return autolink.lateBind(repo);
88+
}
89+
90+
}

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,4 +3375,64 @@ protected Setter(@Nonnull GHRepository repository) {
33753375
requester.method("PATCH").withUrlPath(repository.getApiTailUrl(""));
33763376
}
33773377
}
3378+
3379+
/**
3380+
* Create an autolink gh autolink builder.
3381+
*
3382+
* @return the gh autolink builder
3383+
*/
3384+
public GHAutolinkBuilder createAutolink() {
3385+
return new GHAutolinkBuilder(this);
3386+
}
3387+
3388+
/**
3389+
* List all autolinks of a repo (admin only).
3390+
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#get-all-autolinks-of-a-repository)
3391+
*
3392+
* @return all autolinks in the repo
3393+
* @throws IOException
3394+
* the io exception
3395+
*/
3396+
public PagedIterable<GHAutolink> listAutolinks() throws IOException {
3397+
return root().createRequest()
3398+
.withHeader("Accept", "application/vnd.github+json")
3399+
.withUrlPath(String.format("/repos/%s/%s/autolinks", getOwnerName(), getName()))
3400+
.toIterable(GHAutolink[].class, item -> item.lateBind(this));
3401+
}
3402+
3403+
/**
3404+
* Read an autolink by ID.
3405+
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#get-an-autolink-reference-of-a-repository)
3406+
*
3407+
* @param autolinkId
3408+
* the autolink id
3409+
* @return the autolink
3410+
* @throws IOException
3411+
* the io exception
3412+
*/
3413+
public GHAutolink readAutolink(int autolinkId) throws IOException {
3414+
return root().createRequest()
3415+
.withHeader("Accept", "application/vnd.github+json")
3416+
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", getOwnerName(), getName(), autolinkId))
3417+
.fetch(GHAutolink.class)
3418+
.lateBind(this);
3419+
}
3420+
3421+
/**
3422+
* Delete autolink.
3423+
* (https://docs.github.com/en/rest/repos/autolinks?apiVersion=2022-11-28#delete-an-autolink-reference-from-a-repository)
3424+
*
3425+
* @param autolinkId
3426+
* the autolink id
3427+
* @throws IOException
3428+
* the io exception
3429+
*/
3430+
public void deleteAutolink(int autolinkId) throws IOException {
3431+
root().createRequest()
3432+
.method("DELETE")
3433+
.withHeader("Accept", "application/vnd.github+json")
3434+
.withUrlPath(String.format("/repos/%s/%s/autolinks/%d", getOwnerName(), getName(), autolinkId))
3435+
.send();
3436+
}
3437+
33783438
}

0 commit comments

Comments
 (0)
0