-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHRef.java
More file actions
224 lines (200 loc) · 7.1 KB
/
GHRef.java
File metadata and controls
224 lines (200 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
// TODO: Auto-generated Javadoc
/**
* Provides information on a Git ref from GitHub.
*
* @author Michael Clarke
*/
public class GHRef extends GitHubInteractiveObject {
/**
* The type GHObject.
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public static class GHObject {
private String type, sha, url;
/**
* Create default GHObject instance
*/
public GHObject() {
}
/**
* SHA1 of this object.
*
* @return the sha
*/
public String getSha() {
return sha;
}
/**
* Type of the object, such as "commit".
*
* @return the type
*/
public String getType() {
return type;
}
/**
* API URL to this Git data, such as
* https://api.github.com/repos/jenkinsci/jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
*
* @return the url
*/
public URL getUrl() {
return GitHubClient.parseURL(url);
}
}
/**
* Retrieve a ref of the given type for the current GitHub repository.
*
* @param repository
* the repository to read from
* @param refName
* eg: heads/branch
* @return refs matching the request type
* @throws IOException
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
static GHRef read(GHRepository repository, String refName) throws IOException {
// Also accept e.g. "refs/heads/branch" for consistency with createRef().
if (refName.startsWith("refs/")) {
refName = refName.replaceFirst("refs/", "");
}
// We would expect this to use `git/ref/%s` but some versions of GHE seem to not support it
// Instead use `git/refs/%s` and check the result actually matches the ref
GHRef result = null;
try {
result = repository.root()
.createRequest()
.withUrlPath(repository.getApiTailUrl(String.format("git/refs/%s", refName)))
.fetch(GHRef.class);
} catch (IOException e) {
// If the parse exception is due to the above returning an array instead of a single ref
// that means the individual ref did not exist. Handled by result check below.
// Otherwise, rethrow.
if (!(e.getCause() instanceof JsonMappingException)) {
throw e;
}
}
// Verify that the ref returned is the one requested
// Used .endsWith(refName) instead of .equals("refs/" + refName) to workaround a GitBucket
// issue where the "ref" field omits the "refs/" prefix. "endsWith()" is functionally
// the same for this scenario - the server refs matching is prefix-based, so
// a ref that ends with the correct string will always be the correct one.
if (result == null || !result.getRef().endsWith(refName)) {
throw new GHFileNotFoundException(String.format("git/refs/%s", refName)
+ " {\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}");
}
return result;
}
/**
* Retrieves all refs of the given type for the current GitHub repository.
*
* @param repository
* the repository to read from
* @param refType
* the type of reg to search for e.g. <code>tags</code> or <code>commits</code>
* @return paged iterable of all refs of the specified type
*/
static PagedIterable<GHRef> readMatching(GHRepository repository, String refType) {
if (refType.startsWith("refs/")) {
refType = refType.replaceFirst("refs/", "");
}
String url = repository.getApiTailUrl(String.format("git/refs/%s", refType));
// if no types, do not end with slash just to be safe.
if (refType.equals("")) {
url = url.substring(0, url.length() - 1);
}
return repository.root().createRequest().withUrlPath(url).toIterable(GHRef[].class, item -> repository.root());
}
/**
* Retrieves all refs that match the given prefix using the matching-refs endpoint.
*
* @param repository
* the repository to read from
* @param refPrefix
* the ref prefix to search for e.g. <code>heads/main</code> or <code>tags/v1</code>
* @return paged iterable of all refs matching the specified prefix
*/
static PagedIterable<GHRef> readMatchingRefs(GHRepository repository, String refPrefix) {
if (refPrefix.startsWith("refs/")) {
refPrefix = refPrefix.replaceFirst("refs/", "");
}
String url = repository.getApiTailUrl(String.format("git/matching-refs/%s", refPrefix));
return repository.root().createRequest().withUrlPath(url).toIterable(GHRef[].class, item -> repository.root());
}
private GHObject object;
private String ref, url;
/**
* Create default GHRef instance
*/
public GHRef() {
}
/**
* Deletes this ref from the repository using the GitHub API.
*
* @throws IOException
* the io exception
*/
public void delete() throws IOException {
root().createRequest().method("DELETE").withUrlPath(url).send();
}
/**
* The object that this ref points to.
*
* @return the object
*/
public GHObject getObject() {
return object;
}
/**
* Name of the ref, such as "refs/tags/abc".
*
* @return the ref
*/
public String getRef() {
return ref;
}
/**
* The API URL of this tag, such as https://api.github.com/repos/jenkinsci/jenkins/git/refs/tags/1.312
*
* @return the url
*/
public URL getUrl() {
return GitHubClient.parseURL(url);
}
/**
* Updates this ref to the specified commit.
*
* @param sha
* The SHA1 value to set this reference to
* @throws IOException
* the io exception
*/
public void updateTo(String sha) throws IOException {
updateTo(sha, false);
}
/**
* Updates this ref to the specified commit.
*
* @param sha
* The SHA1 value to set this reference to
* @param force
* Whether or not to force this ref update.
* @throws IOException
* the io exception
*/
public void updateTo(String sha, Boolean force) throws IOException {
root().createRequest()
.method("PATCH")
.with("sha", sha)
.with("force", force)
.withUrlPath(url)
.fetch(GHRef.class);
}
}