11package pl .simplemethod .codebin .githubOauth ;
22
33
4+ import org .json .simple .parser .JSONParser ;
5+ import org .json .simple .parser .ParseException ;
46import org .springframework .beans .factory .annotation .Autowired ;
57import org .springframework .http .HttpHeaders ;
68import org .springframework .http .HttpStatus ;
@@ -31,7 +33,7 @@ public class GithubRestController {
3133 */
3234 @ GetMapping ("/repos/{userName}/{repoName}/minimal" )
3335 public @ ResponseBody
34- ResponseEntity getReposCloneWithMinimalInfo (@ CookieValue ("token" ) String token ,@ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
36+ ResponseEntity getReposCloneWithMinimalInfo (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
3537 HttpHeaders headers = new HttpHeaders ();
3638 headers .setContentType (MediaType .APPLICATION_JSON );
3739 return new ResponseEntity <>(githubClient .getCloneReposMinimalInfo (token , username , repos ).toString (), headers , HttpStatus .valueOf (200 ));
@@ -47,22 +49,23 @@ ResponseEntity getReposCloneWithMinimalInfo(@CookieValue("token") String token,@
4749 */
4850 @ GetMapping ("/repos/{userName}/{repoName}" )
4951 public @ ResponseBody
50- ResponseEntity getReposInfo (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
52+ ResponseEntity getReposInfo (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
5153 HttpHeaders headers = new HttpHeaders ();
5254 headers .setContentType (MediaType .APPLICATION_JSON );
5355 return new ResponseEntity <>(githubClient .getReposInfo (token , username , repos ).toString (), headers , HttpStatus .valueOf (200 ));
5456 }
5557
5658 /**
5759 * Statistics contributors in a repository. Does not work for private repositories and requires a double request for working. https://developer.github.com/v3/repos/statistics/
58- * @param token Token for authorization
59- * @param username User name
60- * @param repos Name of the repository
60+ *
61+ * @param token Token for authorization
62+ * @param username User name
63+ * @param repos Name of the repository
6164 * @return Json object with data
6265 */
6366 @ GetMapping ("/repos/{userName}/{repoName}/contributors" )
6467 public @ ResponseBody
65- ResponseEntity getReposContributorsStatistics (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
68+ ResponseEntity getReposContributorsStatistics (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username , @ PathVariable (value = "repoName" ) String repos ) {
6669 HttpHeaders headers = new HttpHeaders ();
6770 headers .setContentType (MediaType .APPLICATION_JSON );
6871 return new ResponseEntity <>(githubClient .getReposContributorsStatistics (token , username , repos ).toString (), headers , HttpStatus .valueOf (200 ));
@@ -95,53 +98,49 @@ ResponseEntity getInfoAboutOwner(@CookieValue("token") String token) {
9598 headers .setContentType (MediaType .APPLICATION_JSON );
9699 return new ResponseEntity <>(githubClient .getUserInfo (token ).toString (), headers , HttpStatus .valueOf (200 ));
97100 }
101+
98102 /**
99103 * Returns JSON with user information
100104 *
101- * @param token Token for authorization
102- * @param username Username
105+ * @param token Token for authorization
106+ * @param username Username
103107 * @return Json with data
104108 */
105109 @ GetMapping (value = "/users/{userName}" )
106110 public @ ResponseBody
107- ResponseEntity getInfoAboutUser (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username ) {
111+ ResponseEntity getInfoAboutUser (@ CookieValue ("token" ) String token , @ PathVariable (value = "userName" ) String username ) {
108112 HttpHeaders headers = new HttpHeaders ();
109113 headers .setContentType (MediaType .APPLICATION_JSON );
110- return new ResponseEntity <>(githubClient .getUserInfo (token ,username ).toString (), headers , HttpStatus .valueOf (200 ));
114+ return new ResponseEntity <>(githubClient .getUserInfo (token , username ).toString (), headers , HttpStatus .valueOf (200 ));
111115 }
112116
113117
114-
115118 /**
116119 * Method for verifying if a profile exists
117- * @param token Token to github authorize
120+ *
121+ * @param token Token to github authorize
118122 * @return Json object
119123 */
120- @ GetMapping ("/checktoken" )
124+ @ GetMapping ("/user/ checktoken" )
121125 public @ ResponseBody
122126 ResponseEntity checkToken (@ RequestParam ("token" ) String token ) {
123127 HttpHeaders headers = new HttpHeaders ();
124128 org .json .JSONObject body = new org .json .JSONObject ();
125129 try {
126130 Users users = usersRepository .getFirstByToken (token );
127131 if (users == null ) {
128- body .put ("token" ,"errr" );
132+ body .put ("token" , "errr" );
129133 return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (404 ));
130- }
131- else
132- {
133- org .json .JSONObject oauth = githubClient .getUserInfo (token );
134- try {
134+ } else {
135+ org .json .JSONObject oauth = githubClient .getUserInfo (token );
136+ try {
135137 oauth .get ("login" );
136- if ( oauth .get ("login" )!=null )
137- {
138- body .put ("token" ,users );
138+ if (oauth .get ("login" ) != null ) {
139+ body .put ("token" , users );
139140 return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (200 ));
140141 }
141- }
142- catch (org .json .JSONException e )
143- {
144- body .put ("token" ,"errr" );
142+ } catch (org .json .JSONException e ) {
143+ body .put ("token" , "errr" );
145144 return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (404 ));
146145 }
147146
@@ -153,4 +152,49 @@ ResponseEntity checkToken(@RequestParam("token") String token) {
153152 }
154153 return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (404 ));
155154 }
155+
156+ @ GetMapping ("/user/repos/public" )
157+ public @ ResponseBody
158+ ResponseEntity getpublicrepos (@ CookieValue ("token" ) String token ) {
159+ HttpHeaders headers = new HttpHeaders ();
160+ JSONParser parser = new JSONParser ();
161+ org .json .JSONObject body = new org .json .JSONObject ();
162+ org .json .simple .JSONArray result = new org .json .simple .JSONArray ();
163+ Object obj = null ;
164+ try {
165+ Users users = usersRepository .getFirstByToken (token );
166+ if (users == null ) {
167+ body .put ("token" , "errr" );
168+ return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (404 ));
169+ } else {
170+ try {
171+ obj = parser .parse (githubClient .getUserRepos (token ));
172+ org .json .simple .JSONArray jsonArray = (org .json .simple .JSONArray ) obj ;
173+ jsonArray .forEach (item -> {
174+ org .json .simple .JSONObject obj1 = (org .json .simple .JSONObject ) item ;
175+
176+ if (obj1 .get ("private" ).toString ().equals ("false" )) {
177+ org .json .simple .JSONObject helper = new org .json .simple .JSONObject ();
178+ helper .put ("language" , obj1 .get ("language" ));
179+ helper .put ("id" , obj1 .get ("id" ));
180+ helper .put ("license" , obj1 .get ("license" ));
181+ helper .put ("name" , obj1 .get ("name" ));
182+ helper .put ("git_url" , obj1 .get ("git_url" ));
183+ helper .put ("html_url" , obj1 .get ("html_url" ));
184+ helper .put ("description" , obj1 .get ("description" ));
185+ helper .put ("created_at" , obj1 .get ("created_at" ));
186+ result .add (helper );
187+ }
188+ });
189+ return new ResponseEntity <>(result .toString (), headers , HttpStatus .valueOf (200 ));
190+ } catch (NullPointerException | ParseException | org .json .JSONException e ) {
191+ body .put ("token" , "errr" );
192+ return new ResponseEntity <>(body .toString (), headers , HttpStatus .valueOf (404 ));
193+ }
194+ }
195+ } catch (org .json .JSONException e ) {
196+ return new ResponseEntity <>(e , headers , HttpStatus .valueOf (404 ));
197+
198+ }
199+ }
156200}
0 commit comments