|
31 | 31 | import edu.umd.cs.findbugs.annotations.NonNull; |
32 | 32 | import hudson.Extension; |
33 | 33 | import hudson.model.Item; |
| 34 | +import hudson.model.Queue.Task; |
34 | 35 | import hudson.scm.SCM; |
35 | 36 | import hudson.scm.SCMDescriptor; |
| 37 | +import hudson.security.ACL; |
36 | 38 | import java.io.IOException; |
37 | 39 | import jenkins.authentication.tokens.api.AuthenticationTokens; |
38 | | -import jenkins.model.Jenkins; |
39 | 40 | import jenkins.scm.api.SCMFile; |
40 | 41 | import jenkins.scm.api.SCMFileSystem; |
41 | 42 | import jenkins.scm.api.SCMHead; |
42 | 43 | import jenkins.scm.api.SCMRevision; |
43 | 44 | import jenkins.scm.api.SCMSource; |
44 | 45 | import jenkins.scm.api.SCMSourceDescriptor; |
45 | 46 | import jenkins.scm.api.SCMSourceOwner; |
| 47 | +import org.acegisecurity.Authentication; |
46 | 48 | import org.apache.commons.lang.StringUtils; |
47 | 49 | import org.jenkinsci.plugin.gitea.client.api.Gitea; |
48 | 50 | import org.jenkinsci.plugin.gitea.client.api.GiteaAuth; |
@@ -146,20 +148,24 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch |
146 | 148 | SCMSourceOwner owner = source.getOwner(); |
147 | 149 | String serverUrl = src.getServerUrl(); |
148 | 150 | String credentialsId = src.getCredentialsId(); |
149 | | - StandardCredentials credentials = StringUtils.isBlank(credentialsId) |
150 | | - ? null |
151 | | - : CredentialsMatchers.firstOrNull( |
| 151 | + StandardCredentials credentials = null; |
| 152 | + if (!StringUtils.isBlank(credentialsId)) { |
| 153 | + Authentication authentication = owner instanceof Task |
| 154 | + ? ((Task) owner).getDefaultAuthentication() |
| 155 | + : ACL.SYSTEM; |
| 156 | + credentials = CredentialsMatchers.firstOrNull( |
152 | 157 | CredentialsProvider.lookupCredentials( |
153 | 158 | StandardCredentials.class, |
154 | 159 | owner, |
155 | | - Jenkins.getAuthentication(), |
| 160 | + authentication, |
156 | 161 | URIRequirementBuilder.fromUri(serverUrl).build() |
157 | 162 | ), |
158 | 163 | CredentialsMatchers.allOf( |
159 | 164 | AuthenticationTokens.matcher(GiteaAuth.class), |
160 | 165 | CredentialsMatchers.withId(credentialsId) |
161 | 166 | ) |
162 | | - ); |
| 167 | + ); |
| 168 | + } |
163 | 169 | if (owner != null) { |
164 | 170 | CredentialsProvider.track(owner, credentials); |
165 | 171 | } |
|
0 commit comments