summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java
blob: 063ad3bc7bb61413cce03be6dc81941a902ed62e (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
package org.jetbrains.plugins.github.api;

import com.google.gson.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.message.BasicHeader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.api.GithubConnection.PagedRequest;
import org.jetbrains.plugins.github.exceptions.GithubConfusingException;
import org.jetbrains.plugins.github.exceptions.GithubJsonException;
import org.jetbrains.plugins.github.exceptions.GithubStatusCodeException;
import org.jetbrains.plugins.github.util.GithubUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

public class GithubApiUtil {
  private static final Logger LOG = GithubUtil.LOG;

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

  private static final String PER_PAGE = "per_page=100";

  private static final Header ACCEPT_V3_JSON_HTML_MARKUP = new BasicHeader("Accept", "application/vnd.github.v3.html+json");
  private static final Header ACCEPT_V3_JSON = new BasicHeader("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();
  }

  @NotNull
  public 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;
  }

  @NotNull
  public 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);
    }
  }

  /*
   * Operations
   */

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

  @NotNull
  public static Collection<String> getTokenScopes(@NotNull GithubConnection connection) throws IOException {
    Header[] headers = connection.headRequest("/user", ACCEPT_V3_JSON);

    Header scopesHeader = null;
    for (Header header : headers) {
      if (header.getName().equals("X-OAuth-Scopes")) {
        scopesHeader = header;
        break;
      }
    }
    if (scopesHeader == null) {
      throw new GithubConfusingException("No scopes header");
    }

    Collection<String> scopes = new ArrayList<String>();
    for (HeaderElement elem : scopesHeader.getElements()) {
      scopes.add(elem.getName());
    }
    return scopes;
  }

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

  @NotNull
  private static GithubAuthorization updateTokenScopes(@NotNull GithubConnection connection,
                                                       @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(connection.patchRequest(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 GithubConnection connection,
                                                       @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(connection.postRequest(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 GithubConnection connection, @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(connection);

      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 GithubConnection connection, @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(connection, scopes, note);
  }

  @NotNull
  public static String getReadOnlyToken(@NotNull GithubConnection connection,
                                        @NotNull String user,
                                        @NotNull String repo,
                                        @NotNull String note)
    throws IOException {
    GithubRepo repository = getDetailedRepoInfo(connection, 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(connection, scopes, note);
  }

  @NotNull
  public static GithubUser getCurrentUser(@NotNull GithubConnection connection) throws IOException {
    try {
      JsonElement result = connection.getRequest("/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 GithubConnection connection) throws IOException {
    try {
      JsonElement result = connection.getRequest("/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 GithubConnection connection) 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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user repositories");
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepo> getUserRepos(@NotNull GithubConnection connection, @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get user repositories: " + user);
      throw e;
    }
  }

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

      repos.addAll(getUserRepos(connection));

      // 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(connection));
      }
      catch (GithubStatusCodeException ignore) {
      }
      try {
        repos.addAll(getWatchedRepos(connection));
      }
      catch (GithubStatusCodeException ignore) {
      }

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

  @NotNull
  public static List<GithubRepoOrg> getMembershipRepos(@NotNull GithubConnection connection) 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(connection)) {
      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(connection));
    }

    return repos;
  }

  @NotNull
  public static List<GithubRepo> getWatchedRepos(@NotNull GithubConnection connection) 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(connection);
  }

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

      JsonElement jsonObject = connection.getRequest(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 GithubConnection connection, @NotNull String username, @NotNull String repo)
    throws IOException {
    try {
      String path = "/repos/" + username + "/" + repo;
      connection.deleteRequest(path);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't delete repository: " + username + "/" + repo);
      throw e;
    }
  }

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

  @NotNull
  public static GithubGist getGist(@NotNull GithubConnection connection, @NotNull String id) throws IOException {
    try {
      String path = "/gists/" + id;
      JsonElement result = connection.getRequest(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 GithubConnection connection,
                                      @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(connection.postRequest("/gists", request, ACCEPT_V3_JSON), GithubGistRaw.class), GithubGist.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't create gist");
      throw e;
    }
  }

  @NotNull
  public static List<GithubRepo> getForks(@NotNull GithubConnection connection, @NotNull String owner, @NotNull String name)
    throws IOException {
    String path = "/repos/" + owner + "/" + name + "/forks?" + PER_PAGE;
    PagedRequest<GithubRepo> requestWatched =
      new PagedRequest<GithubRepo>(path, GithubRepo.class, GithubRepoRaw[].class, ACCEPT_V3_JSON);
    return requestWatched.getAll(connection);
  }

  @NotNull
  public static GithubPullRequest createPullRequest(@NotNull GithubConnection connection,
                                                    @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(connection.postRequest("/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 GithubConnection connection,
                                      @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(connection.postRequest(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 GithubConnection connection,
                                                    @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(connection));
      }
      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 GithubConnection connection,
                                                   @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 = connection.getRequest(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 GithubConnection connection, @NotNull String user, @NotNull String repo, @NotNull String id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/issues/" + id;

      JsonElement result = connection.getRequest(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 GithubConnection connection,
                                                          @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get issue comments: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  public static void setIssueState(@NotNull GithubConnection connection,
                                   @NotNull String user,
                                   @NotNull String repo,
                                   @NotNull String id,
                                   boolean open)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/issues/" + id;

      GithubChangeIssueStateRequest request = new GithubChangeIssueStateRequest(open ? "open" : "closed");

      JsonElement result = connection.patchRequest(path, gson.toJson(request), ACCEPT_V3_JSON);

      createDataFromRaw(fromJson(result, GithubIssueRaw.class), GithubIssue.class);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't set issue state: " + user + "/" + repo + " - " + id + "@" + (open ? "open" : "closed"));
      throw e;
    }
  }


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

      JsonElement result = connection.getRequest(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 GithubConnection connection,
                                                            @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get commit comments: " + user + "/" + repo + " - " + sha);
      throw e;
    }
  }

  @NotNull
  public static List<GithubCommitComment> getPullRequestComments(@NotNull GithubConnection connection,
                                                                 @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request comments: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static GithubPullRequest getPullRequest(@NotNull GithubConnection connection, @NotNull String user, @NotNull String repo, int id)
    throws IOException {
    try {
      String path = "/repos/" + user + "/" + repo + "/pulls/" + id;
      return createDataFromRaw(fromJson(connection.getRequest(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 GithubConnection connection, @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(connection);
    }
    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 GithubConnection connection,
                                                         @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request commits: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubFile> getPullRequestFiles(@NotNull GithubConnection connection,
                                                     @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get pull request files: " + user + "/" + repo + " - " + id);
      throw e;
    }
  }

  @NotNull
  public static List<GithubBranch> getRepoBranches(@NotNull GithubConnection connection, @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(connection);
    }
    catch (GithubConfusingException e) {
      e.setDetails("Can't get repository branches: " + user + "/" + repo);
      throw e;
    }
  }

  @Nullable
  public static GithubRepo findForkByUser(@NotNull GithubConnection connection,
                                          @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(connection)) {
          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;
    }
  }
}