-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathGHObject.java
188 lines (166 loc) · 5.88 KB
/
GHObject.java
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
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
/**
* Most (all?) domain objects in GitHub seems to have these 4 properties.
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public abstract class GHObject extends GitHubInteractiveObject {
/**
* Capture response HTTP headers on the state object.
*/
protected transient Map<String, List<String>> responseHeaderFields;
private String url;
private long id;
private String nodeId;
private String createdAt;
private String updatedAt;
GHObject() {
}
/**
* Called by Jackson
*
* @param connectorResponse
* the {@link GitHubConnectorResponse} to get headers from.
*/
@JacksonInject
protected void setResponseHeaderFields(@CheckForNull GitHubConnectorResponse connectorResponse) {
if (connectorResponse != null) {
responseHeaderFields = connectorResponse.allHeaders();
}
}
/**
* Returns the HTTP response headers given along with the state of this object.
*
* <p>
* Some of the HTTP headers have nothing to do with the object, for example "Cache-Control" and others are different
* depending on how this object was retrieved.
* <p>
* This method was added as a kind of hack to allow the caller to retrieve OAuth scopes and such. Use with caution.
* The method might be removed in the future.
*
* @return a map of header names to value lists
*/
@CheckForNull
@Deprecated
public Map<String, List<String>> getResponseHeaderFields() {
return GitHubClient.unmodifiableMapOrNull(responseHeaderFields);
}
/**
* When was this resource created?
*
* @return date created
* @throws IOException
* on error
*/
@WithBridgeMethods(value = String.class, adapterMethod = "createdAtStr")
public Date getCreatedAt() throws IOException {
return GitHubClient.parseDate(createdAt);
}
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getCreatedAt")
private Object createdAtStr(Date id, Class type) {
return createdAt;
}
/**
* Gets url.
*
* @return API URL of this object.
*/
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
public URL getUrl() {
return GitHubClient.parseURL(url);
}
/**
* Gets html url.
*
* @return URL of this object for humans, which renders some HTML.
* @throws IOException
* on error
*/
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
public abstract URL getHtmlUrl() throws IOException;
/**
* When was this resource last updated?
*
* @return updated date
* @throws IOException
* on error
*/
public Date getUpdatedAt() throws IOException {
return GitHubClient.parseDate(updatedAt);
}
/**
* Get Global node_id from Github object.
*
* @see <a href="https://developer.github.com/v4/guides/using-global-node-ids/">Using Global Node IDs</a>
*
* @return Global Node ID.
*/
public String getNodeId() {
return nodeId;
}
/**
* Gets id.
*
* @return Unique ID number of this resource.
*/
@WithBridgeMethods(value = { String.class, int.class }, adapterMethod = "longToStringOrInt")
public long getId() {
return id;
}
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getId")
private Object longToStringOrInt(long id, Class type) {
if (type == String.class)
return String.valueOf(id);
if (type == int.class)
return (int) id;
throw new AssertionError("Unexpected type: " + type);
}
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getHtmlUrl")
private Object urlToString(URL url, Class type) {
return url == null ? null : url.toString();
}
/**
* String representation to assist debugging and inspection. The output format of this string is not a committed
* part of the API and is subject to change.
*/
@Override
public String toString() {
return new ReflectionToStringBuilder(this, TOSTRING_STYLE, null, null, false, false) {
@Override
protected boolean accept(Field field) {
return super.accept(field) && !field.isAnnotationPresent(SkipFromToString.class);
}
}.toString();
}
private static final ToStringStyle TOSTRING_STYLE = new ToStringStyle() {
{
this.setUseShortClassName(true);
}
@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
// skip unimportant properties. '_' is a heuristics as important properties tend to have short names
if (fieldName.contains("_"))
return;
// avoid recursing other GHObject
if (value instanceof GHObject)
return;
// likewise no point in showing root
if (value instanceof GitHub)
return;
super.append(buffer, fieldName, value, fullDetail);
}
};
}