Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb1cecb

Browse filesBrowse files
dedickinsonkohsuke
authored andcommitted
PR-284: license API support
Had to do git-diff | git-apply to avoid whitespe changes to GHRepository
1 parent d82397a commit bb1cecb
Copy full SHA for bb1cecb

File tree

Expand file treeCollapse file tree

6 files changed

+580
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+580
-0
lines changed
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016, Duncan Dickinson
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.kohsuke.github;
26+
27+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28+
29+
import java.net.URL;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
/**
34+
* The GitHub Preview API's license information
35+
* <p>
36+
* WARNING: This uses a PREVIEW API - you must use {@link org.kohsuke.github.extras.PreviewHttpConnector}
37+
*
38+
* @author Duncan Dickinson
39+
* @see GitHub#getLicense(String)
40+
* @see GHRepository#getFullLicense()
41+
* @see <a href="https://developer.github.com/v3/licenses/">https://developer.github.com/v3/licenses/</a>
42+
*/
43+
@SuppressWarnings({"UnusedDeclaration"})
44+
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
45+
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
46+
public class GHLicense extends GHLicenseBase {
47+
48+
protected String html_url, description, category, implementation, body;
49+
50+
protected List<String> required = new ArrayList<String>();
51+
protected List<String> permitted = new ArrayList<String>();
52+
protected List<String> forbidden = new ArrayList<String>();
53+
54+
public URL getHtmlUrl() {
55+
return GitHub.parseURL(html_url);
56+
}
57+
58+
public String getDescription() {
59+
return description;
60+
}
61+
62+
public String getCategory() {
63+
return category;
64+
}
65+
66+
public String getImplementation() {
67+
return implementation;
68+
}
69+
70+
public List<String> getRequired() {
71+
return required;
72+
}
73+
74+
public List<String> getPermitted() {
75+
return permitted;
76+
}
77+
78+
public List<String> getForbidden() {
79+
return forbidden;
80+
}
81+
82+
public String getBody() {
83+
return body;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "GHLicense{" +
89+
"html_url='" + html_url + '\'' +
90+
", description='" + description + '\'' +
91+
", category='" + category + '\'' +
92+
", implementation='" + implementation + '\'' +
93+
", body='" + body + '\'' +
94+
", required=" + required +
95+
", permitted=" + permitted +
96+
", forbidden=" + forbidden +
97+
", htmlUrl=" + getHtmlUrl() +
98+
"} " + super.toString();
99+
}
100+
101+
@Override
102+
public boolean equals(Object o) {
103+
return super.equals(o);
104+
}
105+
106+
@Override
107+
public int hashCode() {
108+
return super.hashCode();
109+
}
110+
}
+107Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016, Duncan Dickinson
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.kohsuke.github;
26+
27+
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
28+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29+
30+
import java.net.URL;
31+
32+
/**
33+
* The basic information for GitHub API licenses - as use in a number of
34+
* API calls that only return the basic details
35+
* <p>
36+
* WARNING: This uses a PREVIEW API - you must use {@link org.kohsuke.github.extras.PreviewHttpConnector}
37+
*
38+
* @author Duncan Dickinson
39+
* @see <a href="https://developer.github.com/v3/licenses/">https://developer.github.com/v3/licenses/</a>
40+
* @see GitHub#listLicenses()
41+
* @see GHRepository#getLicense()
42+
* @see GHLicense GHLicense subclass for the more comprehensive listing of properties
43+
*/
44+
@SuppressWarnings({"UnusedDeclaration"})
45+
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
46+
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
47+
public class GHLicenseBase {
48+
49+
protected String key, name, url;
50+
protected Boolean featured;
51+
52+
/**
53+
* @return a mnemonic for the license
54+
*/
55+
public String getKey() {
56+
return key;
57+
}
58+
59+
/**
60+
* @return the license name
61+
*/
62+
public String getName() {
63+
return name;
64+
}
65+
66+
/**
67+
* @return API URL of this object.
68+
*/
69+
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
70+
public URL getUrl() {
71+
return GitHub.parseURL(url);
72+
}
73+
74+
/**
75+
* Featured licenses are bold in the new repository drop-down
76+
*
77+
* @return True if the license is featured, false otherwise
78+
*/
79+
public Boolean isFeatured() {
80+
return featured;
81+
}
82+
83+
@Override
84+
public boolean equals(Object o) {
85+
if (this == o) return true;
86+
if (!(o instanceof GHLicenseBase)) return false;
87+
88+
GHLicenseBase that = (GHLicenseBase) o;
89+
90+
return getUrl().toString().equals(that.getUrl().toString());
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return getUrl().toString().hashCode();
96+
}
97+
98+
@Override
99+
public String toString() {
100+
return "GHLicenseBase{" +
101+
"key='" + key + '\'' +
102+
", name='" + name + '\'' +
103+
", url='" + url + '\'' +
104+
", featured=" + featured +
105+
'}';
106+
}
107+
}

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public class GHRepository extends GHObject {
6565

6666
private String description, homepage, name, full_name;
6767
private String html_url; // this is the UI
68+
/*
69+
* The license information makes use of the preview API.
70+
*
71+
* See: https://developer.github.com/v3/licenses/
72+
*/
73+
/**
74+
* The basic license details as returned from {@link GitHub#getRepository(String)}
75+
*/
76+
private GHLicenseBase license;
77+
6878
private String git_url, ssh_url, clone_url, svn_url, mirror_url;
6979
private GHUser owner; // not fully populated. beware.
7080
private boolean has_issues, has_wiki, fork, has_downloads;
@@ -839,6 +849,44 @@ protected void wrapUp(GHCommitComment[] page) {
839849
}
840850
};
841851
}
852+
* Gets the basic license details for the repository.
853+
* <p>
854+
* This is a preview item and requires you to use {@link org.kohsuke.github.extras.PreviewHttpConnector}
855+
* <p>
856+
* Warning: Only returns the basic license details. Use {@link GitHub#getLicense(String)}
857+
* to get the full license information (hint: pass it {@link GHLicenseBase#getKey()}).
858+
*
859+
* @throws IOException as usual but also if you don't use the preview connector
860+
*/
861+
public GHLicenseBase getLicense() {
862+
return license;
863+
}
864+
865+
/**
866+
* Access the full license details - makes an additional API call
867+
* <p>
868+
* This is a preview item and requires you to use {@link org.kohsuke.github.extras.PreviewHttpConnector}
869+
*
870+
* @return the license details
871+
* @throws IOException as usual but also if you don't use the preview connector
872+
*/
873+
public GHLicense getFullLicense() throws IOException {
874+
return root.getLicense(license.getKey());
875+
}
876+
877+
/**
878+
* Retrieves the contents of the repository's license file - makes an additional API call
879+
* <p>
880+
* This is a preview item and requires you to use {@link org.kohsuke.github.extras.PreviewHttpConnector}
881+
*
882+
* @return details regarding the license contents
883+
* @throws IOException as usual but also if you don't use the preview connector
884+
*/
885+
public GHContent getLicenseContent() throws IOException {
886+
return root.retrieve().to(getApiTailUrl("license"), GHContent.class).wrap(this);
887+
}
888+
889+
/**
842890
843891
/**
844892
* Lists all the commit statues attached to the given commit, newer ones first.

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.fasterxml.jackson.databind.ObjectMapper;
5757
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
5858
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
59+
5960
import java.util.logging.Logger;
6061

6162
/**
@@ -339,6 +340,33 @@ public GHRepository getRepository(String name) throws IOException {
339340
String[] tokens = name.split("/");
340341
return retrieve().to("/repos/" + tokens[0] + '/' + tokens[1], GHRepository.class).wrap(this);
341342
}
343+
* Returns a list of popular open source licenses
344+
*
345+
* WARNING: This uses a PREVIEW API - you must use {@link org.kohsuke.github.extras.PreviewHttpConnector}
346+
*
347+
* @see <a href="https://developer.github.com/v3/licenses/">GitHub API - Licenses</a>
348+
*
349+
* @return a list of popular open source licenses
350+
* @throws IOException if the HttpConnector doesn't pass in the preview header or other IO issue
351+
*/
352+
public List<GHLicenseBase> listLicenses() throws IOException {
353+
return Arrays.asList(retrieve().to("/licenses", GHLicenseBase[].class));
354+
}
355+
356+
/**
357+
* Returns the full details for a license
358+
*
359+
* WARNING: This uses a PREVIEW API - you must use {@link org.kohsuke.github.extras.PreviewHttpConnector}
360+
*
361+
* @param key The license key provided from the API
362+
* @return The license details
363+
* @throws IOException
364+
*/
365+
public GHLicense getLicense(String key) throws IOException {
366+
return retrieve().to("/licenses/" + key, GHLicense.class);
367+
}
368+
369+
/**
342370
343371
/**
344372
* This method returns a shallowly populated organizations.
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright $year slavinson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.kohsuke.github.extras;
18+
19+
import org.kohsuke.github.HttpConnector;
20+
21+
import java.io.IOException;
22+
import java.net.HttpURLConnection;
23+
import java.net.URL;
24+
25+
public class PreviewHttpConnector implements HttpConnector {
26+
private final HttpConnector base;
27+
private final int readTimeout, connectTimeout;
28+
29+
/**
30+
* @param connectTimeout HTTP connection timeout in milliseconds
31+
* @param readTimeout HTTP read timeout in milliseconds
32+
*/
33+
public PreviewHttpConnector(HttpConnector base, int connectTimeout, int readTimeout) {
34+
this.base = base;
35+
this.connectTimeout = connectTimeout;
36+
this.readTimeout = readTimeout;
37+
}
38+
39+
public PreviewHttpConnector(HttpConnector base, int timeout) {
40+
this(base, timeout, timeout);
41+
}
42+
43+
public PreviewHttpConnector(HttpConnector base) {
44+
this(base, ImpatientHttpConnector.CONNECT_TIMEOUT, ImpatientHttpConnector.READ_TIMEOUT);
45+
}
46+
47+
public PreviewHttpConnector() {
48+
this(new HttpConnector() {
49+
public HttpURLConnection connect(URL url) throws IOException {
50+
return (HttpURLConnection) url.openConnection();
51+
}
52+
}, ImpatientHttpConnector.CONNECT_TIMEOUT, ImpatientHttpConnector.READ_TIMEOUT);
53+
}
54+
55+
public HttpURLConnection connect(URL url) throws IOException {
56+
HttpURLConnection con = base.connect(url);
57+
con.setConnectTimeout(connectTimeout);
58+
con.setReadTimeout(readTimeout);
59+
con.addRequestProperty("Accept", PREVIEW_MEDIA_TYPE);
60+
return con;
61+
}
62+
63+
/**
64+
* Default connection timeout in milliseconds
65+
*/
66+
public static final String PREVIEW_MEDIA_TYPE = "application/vnd.github.drax-preview+json";
67+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.