30
30
import static org .apache .commons .lang .StringUtils .removeEnd ;
31
31
import static org .jenkinsci .plugins .github_branch_source .Connector .isCredentialValid ;
32
32
import static org .jenkinsci .plugins .github_branch_source .GitHubSCMBuilder .API_V3 ;
33
+ import static org .jenkinsci .plugins .github_branch_source .GitHubSCMBuilder .HTTPS ;
33
34
34
35
import com .cloudbees .jenkins .GitHubWebHook ;
35
36
import com .cloudbees .plugins .credentials .CredentialsNameProvider ;
@@ -272,6 +273,9 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
272
273
/** The cache of {@link ObjectMetadataAction} instances for each open PR. */
273
274
@ NonNull
274
275
private transient /*effectively final*/ Map <Integer , ContributorMetadataAction > pullRequestContributorCache ;
276
+ /** The cache of the credentials object */
277
+ @ CheckForNull
278
+ private transient volatile StandardCredentials credentials ;
275
279
276
280
/**
277
281
* Used during upgrade from 1.x to 2.2.0+ only.
@@ -317,6 +321,19 @@ public GitHubSCMSource(String repoOwner, String repository, String repositoryUrl
317
321
this .traits = new ArrayList <>();
318
322
}
319
323
324
+ /**
325
+ * Constructor that passes a looked up credentials object.
326
+ *
327
+ * @param repoOwner the repository owner.
328
+ * @param repository the repository name.
329
+ * @param credentials a {@link com.cloudbees.plugins.credentials.common.StandardCredentials}
330
+ */
331
+ @ Restricted (NoExternalUse .class )
332
+ GitHubSCMSource (String repoOwner , String repository , StandardCredentials credentials ) {
333
+ this (repoOwner , repository , null , false );
334
+ this .credentials = credentials ;
335
+ }
336
+
320
337
/**
321
338
* Legacy constructor.
322
339
*
@@ -362,6 +379,15 @@ public GitHubSCMSource(
362
379
}
363
380
}
364
381
382
+ @ CheckForNull
383
+ @ Restricted (NoExternalUse .class )
384
+ private StandardCredentials getCredentials (@ CheckForNull Item context , boolean forceRefresh ) {
385
+ if (credentials == null || forceRefresh ) {
386
+ credentials = Connector .lookupScanCredentials (context , getApiUri (), getCredentialsId (), getRepoOwner ());
387
+ }
388
+ return credentials ;
389
+ }
390
+
365
391
@ Restricted (NoExternalUse .class )
366
392
public boolean isConfiguredByUrl () {
367
393
return repositoryUrl != null ;
@@ -598,8 +624,8 @@ public static void setCacheSize(int cacheSize) {
598
624
/** {@inheritDoc} */
599
625
@ Override
600
626
public String getRemote () {
601
- return GitHubSCMBuilder . uriResolver ( getOwner (), apiUri , credentialsId )
602
- .getRepositoryUri (apiUri , repoOwner , repository );
627
+ // Only HTTPS is applicable to the source remote with Username / Password credentials
628
+ return HTTPS .getRepositoryUri (apiUri , repoOwner , repository );
603
629
}
604
630
605
631
/** {@inheritDoc} */
@@ -619,7 +645,7 @@ public String getPronoun() {
619
645
@ Restricted (DoNotUse .class )
620
646
@ RestrictedSince ("2.2.0" )
621
647
public RepositoryUriResolver getUriResolver () {
622
- return GitHubSCMBuilder . uriResolver ( getOwner (), apiUri , credentialsId ) ;
648
+ return HTTPS ;
623
649
}
624
650
625
651
@ Restricted (NoExternalUse .class )
@@ -977,8 +1003,10 @@ protected final void retrieve(
977
1003
@ CheckForNull SCMHeadEvent <?> event ,
978
1004
@ NonNull final TaskListener listener )
979
1005
throws IOException , InterruptedException {
980
- StandardCredentials credentials =
981
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
1006
+ // In case we are in an Organization Scan - i.e. (observer instanceof SCMHeadObserver.Any) - use the cached
1007
+ // credentials
1008
+ // https://github.com/jenkinsci/branch-api-plugin/blob/2.1169.va_f810c56e895/src/main/java/jenkins/branch/MultiBranchProjectFactory.java#L262
1009
+ StandardCredentials credentials = getCredentials (getOwner (), !(observer instanceof SCMHeadObserver .Any ));
982
1010
// Github client and validation
983
1011
final GitHub github = Connector .connect (apiUri , credentials );
984
1012
try {
@@ -1279,10 +1307,8 @@ public SCMRevision create(@NonNull PullRequestSCMHead head, @Nullable Void ignor
1279
1307
@ Override
1280
1308
protected Set <String > retrieveRevisions (@ NonNull TaskListener listener , Item retrieveContext )
1281
1309
throws IOException , InterruptedException {
1282
- StandardCredentials credentials =
1283
- Connector .lookupScanCredentials (retrieveContext , apiUri , credentialsId , repoOwner );
1284
1310
// Github client and validation
1285
- final GitHub github = Connector .connect (apiUri , credentials );
1311
+ final GitHub github = Connector .connect (apiUri , getCredentials ( retrieveContext , false ) );
1286
1312
try {
1287
1313
Connector .configureLocalRateLimitChecker (listener , github );
1288
1314
Set <String > result = new TreeSet <>();
@@ -1385,10 +1411,8 @@ protected Set<String> retrieveRevisions(@NonNull TaskListener listener, Item ret
1385
1411
@ Override
1386
1412
protected SCMRevision retrieve (@ NonNull String headName , @ NonNull TaskListener listener , Item retrieveContext )
1387
1413
throws IOException , InterruptedException {
1388
- StandardCredentials credentials =
1389
- Connector .lookupScanCredentials (retrieveContext , apiUri , credentialsId , repoOwner );
1390
1414
// Github client and validation
1391
- final GitHub github = Connector .connect (apiUri , credentials );
1415
+ final GitHub github = Connector .connect (apiUri , getCredentials ( retrieveContext , false ) );
1392
1416
try {
1393
1417
Connector .configureLocalRateLimitChecker (listener , github );
1394
1418
// Input data validation
@@ -1614,10 +1638,8 @@ public void unwrap() throws IOException, InterruptedException {
1614
1638
@ NonNull
1615
1639
@ Override
1616
1640
protected SCMProbe createProbe (@ NonNull SCMHead head , @ CheckForNull final SCMRevision revision ) throws IOException {
1617
- StandardCredentials credentials =
1618
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
1619
1641
// Github client and validation
1620
- GitHub github = Connector .connect (apiUri , credentials );
1642
+ GitHub github = Connector .connect (apiUri , getCredentials ( getOwner (), false ) );
1621
1643
try {
1622
1644
String fullName = repoOwner + "/" + repository ;
1623
1645
final GHRepository repo = github .getRepository (fullName );
@@ -1632,11 +1654,8 @@ protected SCMProbe createProbe(@NonNull SCMHead head, @CheckForNull final SCMRev
1632
1654
@ Override
1633
1655
@ CheckForNull
1634
1656
protected SCMRevision retrieve (SCMHead head , TaskListener listener ) throws IOException , InterruptedException {
1635
- StandardCredentials credentials =
1636
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
1637
-
1638
1657
// Github client and validation
1639
- GitHub github = Connector .connect (apiUri , credentials );
1658
+ GitHub github = Connector .connect (apiUri , getCredentials ( getOwner (), false ) );
1640
1659
try {
1641
1660
try {
1642
1661
Connector .checkConnectionValidity (apiUri , listener , credentials , github );
@@ -1783,11 +1802,9 @@ PullRequestSource retrievePullRequestSource(int number) {
1783
1802
if (StringUtils .isNotBlank (repository )) {
1784
1803
String fullName = repoOwner + "/" + repository ;
1785
1804
LOGGER .log (Level .INFO , "Getting remote pull requests from {0}" , fullName );
1786
- StandardCredentials credentials =
1787
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
1788
1805
LogTaskListener listener = new LogTaskListener (LOGGER , Level .INFO );
1789
1806
try {
1790
- GitHub github = Connector .connect (apiUri , credentials );
1807
+ GitHub github = Connector .connect (apiUri , getCredentials ( getOwner (), false ) );
1791
1808
try {
1792
1809
Connector .configureLocalRateLimitChecker (listener , github );
1793
1810
ghRepository = github .getRepository (fullName );
@@ -1952,8 +1969,7 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event, @NonN
1952
1969
result .add (new GitHubRepoMetadataAction ());
1953
1970
String repository = this .repository ;
1954
1971
1955
- StandardCredentials credentials =
1956
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
1972
+ StandardCredentials credentials = getCredentials (getOwner (), true );
1957
1973
GitHub hub = Connector .connect (apiUri , credentials );
1958
1974
try {
1959
1975
Connector .checkConnectionValidity (apiUri , listener , credentials , hub );
@@ -2857,8 +2873,7 @@ protected Set<String> create() {
2857
2873
.format (
2858
2874
"Connecting to %s to obtain list of collaborators for %s/%s%n" ,
2859
2875
apiUri , repoOwner , repository );
2860
- StandardCredentials credentials =
2861
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
2876
+ StandardCredentials credentials = getCredentials (getOwner (), false );
2862
2877
// Github client and validation
2863
2878
try {
2864
2879
GitHub github = Connector .connect (apiUri , credentials );
@@ -2922,9 +2937,7 @@ public GHPermissionType fetch(String username) throws IOException, InterruptedEx
2922
2937
.format (
2923
2938
"Connecting to %s to check permissions of obtain list of %s for %s/%s%n" ,
2924
2939
apiUri , username , repoOwner , repository );
2925
- StandardCredentials credentials =
2926
- Connector .lookupScanCredentials ((Item ) getOwner (), apiUri , credentialsId , repoOwner );
2927
- github = Connector .connect (apiUri , credentials );
2940
+ github = Connector .connect (apiUri , getCredentials (getOwner (), false ));
2928
2941
String fullName = repoOwner + "/" + repository ;
2929
2942
repo = github .getRepository (fullName );
2930
2943
}
0 commit comments