-
Notifications
You must be signed in to change notification settings - Fork 352
/
BitbucketCloudApiClient.java
1119 lines (1030 loc) · 45.5 KB
/
BitbucketCloudApiClient.java
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
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
* Copyright (c) 2017-2018, bguerin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.client;
import com.cloudbees.jenkins.plugins.bitbucket.JsonParser;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBuildStatus;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCloudWorkspace;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketException;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook;
import com.cloudbees.jenkins.plugins.bitbucket.api.credentials.BitbucketUsernamePasswordAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.avatars.AvatarCacheSource.AvatarImage;
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudBranch;
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudCommit;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestCommit;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestCommits;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValue;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequests;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositoryHook;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositoryHooks;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositorySource;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.PaginatedBitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository;
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFile;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.damnhandy.uri.template.UriTemplate;
import com.damnhandy.uri.template.impl.Operator;
import com.fasterxml.jackson.core.type.TypeReference;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ProxyConfiguration;
import hudson.Util;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMFile;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.ProtectedExternally;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
public class BitbucketCloudApiClient implements BitbucketApi {
private static final Logger LOGGER = Logger.getLogger(BitbucketCloudApiClient.class.getName());
private static final HttpHost API_HOST = HttpHost.create("https://api.bitbucket.org");
private static final String V2_API_BASE_URL = "https://api.bitbucket.org/2.0/repositories";
private static final String V2_WORKSPACES_API_BASE_URL = "https://api.bitbucket.org/2.0/workspaces";
private static final String REPO_URL_TEMPLATE = V2_API_BASE_URL + "{/owner,repo}";
private static final int API_RATE_LIMIT_CODE = 429;
// Limit images to 16k
private static final int MAX_AVATAR_LENGTH = 16384;
private static final int MAX_PAGE_LENGTH = 100;
private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
private CloseableHttpClient client;
private HttpClientContext context;
private final String owner;
private final String projectKey;
private final String repositoryName;
private final boolean enableCache;
private final BitbucketAuthenticator authenticator;
private static final Cache<String, BitbucketTeam> cachedTeam = new Cache<>(6, HOURS);
private static final Cache<String, AvatarImage> cachedAvatar = new Cache<>(6, HOURS);
private static final Cache<String, List<BitbucketCloudRepository>> cachedRepositories = new Cache<>(3, HOURS);
private transient BitbucketRepository cachedRepository;
private transient String cachedDefaultBranch;
static {
connectionManager.setDefaultMaxPerRoute(20);
connectionManager.setMaxTotal(22);
connectionManager.setSocketConfig(API_HOST, SocketConfig.custom().setSoTimeout(60 * 1000).build());
}
public static List<String> stats() {
List<String> stats = new ArrayList<>();
stats.add("Team: " + cachedTeam.stats().toString());
stats.add("Repositories : " + cachedRepositories.stats().toString());
return stats;
}
public static void clearCaches() {
cachedTeam.evictAll();
cachedRepositories.evictAll();
}
@Deprecated
public BitbucketCloudApiClient(boolean enableCache, int teamCacheDuration, int repositoriesCacheDuration,
String owner, String repositoryName, StandardUsernamePasswordCredentials credentials) {
this(enableCache, teamCacheDuration, repositoriesCacheDuration, owner, null, repositoryName,
new BitbucketUsernamePasswordAuthenticator(credentials));
}
public BitbucketCloudApiClient(boolean enableCache, int teamCacheDuration, int repositoriesCacheDuration,
String owner, String projectKey, String repositoryName, BitbucketAuthenticator authenticator) {
this.authenticator = authenticator;
this.owner = owner;
this.projectKey = projectKey;
this.repositoryName = repositoryName;
this.enableCache = enableCache;
if (enableCache) {
cachedTeam.setExpireDuration(teamCacheDuration, MINUTES);
cachedRepositories.setExpireDuration(repositoriesCacheDuration, MINUTES);
}
// Create Http client
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setKeepAliveStrategy((__, ___) -> MILLISECONDS.convert(5, SECONDS));
httpClientBuilder.setConnectionManager(connectionManager);
httpClientBuilder.setConnectionManagerShared(true);
httpClientBuilder.setRetryHandler(new StandardHttpRequestRetryHandler());
if (authenticator != null) {
authenticator.configureBuilder(httpClientBuilder);
context = HttpClientContext.create();
authenticator.configureContext(context, API_HOST);
}
setClientProxyParams("bitbucket.org", httpClientBuilder);
this.client = httpClientBuilder.build();
}
@Override
protected void finalize() throws Throwable {
if (client != null) {
client.close();
}
super.finalize();
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public String getOwner() {
return owner;
}
/**
* {@inheritDoc}
*/
@Override
@CheckForNull
public String getRepositoryName() {
return repositoryName;
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public List<BitbucketPullRequestValue> getPullRequests() throws InterruptedException, IOException {
List<BitbucketPullRequestValue> pullRequests = new ArrayList<>();
// we can not use the default max pagelen also if documented
// https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests#get
// so because with values greater than 50 the API returns HTTP 400
int pageLen = 50;
UriTemplate template = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/pullrequests{?page,pagelen}")
.set("owner", owner)
.set("repo", repositoryName)
.set("pagelen", pageLen);
BitbucketPullRequests page;
int pageNumber = 1;
do {
if (Thread.interrupted()) {
throw new InterruptedException();
}
String url = template //
.set("page", pageNumber++) //
.expand();
String response = getRequest(url);
try {
page = JsonParser.toJava(response, BitbucketPullRequests.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
pullRequests.addAll(page.getValues());
} while (page.getNext() != null);
// PRs with missing destination branch are invalid and should be ignored.
pullRequests.removeIf(this::shouldIgnore);
for (BitbucketPullRequestValue pullRequest : pullRequests) {
setupClosureForPRBranch(pullRequest);
}
return pullRequests;
}
/**
* PRs with missing source / destination branch are invalid and should be ignored.
*
* @param pr a {@link BitbucketPullRequest}
* @return whether the PR should be ignored
*/
private boolean shouldIgnore(BitbucketPullRequest pr) {
return pr.getSource().getRepository() == null
|| pr.getSource().getCommit() == null
|| pr.getDestination().getBranch() == null
|| pr.getDestination().getCommit() == null;
}
/**
* Make available commit information in a lazy way.
*
* @author Nikolas Falco
*/
private class CommitClosure implements Callable<BitbucketCommit> {
private final String hash;
public CommitClosure(@NonNull String hash) {
this.hash = hash;
}
@Override
public BitbucketCommit call() throws Exception {
return resolveCommit(hash);
}
}
private void setupClosureForPRBranch(BitbucketPullRequestValue pullRequest) {
BitbucketCloudBranch branch = pullRequest.getSource().getBranch();
if (branch != null) {
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
}
branch = pullRequest.getDestination().getBranch();
if (branch != null) {
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
}
}
@Deprecated
@CheckForNull
public String getLogin() {
if (authenticator != null) {
return authenticator.getId();
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
@NonNull
public BitbucketPullRequest getPullRequestById(@NonNull Integer id) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/pullrequests{/id}")
.set("owner", owner)
.set("repo", repositoryName)
.set("id", id)
.expand();
String response = getRequest(url);
try {
BitbucketPullRequestValue pr = JsonParser.toJava(response, BitbucketPullRequestValue.class);
setupClosureForPRBranch(pr);
return pr;
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@Override
@NonNull
public BitbucketRepository getRepository() throws IOException, InterruptedException {
if (repositoryName == null) {
throw new UnsupportedOperationException("Cannot get a repository from an API instance that is not associated with a repository");
}
if (!enableCache || cachedRepository == null) {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE)
.set("owner", owner)
.set("repo", repositoryName)
.expand();
String response = getRequest(url);
try {
cachedRepository = JsonParser.toJava(response, BitbucketCloudRepository.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
return cachedRepository;
}
/**
* {@inheritDoc}
*/
@Override
public void postCommitComment(@NonNull String hash, @NonNull String comment) throws IOException, InterruptedException {
String path = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit{/hash}/build")
.set("owner", owner)
.set("repo", repositoryName)
.set("hash", hash)
.expand();
try {
postRequest(path, Collections.singletonList(new BasicNameValuePair("content", comment)));
} catch (UnsupportedEncodingException e) {
throw e;
} catch (IOException e) {
throw new IOException("Cannot comment on commit, url: " + path, e);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean checkPathExists(@NonNull String branchOrHash, @NonNull String path)
throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/src{/branchOrHash,path*}")
.set("owner", owner)
.set("repo", repositoryName)
.set("branchOrHash", branchOrHash)
.set("path", path.split(Operator.PATH.getSeparator()))
.expand();
int status = headRequestStatus(url);
if (HttpStatus.SC_OK == status) {
return true;
} else if (HttpStatus.SC_NOT_FOUND == status) {
return false;
} else if (HttpStatus.SC_FORBIDDEN == status) {
// Needs to skip over the branch if there are permissions issues but let you know in the logs
LOGGER.log(Level.FINE, "You currently do not have permissions to pull from repo: {0} at branch {1}", new Object[]{repositoryName, branchOrHash});
return false;
} else {
throw new IOException("Communication error for url: " + path + " status code: " + status);
}
}
/**
* {@inheritDoc}
*/
@CheckForNull
@Override
public String getDefaultBranch() throws IOException, InterruptedException {
if (!enableCache || cachedDefaultBranch == null) {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/{?fields}")
.set("owner", owner)
.set("repo", repositoryName)
.set("fields", "mainbranch.name")
.expand();
String response;
try {
response = getRequest(url);
} catch (FileNotFoundException e) {
LOGGER.log(Level.FINE, "Could not find default branch for {0}/{1}",
new Object[]{this.owner, this.repositoryName});
return null;
}
Map resp = JsonParser.toJava(response, Map.class);
Map mainbranch = (Map) resp.get("mainbranch");
if (mainbranch != null) {
cachedDefaultBranch = (String) mainbranch.get("name");
}
}
return cachedDefaultBranch;
}
/**
* {@inheritDoc}
*/
@Override
public BitbucketCloudBranch getTag(@NonNull String tagName) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/refs/tags/{name}")
.set("owner", owner)
.set("repo", repositoryName)
.set("name", tagName)
.expand();
String response = getRequest(url);
try {
return getSingleBranch(response);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public List<BitbucketCloudBranch> getTags() throws IOException, InterruptedException {
return getBranchesByRef("/refs/tags");
}
/**
* {@inheritDoc}
*/
@Override
public BitbucketCloudBranch getBranch(@NonNull String branchName) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/refs/branches/{name}")
.set("owner", owner)
.set("repo", repositoryName)
.set("name", branchName)
.expand();
String response = getRequest(url);
try {
return getSingleBranch(response);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public List<BitbucketCloudBranch> getBranches() throws IOException, InterruptedException {
return getBranchesByRef("/refs/branches");
}
public List<BitbucketCloudBranch> getBranchesByRef(String nodePath) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + nodePath + "{?pagelen}")
.set("owner", owner)
.set("repo", repositoryName)
.set("pagelen", MAX_PAGE_LENGTH)
.expand();
String response = getRequest(url);
try {
return getAllBranches(response);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@Override
@CheckForNull
public BitbucketCommit resolveCommit(@NonNull String hash) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}")
.set("owner", owner)
.set("repo", repositoryName)
.set("hash", hash)
.expand();
String response;
try {
response = getRequest(url);
} catch (FileNotFoundException e) {
return null;
}
try {
return JsonParser.toJava(response, BitbucketCloudCommit.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public String resolveSourceFullHash(@NonNull BitbucketPullRequest pull) throws IOException, InterruptedException {
return resolveCommit(pull).getHash();
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public BitbucketCommit resolveCommit(@NonNull BitbucketPullRequest pull) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/pullrequests/{pullId}/commits{?fields,pagelen}")
.set("owner", owner)
.set("repo", repositoryName)
.set("pullId", pull.getId())
.set("fields", "values.hash,values.author.raw,values.date,values.message")
.set("pagelen", 1)
.expand();
String response = getRequest(url);
try {
BitbucketPullRequestCommits commits = JsonParser.toJava(response, BitbucketPullRequestCommits.class);
for (BitbucketPullRequestCommit commit : Util.fixNull(commits.getValues())) {
return commit;
}
throw new BitbucketException("Could not determine commit for pull request " + pull.getId());
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void registerCommitWebHook(@NonNull BitbucketWebHook hook) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/hooks")
.set("owner", owner)
.set("repo", repositoryName)
.expand();
postRequest(url, JsonParser.toJson(hook));
}
/**
* {@inheritDoc}
*/
@Override
public void updateCommitWebHook(@NonNull BitbucketWebHook hook) throws IOException, InterruptedException {
String url = UriTemplate
.fromTemplate(REPO_URL_TEMPLATE + "/hooks/{hook}")
.set("owner", owner)
.set("repo", repositoryName)
.set("hook", hook.getUuid())
.expand();
putRequest(url, JsonParser.toJson(hook));
}
/**
* {@inheritDoc}
*/
@Override
public void removeCommitWebHook(@NonNull BitbucketWebHook hook) throws IOException, InterruptedException {
if (StringUtils.isBlank(hook.getUuid())) {
throw new BitbucketException("Hook UUID required");
}
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/hooks/{uuid}")
.set("owner", owner)
.set("repo", repositoryName)
.set("uuid", hook.getUuid())
.expand();
deleteRequest(url);
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public List<BitbucketRepositoryHook> getWebHooks() throws IOException, InterruptedException {
List<BitbucketRepositoryHook> repositoryHooks = new ArrayList<>();
int pageNumber = 1;
UriTemplate template = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/hooks{?page,pagelen}")
.set("owner", owner)
.set("repo", repositoryName)
.set("page", pageNumber)
.set("pagelen", MAX_PAGE_LENGTH);
String url = template.expand();
try {
String response = getRequest(url);
BitbucketRepositoryHooks page = parsePaginatedRepositoryHooks(response);
repositoryHooks.addAll(page.getValues());
while (page.getNext() != null) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
pageNumber++;
response = getRequest(url = template.set("page", pageNumber).expand());
page = parsePaginatedRepositoryHooks(response);
repositoryHooks.addAll(page.getValues());
}
return repositoryHooks;
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void postBuildStatus(@NonNull BitbucketBuildStatus status) throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/commit/{hash}/statuses/build")
.set("owner", owner)
.set("repo", repositoryName)
.set("hash", status.getHash())
.expand();
postRequest(url, JsonParser.toJson(status));
}
/**
* {@inheritDoc}
*/
@Override
public boolean isPrivate() throws IOException, InterruptedException {
return getRepository().isPrivate();
}
private BitbucketRepositoryHooks parsePaginatedRepositoryHooks(String response) throws IOException {
BitbucketRepositoryHooks parsedResponse;
parsedResponse = JsonParser.toJava(response, BitbucketRepositoryHooks.class);
return parsedResponse;
}
/**
* {@inheritDoc}
*/
@Override
@CheckForNull
public BitbucketTeam getTeam() throws IOException, InterruptedException {
final String url = UriTemplate.fromTemplate(V2_WORKSPACES_API_BASE_URL + "{/owner}")
.set("owner", owner)
.expand();
Callable<BitbucketTeam> request = () -> {
try {
String response = getRequest(url);
return JsonParser.toJava(response, BitbucketCloudWorkspace.class);
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
};
try {
if (enableCache) {
return cachedTeam.get(owner, request);
} else {
return request.call();
}
} catch (Exception ex) {
return null;
}
}
/**
* {@inheritDoc}
*/
@Override
@CheckForNull
public AvatarImage getTeamAvatar() throws IOException, InterruptedException {
try {
final BitbucketTeam team = getTeam();
final String url = (team!=null) ? team.getLink("avatar") : null;
if (url == null) {
return AvatarImage.EMPTY;
}
Callable<AvatarImage> request = () -> {
try {
BufferedImage avatar = getImageRequest(url);
return new AvatarImage(avatar, System.currentTimeMillis());
} catch (FileNotFoundException e) {
LOGGER.log(Level.FINE, "Failed to get avatar for team {0} from URL: " + url,
team.getName());
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
return null;
};
try {
if (enableCache) {
return cachedAvatar.get(owner, request);
} else {
return request.call();
}
} catch (Exception ex) {
return null;
}
} catch (Exception ex) {
LOGGER.log(Level.FINE, "Unexpected exception while loading team avatar: "+ex.getMessage(), ex);
throw ex;
}
}
/**
* The role parameter only makes sense when the request is authenticated, so
* if there is no auth information ({@link #authenticator}) the role will be omitted.
*/
@NonNull
@Override
public List<BitbucketCloudRepository> getRepositories(@CheckForNull UserRoleInRepository role)
throws InterruptedException, IOException {
StringBuilder cacheKey = new StringBuilder();
cacheKey.append(owner);
if (authenticator != null) {
cacheKey.append("::").append(authenticator.getId());
} else {
cacheKey.append("::<anonymous>");
}
final UriTemplate template = UriTemplate.fromTemplate(V2_API_BASE_URL + "{/owner}{?role,page,pagelen,q}")
.set("owner", owner)
.set("pagelen", MAX_PAGE_LENGTH);
if (StringUtils.isNotBlank(projectKey)) {
template.set("q", "project.key=" + "\"" + projectKey + "\""); // q=project.key="<projectKey>"
}
if (role != null && authenticator != null) {
template.set("role", role.getId());
cacheKey.append("::").append(role.getId());
}
Callable<List<BitbucketCloudRepository>> request = () -> {
List<BitbucketCloudRepository> repositories = new ArrayList<>();
Integer pageNumber = 1;
String url, response;
PaginatedBitbucketRepository page;
do {
response = getRequest(url = template.set("page", pageNumber).expand());
try {
page = JsonParser.toJava(response, PaginatedBitbucketRepository.class);
repositories.addAll(page.getValues());
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
pageNumber++;
} while (page.getNext() != null);
repositories.sort(Comparator.comparing(BitbucketCloudRepository::getRepositoryName));
return repositories;
};
try {
if (enableCache) {
return cachedRepositories.get(cacheKey.toString(), request);
} else {
return request.call();
}
} catch (Exception ex) {
throw new IOException("Error while loading repositories from cache", ex);
}
}
/** {@inheritDoc} */
@NonNull
@Override
public List<BitbucketCloudRepository> getRepositories() throws IOException, InterruptedException {
return getRepositories(null);
}
private void setClientProxyParams(String host, HttpClientBuilder builder) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
ProxyConfiguration proxyConfig = null;
if (jenkins != null) {
proxyConfig = jenkins.proxy;
}
Proxy proxy = Proxy.NO_PROXY;
if (proxyConfig != null) {
proxy = proxyConfig.createProxy(host);
}
if (proxy.type() != Proxy.Type.DIRECT) {
final InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
LOGGER.fine("Jenkins proxy: " + proxy.address());
HttpHost proxyHttpHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort());
builder.setProxy(proxyHttpHost);
String username = proxyConfig.getUserName();
String password = proxyConfig.getPassword();
if (username != null && !"".equals(username.trim())) {
LOGGER.fine("Using proxy authentication (user=" + username + ")");
if (context == null) {
// may have been already set in com.cloudbees.jenkins.plugins.bitbucket.api.credentials.BitbucketUsernamePasswordAuthenticator.configureContext(HttpClientContext, HttpHost)
context = HttpClientContext.create();
}
CredentialsProvider credentialsProvider = context.getCredentialsProvider();
if (credentialsProvider == null) {
credentialsProvider = new BasicCredentialsProvider();
// may have been already set in com.cloudbees.jenkins.plugins.bitbucket.api.credentials.BitbucketUsernamePasswordAuthenticator.configureContext(HttpClientContext, HttpHost)
context.setCredentialsProvider(credentialsProvider);
}
credentialsProvider.setCredentials(new AuthScope(proxyHttpHost), new UsernamePasswordCredentials(username, password));
AuthCache authCache = context.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAuthCache(authCache);
}
authCache.put(proxyHttpHost, new BasicScheme());
}
}
}
@Restricted(ProtectedExternally.class)
protected CloseableHttpResponse executeMethod(HttpRequestBase httpMethod) throws InterruptedException, IOException {
return executeMethod(API_HOST, httpMethod);
}
@Restricted(ProtectedExternally.class)
protected CloseableHttpResponse executeMethod(HttpHost host, HttpRequestBase httpMethod) throws InterruptedException, IOException {
HttpClientContext requestContext = null;
if (API_HOST.equals(host)) {
requestContext = context;
if (authenticator != null) {
authenticator.configureRequest(httpMethod);
}
}
RequestConfig.Builder requestConfig = RequestConfig.custom();
String connectTimeout = System.getProperty("http.connect.timeout", "10");
requestConfig.setConnectTimeout(Integer.parseInt(connectTimeout) * 1000);
String connectionRequestTimeout = System.getProperty("http.connect.request.timeout", "60");
requestConfig.setConnectionRequestTimeout(Integer.parseInt(connectionRequestTimeout) * 1000);
String socketTimeout = System.getProperty("http.socket.timeout", "60");
requestConfig.setSocketTimeout(Integer.parseInt(socketTimeout) * 1000);
httpMethod.setConfig(requestConfig.build());
CloseableHttpResponse response = client.execute(host, httpMethod, requestContext);
while (response.getStatusLine().getStatusCode() == API_RATE_LIMIT_CODE) {
release(httpMethod);
if (Thread.interrupted()) {
throw new InterruptedException();
}
/*
TODO: When bitbucket starts supporting rate limit expiration time, remove 5 sec wait and put code
to wait till expiration time is over. It should also fix the wait for ever loop.
*/
LOGGER.fine("Bitbucket Cloud API rate limit reached, sleeping for 5 sec then retry...");
Thread.sleep(5000);
response = client.execute(host, httpMethod, requestContext);
}
return response;
}
/**
* Caller's responsible to close the InputStream.
*/
private InputStream getRequestAsInputStream(String path) throws IOException, InterruptedException {
HttpGet httpget = new HttpGet(path);
HttpHost host = null;
// Extract host from URL, if present
try {
URI uri = new URI(path);
if (uri.isAbsolute() && ! uri.isOpaque()) {
host = HttpHost.create(""+uri.getScheme()+"://"+uri.getAuthority());
}
} catch (URISyntaxException ex) {
}
// Use default API Host otherwise
if (host == null) {
host = API_HOST;
}
try {
CloseableHttpResponse response = executeMethod(host, httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
EntityUtils.consume(response.getEntity());
response.close();
throw new FileNotFoundException("URL: " + path);
}
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
String content = IOUtils.toString(response.getEntity().getContent());
int statusCode = response.getStatusLine().getStatusCode();
String status = response.getStatusLine().getReasonPhrase();
EntityUtils.consume(response.getEntity());
response.close();
throw new BitbucketRequestException(statusCode,
"HTTP request error. Status: " + statusCode + ": " + status + ".\n" + content);
}
return new ClosingConnectionInputStream(response, httpget, connectionManager);
} catch (BitbucketRequestException | FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw new IOException("Communication error for url: " + path, e);
}
}
private String getRequest(String path) throws IOException, InterruptedException {
try (InputStream inputStream = getRequestAsInputStream(path)){
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
private BufferedImage getImageRequest(String path) throws IOException, InterruptedException {
try (InputStream inputStream = getRequestAsInputStream(path)) {
int length = MAX_AVATAR_LENGTH;
BufferedInputStream bis = new BufferedInputStream(inputStream, length);
BufferedImage image = ImageIO.read(bis);
return image;
}
}
private int headRequestStatus(String path) throws IOException, InterruptedException {
HttpHead httpHead = new HttpHead(path);
try(CloseableHttpResponse response = executeMethod(httpHead)) {
EntityUtils.consume(response.getEntity());
return response.getStatusLine().getStatusCode();
} catch (IOException e) {
throw new IOException("Communication error for url: " + path, e);
} finally {
release(httpHead);
}
}
private void deleteRequest(String path) throws IOException, InterruptedException {
HttpDelete httppost = new HttpDelete(path);
try(CloseableHttpResponse response = executeMethod(httppost)) {
EntityUtils.consume(response.getEntity());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
throw new FileNotFoundException("URL: " + path);
}
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), "HTTP request error. Status: " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase());
}
} catch (BitbucketRequestException e) {
throw e;
} catch (IOException e) {
throw new IOException("Communication error for url: " + path, e);
} finally {
release(httppost);
}
}
private String doRequest(HttpRequestBase httppost) throws IOException, InterruptedException {
try(CloseableHttpResponse response = executeMethod(httppost)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
EntityUtils.consume(response.getEntity());
// 204, no content
return "";
}
String content = getResponseContent(response);
EntityUtils.consume(response.getEntity());
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), "HTTP request error. Status: " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase() + ".\n" + response);
}
return content;
} catch (BitbucketRequestException e) {
throw e;
} catch (IOException e) {