summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java
blob: b8cf537f7709a7160be09bdcb83b51cedb4ff3b9 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.github.api;

import com.google.gson.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.net.HttpConfigurable;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.exceptions.*;
import org.jetbrains.plugins.github.util.GithubAuthData;
import org.jetbrains.plugins.github.util.GithubSettings;
import org.jetbrains.plugins.github.util.GithubUrlUtil;
import org.jetbrains.plugins.github.util.GithubUtil;
import sun.security.validator.ValidatorException;

import javax.net.ssl.SSLHandshakeException;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URLEncoder;
import java.util.*;
import java.util.List;

/**
 * @author Kirill Likhodedov
 */
public class GithubApiUtil {

  public static final String DEFAULT_GITHUB_HOST = "github.com";

  private static final String PER_PAGE = "per_page=100";
  private static final Logger LOG = GithubUtil.LOG;

  private static final Header ACCEPT_V3_JSON_HTML_MARKUP = new Header("Accept", "application/vnd.github.v3.html+json");
  private static final Header ACCEPT_V3_JSON = new Header("Accept", "application/vnd.github.v3+json");

  @NotNull private static final Gson gson = initGson();

  private static Gson initGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
  }

  private enum HttpVerb {
    GET, POST, DELETE, HEAD, PATCH
  }

  @Nullable
  private static JsonElement postRequest(@NotNull GithubAuthData auth,
                                         @NotNull String path,
                                         @Nullable String requestBody,
                                         @NotNull Header... headers) throws IOException {
    return request(auth, path, requestBody, Arrays.asList(headers), HttpVerb.POST).getJsonElement();
  }

  @Nullable
  private static JsonElement patchRequest(@NotNull GithubAuthData auth,
                                          @NotNull String path,
                                          @Nullable String requestBody,
                                          @NotNull Header... headers) throws IOException {
    return request(auth, path, requestBody, Arrays.asList(headers), HttpVerb.PATCH).getJsonElement();
  }

  @Nullable
  private static JsonElement deleteRequest(@NotNull GithubAuthData auth, @NotNull String path, @NotNull Header... headers)
    throws IOException {
    return request(auth, path, null, Arrays.asList(headers), HttpVerb.DELETE).getJsonElement();
  }

  @Nullable
  private static JsonElement getRequest(@NotNull GithubAuthData auth, @NotNull String path, @NotNull Header... headers) throws IOException {
    return request(auth, path, null, Arrays.asList(headers), HttpVerb.GET).getJsonElement();
  }

  @NotNull
  private static ResponsePage request(@NotNull GithubAuthData auth,
                                      @NotNull String path,
                                      @Nullable String requestBody,
                                      @NotNull Collection<Header> headers,
                                      @NotNull HttpVerb verb) throws IOException {
    if (EventQueue.isDispatchThread() && !ApplicationManager.getApplication().isUnitTestMode()) {
      LOG.warn("Network operation in EDT"); // TODO: fix
    }

    HttpMethod method = null;
    try {
      String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + path;
      method = doREST(auth, uri, requestBody, headers, verb);

      checkStatusCode(method, requestBody);

      InputStream resp = method.getResponseBodyAsStream();
      if (resp == null) {
        return new ResponsePage();
      }

      JsonElement ret = parseResponse(resp);
      if (ret.isJsonNull()) {
        return new ResponsePage();
      }

      Header header = method.getResponseHeader("Link");
      if (header != null) {
        String value = header.getValue();
        int end = value.indexOf(">; rel=\"next\"");
        int begin = value.lastIndexOf('<', end);
        if (begin >= 0 && end >= 0) {
          String newPath = GithubUrlUtil.removeProtocolPrefix(value.substring(begin + 1, end));
          int index = newPath.indexOf('/');

          return new ResponsePage(ret, newPath.substring(index));
        }
      }

      return new ResponsePage(ret);
    }
    finally {
      if (method != null) {
        method.releaseConnection();
      }
    }
  }

  @NotNull
  private static HttpMethod doREST(@NotNull final GithubAuthData auth,
                                   @NotNull final String uri,
                                   @Nullable final String requestBody,
                                   @NotNull final Collection<Header> headers,
                                   @NotNull final HttpVerb verb) throws IOException {
    HttpClient client = getHttpClient(auth.getBasicAuth(), auth.isUseProxy());
    HttpMethod method;
    switch (verb) {
      case POST:
        method = new PostMethod(uri);
        if (requestBody != null) {
          ((PostMethod)method).setRequestEntity(new StringRequestEntity(requestBody, "application/json", "UTF-8"));
        }
        break;
      case PATCH:
        method = new PostMethod(uri) { // TODO: httpclient 4.x
          @Override
          public String getName() {
            return "PATCH";
          }
        };
        if (requestBody != null) {
          ((PostMethod)method).setRequestEntity(new StringRequestEntity(requestBody, "application/json", "UTF-8"));
        }
        break;
      case GET:
        method = new GetMethod(uri);
        break;
      case DELETE:
        method = new DeleteMethod(uri);
        break;
      case HEAD:
        method = new HeadMethod(uri);
        break;
      default:
        throw new IllegalStateException("Wrong HttpVerb: unknown method: " + verb.toString());
    }

    GithubAuthData.TokenAuth tokenAuth = auth.getTokenAuth();
    if (tokenAuth != null) {
      method.addRequestHeader("Authorization", "token " + tokenAuth.getToken());
    }
    GithubAuthData.BasicAuth basicAuth = auth.getBasicAuth();
    if (basicAuth != null && basicAuth.getCode() != null) {
      method.addRequestHeader("X-GitHub-OTP", basicAuth.getCode());
    }
    for (Header header : headers) {
      method.addRequestHeader(header);
    }

    try {
      client.executeMethod(method);
    }
    catch (SSLHandshakeException e) { // User canceled operation from CertificateManager
      if (e.getCause() instanceof ValidatorException) {
        LOG.info("Host SSL certificate is not trusted", e);
        throw new GithubOperationCanceledException("Host SSL certificate is not trusted", e);
      }
      throw e;
    }
    return method;
  }

  @NotNull
  private static HttpClient getHttpClient(@Nullable GithubAuthData.BasicAuth basicAuth, boolean useProxy) {
    int timeout = GithubSettings.getInstance().getConnectionTimeout();
    final HttpClient client = new HttpClient();
    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    params.setConnectionTimeout(timeout); //set connection timeout (how long it takes to connect to remote host)
    params.setSoTimeout(timeout); //set socket timeout (how long it takes to retrieve data from remote host)

    client.getParams().setContentCharset("UTF-8");
    // Configure proxySettings if it is required
    final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
    if (useProxy && proxySettings.USE_HTTP_PROXY && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
      client.getHostConfiguration().setProxy(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT);
      if (proxySettings.PROXY_AUTHENTICATION) {
        client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxySettings.PROXY_LOGIN,
                                                                                             proxySettings.getPlainProxyPassword()));
      }
    }
    if (basicAuth != null) {
      client.getParams().setCredentialCharset("UTF-8");
      client.getParams().setAuthenticationPreemptive(true);
      client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuth.getLogin(), basicAuth.getPassword()));
    }
    return client;
  }

  private static void checkStatusCode(@NotNull HttpMethod method, @Nullable String body) throws IOException {
    int code = method.getStatusCode();
    switch (code) {
      case HttpStatus.SC_OK:
      case HttpStatus.SC_CREATED:
      case HttpStatus.SC_ACCEPTED:
      case HttpStatus.SC_NO_CONTENT:
        return;
      case HttpStatus.SC_UNAUTHORIZED:
      case HttpStatus.SC_PAYMENT_REQUIRED:
      case HttpStatus.SC_FORBIDDEN:
        String message = getErrorMessage(method);

        Header headerOTP = method.getResponseHeader("X-GitHub-OTP");
        if (headerOTP != null) {
          if (headerOTP.getValue().startsWith("required")) {
            throw new GithubTwoFactorAuthenticationException(message);
          }
        }

        if (message.contains("API rate limit exceeded")) {
          throw new GithubRateLimitExceededException(message);
        }

        throw new GithubAuthenticationException("Request response: " + message);
      case HttpStatus.SC_BAD_REQUEST:
      case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        if (body != null) {
          LOG.info(body);
        }
        throw new GithubStatusCodeException(code + ": " + getErrorMessage(method), code);
      default:
        throw new GithubStatusCodeException(code + ": " + getErrorMessage(method), code);
    }
  }

  @NotNull
  private static String getErrorMessage(@NotNull HttpMethod method) {
    try {
      InputStream resp = method.getResponseBodyAsStream();
      if (resp != null) {
        GithubErrorMessageRaw error = fromJson(parseResponse(resp), GithubErrorMessageRaw.class);
        return method.getStatusText() + " - " + error.getMessage();
      }
    }
    catch (IOException e) {
      LOG.info(e);
    }
    return method.getStatusText();
  }

  @NotNull
  private static JsonElement parseResponse(@NotNull InputStream githubResponse) throws IOException {
    Reader reader = new InputStreamReader(githubResponse, "UTF-8");
    try {
      return new JsonParser().parse(reader);
    }
    catch (JsonParseException jse) {
      throw new GithubJsonException("Couldn't parse GitHub response", jse);
    }
    finally {
      reader.close();
    }
  }

  private static class ResponsePage {
    @Nullable private final JsonElement response;
    @Nullable private final String nextPage;

    public ResponsePage() {
      this(null, null);
    }

    public ResponsePage(@Nullable JsonElement response) {
      this(response, null);
    }

    public ResponsePage(@Nullable JsonElement response, @Nullable String next) {
      this.response = response;
      this.nextPage = next;
    }

    @Nullable
    public JsonElement getJsonElement() {
      return response;
    }

    @Nullable
    public String getNextPage() {
      return nextPage;
    }
  }

   /*
   * Json API
   */

  static <Raw extends DataConstructor, Result> Result createDataFromRaw(@NotNull Raw rawObject, @NotNull Class<Result> resultClass)
    throws GithubJsonException {
    try {
      return rawObject.create(resultClass);
    }
    catch (Exception e) {
      throw new GithubJsonException("Json parse error", e);
    }
  }

  public static class PagedRequest<T> {
    @Nullable private String myNextPage;
    @NotNull private final Collection<Header> myHeaders;
    @NotNull private final Class<T> myResult;
    @NotNull private final Class<? extends DataConstructor[]> myRawArray;

    @SuppressWarnings("NullableProblems")
    public PagedRequest(@NotNull String path,
                        @NotNull Class<T> result,
                        @NotNull Class<? extends DataConstructor[]> rawArray,
                        @NotNull Header... headers) {
      myNextPage = path;
      myResult = result;
      myRawArray = rawArray;
      myHeaders = Arrays.asList(headers);
    }

    @NotNull
    public List<T> next(@NotNull GithubAuthData auth) throws IOException {
      if (myNextPage == null) {
        throw new NoSuchElementException();
      }

      String page = myNextPage;
      myNextPage = null;

      ResponsePage response = request(auth, page, null, myHeaders, HttpVerb.GET);

      if (response.getJsonElement() == null) {
        throw new HttpException("Empty response");
      }

      if (!response.getJsonElement().isJsonArray()) {
        throw new GithubJsonException("Wrong json type: expected JsonArray", new Exception(response.getJsonElement().toString()));
      }

      myNextPage = response.getNextPage();

      List<T> result = new ArrayList<T>();
      for (DataConstructor raw : fromJson(response.getJsonElement().getAsJsonArray(), myRawArray)) {
        result.add(createDataFromRaw(raw, myResult));
      }
      return result;
    }

    public boolean hasNext() {
      return myNextPage != null;
    }

    @NotNull
    public List<T> getAll(@NotNull GithubAuthData auth) throws IOException {
      List<T> result = new ArrayList<T>();
      while (hasNext()) {
        result.addAll(next(auth));
      }
      return result;
    }
  }

  @NotNull
  private static <T> T fromJson(@Nullable JsonElement json, @NotNull Class<T> classT) throws IOException {
    if (json == null) {
      throw new GithubJsonException("Unexpected empty response");
    }

    T res;
    try {
      //cast as workaround for early java 1.6 bug
      //noinspection RedundantCast
      res = (T)gson.fromJson(json, classT);
    }
    catch (ClassCastException e) {
      throw new GithubJsonException("Parse exception while converting JSON to object " + classT.toString(), e);
    }
    catch (JsonParseException e) {
      throw new GithubJsonException("Parse exception while converting JSON to object " + classT.toString(), e);
    }
    if (res == null) {
      throw new GithubJsonException("Empty Json response");
    }
    return res;
  }

   /*
   * Github API
   */

  public static void askForTwoFactorCodeSMS(@NotNull GithubAuthData auth) {
    try {
      postRequest(auth, "/authorizations", null, ACCEPT_V3_JSON);
    }
    catch (IOException e) {
      LOG.info(e);
    }
  }

  @NotNull
  public static Collection<String> getTokenScopes(@NotNull GithubAuthData auth) throws IOException {
    HttpMethod method = null;
    try {
      String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + "/user";
      method = doREST(auth, uri, null, Collections.<Header>emptyList(), HttpVerb.HEAD);

      checkStatusCode(method, null);

      Header header = method.getResponseHeader("X-OAuth-Scopes");
      if (header == null) {
        throw new HttpException("No scopes header");
      }

      Collection<String> scopes = new ArrayList<String>();
      for (HeaderElement elem : header.getElements()) {
        scopes.add(elem.getName());
      }
      return scopes;
    }
    finally {
      if (method != null) {
        method.releaseConnection();
      }
    }
  }

  @NotNull
  public static String getScopedToken(@NotNull GithubAuthData auth, @NotNull Collection<String> scopes, @NotNull String note)
    throws IOException {
    GithubAuthorization token = findToken(auth, note);
    if (token == null) {
      return getNewScopedToken(auth, scopes, note).getToken();
    }
    if (token.getScopes().containsAll(scopes)) {
      return token.getToken();
    }
    return updateTokenScopes(auth, token, scopes).getToken();
  }

  @NotNull
  private static GithubAuthorization updateTokenScopes(@NotNull GithubAuthData auth,
                                                       @NotNull GithubAuthorization token,
                                                       @NotNull Collection<String> scopes) throws IOException {
    try {
      String path = "/authorizations/" + token.getId();

      GithubAuthorizationUpdateRequest request = new GithubAuthorizationUpdateRequest(new ArrayList<String>(scopes));

      return createDataFromRaw(fromJson(patchRequest(auth, path, gson.toJson(request), ACCEPT_V3_JSON), GithubAuthorizationRaw.class),
                               GithubAuthorization.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't update token: scopes - " + scopes);
      throw e;
    }
  }

  @NotNull
  private static GithubAuthorization getNewScopedToken(@NotNull GithubAuthData auth,
                                                       @NotNull Collection<String> scopes,
                                                       @NotNull String note)
    throws IOException {
    try {
      String path = "/authorizations";

      GithubAuthorizationCreateRequest request = new GithubAuthorizationCreateRequest(new ArrayList<String>(scopes), note, null);

      return createDataFromRaw(fromJson(postRequest(auth, path, gson.toJson(request), ACCEPT_V3_JSON), GithubAuthorizationRaw.class),
                               GithubAuthorization.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't create token: scopes - " + scopes + " - note " + note);
      throw e;
    }
  }

  @Nullable
  private static GithubAuthorization findToken(@NotNull GithubAuthData auth, @NotNull String note) throws IOException {
    try {
      String path = "/authorizations";

      PagedRequest<GithubAuthorization> request =
        new PagedRequest<GithubAuthorization>(path, GithubAuthorization.class, GithubAuthorizationRaw[].class, ACCEPT_V3_JSON);

      List<GithubAuthorization> tokens = request.getAll(auth);

      for (GithubAuthorization token : tokens) {
        if (note.equals(token.getNote())) return token;
      }
      return null;
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get available tokens");
      throw e;
    }
  }

  @NotNull
  public static String getMasterToken(@NotNull GithubAuthData auth, @NotNull String note) throws IOException {
    // "repo" - read/write access to public/private repositories
    // "gist" - create/delete gists
    List<String> scopes = Arrays.asList("repo", "gist");

    return getScopedToken(auth, scopes, note);
  }

  @NotNull
  public static String getReadOnlyToken(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, @NotNull String note)
    throws IOException {
    GithubRepo repository = getDetailedRepoInfo(auth, user, repo);

    // TODO: use read-only token for private repos when it will be available
    List<String> scopes = repository.isPrivate() ? Collections.singletonList("repo") : Collections.<String>emptyList();

    return getScopedToken(auth, scopes, note);
  }

  @NotNull
  public static GithubUser getCurrentUser(@NotNull GithubAuthData auth) throws IOException {
    try {
      JsonElement result = getRequest(auth, "/user", ACCEPT_V3_JSON);
      return createDataFromRaw(fromJson(result, GithubUserRaw.class), GithubUser.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user info");
      throw e;
    }
  }

  @NotNull
  public static GithubUserDetailed getCurrentUserDetailed(@NotNull GithubAuthData auth) throws IOException {
    try {
      JsonElement result = getRequest(auth, "/user", ACCEPT_V3_JSON);
      return createDataFromRaw(fromJson(result, GithubUserRaw.class), GithubUserDetailed.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user info");
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepo> getUserRepos(@NotNull GithubAuthData auth) throws IOException {
    try {
      String path = "/user/repos?" + PER_PAGE;

      PagedRequest<GithubRepo> request = new PagedRequest<GithubRepo>(path, GithubRepo.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user repositories");
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepo> getUserRepos(@NotNull GithubAuthData auth, @NotNull String user) throws IOException {
    try {
      String path = "/users/" + user + "/repos?" + PER_PAGE;

      PagedRequest<GithubRepo> request = new PagedRequest<GithubRepo>(path, GithubRepo.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user repositories: " + user);
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepo> getAvailableRepos(@NotNull GithubAuthData auth) throws IOException {
    try {
      List<GithubRepo> repos = new ArrayList<GithubRepo>();

      repos.addAll(getUserRepos(auth));

      // We already can return something useful from getUserRepos, so let's ignore errors.
      // One of this may not exist in GitHub enterprise
      try {
        repos.addAll(getMembershipRepos(auth));
      }
      catch (GithubStatusCodeException ignore) {
      }
      try {
        repos.addAll(getWatchedRepos(auth));
      }
      catch (GithubStatusCodeException ignore) {
      }

      return repos;
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get available repositories");
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepoOrg> getMembershipRepos(@NotNull GithubAuthData auth) throws IOException {
    String orgsPath = "/user/orgs?" + PER_PAGE;
    PagedRequest<GithubOrg> orgsRequest = new PagedRequest<GithubOrg>(orgsPath, GithubOrg.class, GithubOrgRaw[].class);

    List<GithubRepoOrg> repos = new ArrayList<GithubRepoOrg>();
    for (GithubOrg org : orgsRequest.getAll(auth)) {
      String path = "/orgs/" + org.getLogin() + "/repos?type=member&" + PER_PAGE;
      PagedRequest<GithubRepoOrg> request =
        new PagedRequest<GithubRepoOrg>(path, GithubRepoOrg.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);
      repos.addAll(request.getAll(auth));
    }

    return repos;
  }

  @NotNull
  public static List<GithubRepo> getWatchedRepos(@NotNull GithubAuthData auth) throws IOException {
    String pathWatched = "/user/subscriptions?" + PER_PAGE;
    PagedRequest<GithubRepo> requestWatched =
      new PagedRequest<GithubRepo>(pathWatched, GithubRepo.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);
    return requestWatched.getAll(auth);
  }

  @NotNull
  public static GithubRepoDetailed getDetailedRepoInfo(@NotNull GithubAuthData auth, @NotNull String owner, @NotNull String name)
    throws IOException {
    try {
      final String request = "/repos/" + owner + "/" + name;

      JsonElement jsonObject = getRequest(auth, request, ACCEPT_V3_JSON);

      return createDataFromRaw(fromJson(jsonObject, GithubRepoRaw.class), GithubRepoDetailed.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get repository info: " + owner + "/" + name);
      throw e;
    }
  }

  public static void deleteGithubRepository(@NotNull GithubAuthData auth, @NotNull String username, @NotNull String repo)
    throws IOException {
    try {
      String path = "/repos/" + username + "/" + repo;
      deleteRequest(auth, path);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't delete repository: " + username + "/" + repo);
      throw e;
    }
  }

  public static void deleteGist(@NotNull GithubAuthData auth, @NotNull String id) throws IOException {
    try {
      String path = "/gists/" + id;
      deleteRequest(auth, path);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't delete gist: id - " + id);
      throw e;
    }
  }

  @NotNull
  public static GithubGist getGist(@NotNull GithubAuthData auth, @NotNull String id) throws IOException {
    try {
      String path = "/gists/" + id;
      JsonElement result = getRequest(auth, path, ACCEPT_V3_JSON);

      return createDataFromRaw(fromJson(result, GithubGistRaw.class), GithubGist.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get gist info: id " + id);
      throw e;
    }
  }

  @NotNull
  public static GithubGist createGist(@NotNull GithubAuthData auth,
                                      @NotNull List<GithubGist.FileContent> contents,
                                      @NotNull String description,
                                      boolean isPrivate) throws IOException {
    try {
      String request = gson.toJson(new GithubGistRequest(contents, description, !isPrivate));
      return createDataFromRaw(fromJson(postRequest(auth, "/gists", request, ACCEPT_V3_JSON), GithubGistRaw.class), GithubGist.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't create gist");
      throw e;
    }
  }

  @NotNull
  public static GithubPullRequest createPullRequest(@NotNull GithubAuthData auth,
                                                    @NotNull String user,
                                                    @NotNull String repo,
                                                    @NotNull String title,
                                                    @NotNull String description,
                                                    @NotNull String head,
                                                    @NotNull String base) throws IOException {
    try {
      String request = gson.toJson(new GithubPullRequestRequest(title, description, head, base));
      return createDataFromRaw(
        fromJson(postRequest(auth, "/repos/" + user + "/" + repo + "/pulls", request, ACCEPT_V3_JSON), GithubPullRequestRaw.class),
        GithubPullRequest.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't create pull request");
      throw e;
    }
  }

  @NotNull
  public static GithubRepo createRepo(@NotNull GithubAuthData auth, @NotNull String name, @NotNull String description, boolean isPrivate)
    throws IOException {
    try {
      String path = "/user/repos";

      GithubRepoRequest request = new GithubRepoRequest(name, description, isPrivate);

      return createDataFromRaw(fromJson(postRequest(auth, path, gson.toJson(request), ACCEPT_V3_JSON), GithubRepoRaw.class),
                               GithubRepo.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't create repository: " + name);
      throw e;
    }
  }

  /*
   * Open issues only
   */
  @NotNull
  public static List<GithubIssue> getIssuesAssigned(@NotNull GithubAuthData auth,
                                                    @NotNull String user,
                                                    @NotNull String repo,
                                                    @Nullable String assigned,
                                                    int max,
                                                    boolean withClosed) throws IOException {
    try {
      String state = "state=" + (withClosed ? "all" : "open");
      String path;
      if (StringUtil.isEmptyOrSpaces(assigned)) {
        path = "/repos/" + user + "/" + repo + "/issues?" + PER_PAGE + "&" + state;
      }
      else {
        path = "/repos/" + user + "/" + repo + "/issues?assignee=" + assigned + "&" + PER_PAGE + "&" + state;
      }

      PagedRequest<GithubIssue> request = new PagedRequest<GithubIssue>(path, GithubIssue.class, GithubIssueRaw[].class, ACCEPT_V3_JSON);

      List<GithubIssue> result = new ArrayList<GithubIssue>();
      while (request.hasNext() && max > result.size()) {
        result.addAll(request.next(auth));
      }
      return result;
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get assigned issues: " + user + "/" + repo + " - " + assigned);
      throw e;
    }
  }

  @NotNull
  /*
   * All issues - open and closed
   */
  public static List<GithubIssue> getIssuesQueried(@NotNull GithubAuthData auth,
                                                   @NotNull String user,
                                                   @NotNull String repo,
                                                   @Nullable String query,
                                                   boolean withClosed) throws IOException {
    try {
      String state = withClosed ? "" : " state:open";
      query = URLEncoder.encode("repo:" + user + "/" + repo + " " + query + state, "UTF-8");
      String path = "/search/issues?q=" + query;

      //TODO: Use bodyHtml for issues - GitHub does not support this feature for SearchApi yet
      JsonElement result = getRequest(auth, path, ACCEPT_V3_JSON);

      return createDataFromRaw(fromJson(result, GithubIssuesSearchResultRaw.class), GithubIssuesSearchResult.class).getIssues();
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get queried issues: " + user + "/" + repo + " - " + query);
      throw e;
    }
  }

  @NotNull
  public static GithubIssue getIssue(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, @NotNull String id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/issues/" + id;

      JsonElement result = getRequest(auth, path, ACCEPT_V3_JSON);

      return createDataFromRaw(fromJson(result, GithubIssueRaw.class), GithubIssue.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get issue info: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubIssueComment> getIssueComments(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, long id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/issues/" + id + "/comments?" + PER_PAGE;

      PagedRequest<GithubIssueComment> request =
        new PagedRequest<GithubIssueComment>(path, GithubIssueComment.class, GithubIssueCommentRaw[].class, ACCEPT_V3_JSON_HTML_MARKUP);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get issue comments: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static GithubCommitDetailed getCommit(@NotNull GithubAuthData auth,
                                               @NotNull String user,
                                               @NotNull String repo,
                                               @NotNull String sha) throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/commits/" + sha;

      JsonElement result = getRequest(auth, path, ACCEPT_V3_JSON);
      return createDataFromRaw(fromJson(result, GithubCommitRaw.class), GithubCommitDetailed.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get commit info: " + user + "/" + repo + " - " + sha);
      throw e;
    }
  }

  @NotNull
  public static List<GithubCommitComment> getCommitComments(@NotNull GithubAuthData auth,
                                                            @NotNull String user,
                                                            @NotNull String repo,
                                                            @NotNull String sha) throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/commits/" + sha + "/comments";

      PagedRequest<GithubCommitComment> request =
        new PagedRequest<GithubCommitComment>(path, GithubCommitComment.class, GithubCommitCommentRaw[].class, ACCEPT_V3_JSON_HTML_MARKUP);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get commit comments: " + user + "/" + repo + " - " + sha);
      throw e;
    }
  }

  @NotNull
  public static List<GithubCommitComment> getPullRequestComments(@NotNull GithubAuthData auth,
                                                                 @NotNull String user,
                                                                 @NotNull String repo,
                                                                 long id) throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls/" + id + "/comments";

      PagedRequest<GithubCommitComment> request =
        new PagedRequest<GithubCommitComment>(path, GithubCommitComment.class, GithubCommitCommentRaw[].class, ACCEPT_V3_JSON_HTML_MARKUP);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request comments: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static GithubPullRequest getPullRequest(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, int id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls/" + id;
      return createDataFromRaw(fromJson(getRequest(auth, path, ACCEPT_V3_JSON_HTML_MARKUP), GithubPullRequestRaw.class),
                               GithubPullRequest.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request info: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubPullRequest> getPullRequests(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls?" + PER_PAGE;

      PagedRequest<GithubPullRequest> request =
        new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_V3_JSON_HTML_MARKUP);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull requests" + user + "/" + repo);
      throw e;
    }
  }

  @NotNull
  public static PagedRequest<GithubPullRequest> getPullRequests(@NotNull String user, @NotNull String repo) {
    String path = "/repos/" + user + "/" + repo + "/pulls?" + PER_PAGE;

    return new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_V3_JSON_HTML_MARKUP);
  }

  @NotNull
  public static List<GithubCommit> getPullRequestCommits(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, long id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls/" + id + "/commits?" + PER_PAGE;

      PagedRequest<GithubCommit> request =
        new PagedRequest<GithubCommit>(path, GithubCommit.class, GithubCommitRaw[].class, ACCEPT_V3_JSON);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request commits: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubFile> getPullRequestFiles(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, long id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls/" + id + "/files?" + PER_PAGE;

      PagedRequest<GithubFile> request = new PagedRequest<GithubFile>(path, GithubFile.class, GithubFileRaw[].class, ACCEPT_V3_JSON);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request files: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubBranch> getRepoBranches(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/branches?" + PER_PAGE;

      PagedRequest<GithubBranch> request =
        new PagedRequest<GithubBranch>(path, GithubBranch.class, GithubBranchRaw[].class, ACCEPT_V3_JSON);

      return request.getAll(auth);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get repository branches: " + user + "/" + repo);
      throw e;
    }
  }

  @Nullable
  public static GithubRepo findForkByUser(@NotNull GithubAuthData auth,
                                          @NotNull String user,
                                          @NotNull String repo,
                                          @NotNull String forkUser) throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/forks?" + PER_PAGE;

      PagedRequest<GithubRepo> request = new PagedRequest<GithubRepo>(path, GithubRepo.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);

      while (request.hasNext()) {
        for (GithubRepo fork : request.next(auth)) {
          if (StringUtil.equalsIgnoreCase(fork.getUserName(), forkUser)) {
            return fork;
          }
        }
      }

      return null;
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't find fork by user: " + user + "/" + repo + " - " + forkUser);
      throw e;
    }
  }
}