1313import org .json .JSONArray ;
1414import org .json .JSONObject ;
1515
16+ import com .prenda .Mode ;
17+
1618public class GithubIssue {
17-
19+
1820 private static Logger log = Logger .getLogger (GithubIssue .class );
19-
20- public int create (String title , String body , String username , String repo , String [] labels , String [] assignees , String token ) {
21+
22+ public int create (String title , String body , String username , String repo , String [] labels , String [] assignees ,
23+ int tokenType , String token ) {
2124 int issue = 0 ;
22- if (exists (title ,username ,repo )) {
23- return issue ;
25+ try {
26+ if (!exists (title , username , repo )) {
27+ JSONObject json = new JSONObject ();
28+ json .put ("title" , title );
29+ json .put ("body" , body );
30+ JSONArray jsonArray = new JSONArray ();
31+ for (String label : labels ) {
32+ jsonArray .put (label );
33+ }
34+ json .put ("labels" , jsonArray );
35+ jsonArray = new JSONArray ();
36+ for (String assignee : assignees ) {
37+ jsonArray .put (assignee );
38+ }
39+ json .put ("assignees" , jsonArray );
40+ log .info (json .toString (3 ));
41+ byte [] postData = json .toString ().getBytes (StandardCharsets .UTF_8 );
42+ int postDataLength = postData .length ;
43+ String request = "https://api.github.com/repos/" + username + "/" + repo + "/issues" ;
44+ URL url = new URL (request );
45+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
46+ conn .setDoOutput (true );
47+ conn .setInstanceFollowRedirects (false );
48+ conn .setRequestMethod ("POST" );
49+ conn .setRequestProperty ("charset" , "utf-8" );
50+ conn .setRequestProperty ("Content-Length" , Integer .toString (postDataLength ));
51+ conn .setUseCaches (false );
52+ conn .setRequestProperty ("Content-Type" , "application/json" );
53+ if (tokenType == Mode .PAT ) { // Personal Access Token only
54+ String encoded = Base64 .getEncoder ()
55+ .encodeToString ((username + ":" + token ).getBytes (StandardCharsets .UTF_8 ));
56+ conn .setRequestProperty ("Authorization" , "Basic " + encoded );
57+ } else if (tokenType == Mode .JWT ) { // JSON Web Token
58+ //if (authenticate()) {
59+ token = getToken (120193 );
60+ conn .setRequestProperty ("Authorization" , "Token " + token );
61+ //}
62+ }
63+ DataOutputStream wr = new DataOutputStream (conn .getOutputStream ());
64+ wr .write (postData );
65+ int responseCode = conn .getResponseCode ();
66+ log .info ("Response Code : " + responseCode );
67+ BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
68+ String inputLine ;
69+ StringBuffer response = new StringBuffer ();
70+ while ((inputLine = in .readLine ()) != null ) {
71+ response .append (inputLine );
72+ }
73+ in .close ();
74+ log .info (response .toString ());
75+ if (responseCode == 201 ) {
76+ JSONObject myResponse = new JSONObject (response .toString ());
77+ issue = myResponse .getInt ("number" );
78+ log .info ("issue: " + issue );
79+ }
80+ }
81+ } catch (Exception e ) {
82+ e .printStackTrace ();
2483 }
84+ return issue ;
85+ }
86+
87+ private String getToken (Integer id ) {
88+ String token = "" ;
2589 try {
26- JSONObject json = new JSONObject ();
27- json .put ("title" , title );
28- json .put ("body" , body );
29- JSONArray jsonArray = new JSONArray ();
30- for (String label : labels ) {
31- jsonArray .put (label );
90+ String request = "https://api.github.com/installations/" + id + "/access_tokens" ;
91+ log .info (request );
92+ URL url = new URL (request );
93+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
94+ conn .setInstanceFollowRedirects (false );
95+ conn .setRequestMethod ("POST" );
96+ conn .setRequestProperty ("Accept" , "application/vnd.github.machine-man-preview+json" );
97+ conn .setRequestProperty ("Authorization" , "Bearer " + KeyUtil .getJws ());
98+ int responseCode = conn .getResponseCode ();
99+ log .info ("Response Code : " + responseCode );
100+ BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
101+ String inputLine ;
102+ StringBuffer response = new StringBuffer ();
103+ while ((inputLine = in .readLine ()) != null ) {
104+ response .append (inputLine );
32105 }
33- json .put ("labels" , jsonArray );
34- jsonArray = new JSONArray ();
35- for (String assignee : assignees ) {
36- jsonArray .put (assignee );
106+ in .close ();
107+ log .info (response .toString ());
108+ if (responseCode == 201 ) {
109+ JSONObject myResponse = new JSONObject (response .toString ());
110+ token = myResponse .getString ("token" );
111+ log .info ("token: " + token );
37112 }
38- json .put ("assignees" , jsonArray );
39- log .info (json .toString (3 ));
40- byte [] postData = json .toString ().getBytes (StandardCharsets .UTF_8 );
41- int postDataLength = postData .length ;
42- String request = "https://api.github.com/repos/" + username + "/" + repo + "/issues" ;
113+ } catch (Exception e ) {
114+ e .printStackTrace ();
115+ }
116+ return token ;
117+ }
118+
119+ protected boolean authenticate () {
120+ boolean auth = false ;
121+ try {
122+ String request = "https://api.github.com/app" ;
123+ log .info (request );
43124 URL url = new URL (request );
44125 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
45- conn .setDoOutput (true );
46126 conn .setInstanceFollowRedirects (false );
47- conn .setRequestMethod ("POST" );
48- conn .setRequestProperty ("Content-Type" , "application/json" );
49- conn .setRequestProperty ("charset" , "utf-8" );
50- conn .setRequestProperty ("Content-Length" , Integer .toString (postDataLength ));
51- conn .setUseCaches (false );
52- //Personal Access Token only
53- String encoded = Base64 .getEncoder ()
54- .encodeToString ((username + ":" + token ).getBytes (StandardCharsets .UTF_8 ));
55- conn .setRequestProperty ("Authorization" , "Basic " + encoded );
56- /*
57- KeyUtil ku = new KeyUtil();
58- conn.setRequestProperty("Authorization", "Bearer " + ku.getJws());
59- */
60- DataOutputStream wr = new DataOutputStream (conn .getOutputStream ());
61- wr .write (postData );
127+ conn .setRequestMethod ("GET" );
128+ conn .setRequestProperty ("Accept" , "application/vnd.github.machine-man-preview+json" );
129+ conn .setRequestProperty ("Authorization" , "Bearer " + KeyUtil .getJws ());
62130 int responseCode = conn .getResponseCode ();
63131 log .info ("Response Code : " + responseCode );
64132 BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
@@ -69,19 +137,20 @@ public int create(String title, String body, String username, String repo, Strin
69137 }
70138 in .close ();
71139 log .info (response .toString ());
72- JSONObject myResponse = new JSONObject ( response . toString ());
73- issue = myResponse . getInt ( "number" ) ;
74- log . info ( "issue: " + issue );
140+ if ( responseCode == 200 ) {
141+ auth = true ;
142+ }
75143 } catch (Exception e ) {
76- log . info ( e . getMessage () );
144+ e . printStackTrace ( );
77145 }
78- return issue ;
146+ return auth ;
79147 }
80148
81149 protected boolean exists (String title , String username , String repo ) {
82- boolean found = false ;
150+ boolean found = false ;
83151 try {
84- String request = "https://api.github.com/search/issues?q=" +URLEncoder .encode (title ,"UTF-8" )+"+type:issue+in:title+repo:" +username +"%2f" +repo ;
152+ String request = "https://api.github.com/search/issues?q=" + URLEncoder .encode (title , "UTF-8" )
153+ + "+type:issue+in:title+repo:" + username + "%2f" + repo ;
85154 log .info (request );
86155 URL url = new URL (request );
87156 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
@@ -97,15 +166,50 @@ protected boolean exists(String title, String username, String repo) {
97166 }
98167 in .close ();
99168 log .info (response .toString ());
100- JSONObject myResponse = new JSONObject (response .toString ());
101- int count = myResponse .getInt ("total_count" );
102- log .info ("total_count: " + count );
103- if (count >0 ) {
104- found = true ;
169+ if (responseCode == 200 ) {
170+ JSONObject myResponse = new JSONObject (response .toString ());
171+ int count = myResponse .getInt ("total_count" );
172+ log .info ("total_count: " + count );
173+ if (count > 0 ) {
174+ found = true ;
175+ }
105176 }
106177 } catch (Exception e ) {
107178 e .printStackTrace ();
108179 }
109180 return found ;
110181 }
182+
183+ protected String getATitle (String username , String repo ) {
184+ String title = "" ;
185+ try {
186+ String request = "https://api.github.com/repos/" +username +"/" +repo +"/issues?state=all" ;
187+ log .info (request );
188+ URL url = new URL (request );
189+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
190+ conn .setInstanceFollowRedirects (false );
191+ conn .setRequestMethod ("GET" );
192+ int responseCode = conn .getResponseCode ();
193+ log .info ("Response Code : " + responseCode );
194+ BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
195+ String inputLine ;
196+ StringBuffer response = new StringBuffer ();
197+ while ((inputLine = in .readLine ()) != null ) {
198+ response .append (inputLine );
199+ }
200+ in .close ();
201+ log .info (response .toString ());
202+ if (responseCode == 200 ) {
203+ JSONArray myResponse = new JSONArray (response .toString ());
204+ JSONObject object = myResponse .getJSONObject (1 );
205+ if (object .length ()>0 ) {
206+ title = object .getString ("title" );
207+ log .info ("title: " + title );
208+ }
209+ }
210+ } catch (Exception e ) {
211+ e .printStackTrace ();
212+ }
213+ return title ;
214+ }
111215}
0 commit comments