use of jenkins.scm.api.SCMSourceOwner in project gitea-plugin by jenkinsci.
the class GiteaSCMSource method gitea.
/*package*/
Gitea gitea() throws AbortException {
GiteaServer server = GiteaServers.get().findServer(serverUrl);
if (server == null) {
throw new AbortException("Unknown server: " + serverUrl);
}
StandardCredentials credentials = credentials();
SCMSourceOwner owner = getOwner();
if (owner != null) {
CredentialsProvider.track(owner, credentials);
}
return Gitea.server(serverUrl).as(AuthenticationTokens.convert(GiteaAuth.class, credentials));
}
use of jenkins.scm.api.SCMSourceOwner in project blueocean-plugin by jenkinsci.
the class GitScm method validateAndCreate.
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
boolean requirePush = request.has("requirePush");
final String repositoryUrl;
final AbstractGitSCMSource scmSource;
if (request.has("repositoryUrl")) {
scmSource = null;
repositoryUrl = request.getString("repositoryUrl");
} else {
try {
String fullName = request.getJSONObject("pipeline").getString("fullName");
SCMSourceOwner item = Jenkins.getInstance().getItemByFullName(fullName, SCMSourceOwner.class);
if (item != null) {
scmSource = (AbstractGitSCMSource) item.getSCMSources().iterator().next();
repositoryUrl = scmSource.getRemote();
} else {
return HttpResponses.errorJSON("No repository found for: " + fullName);
}
} catch (JSONException e) {
return HttpResponses.errorJSON("No repositoryUrl or pipeline.fullName specified in request.");
} catch (RuntimeException e) {
return HttpResponses.errorWithoutStack(ServiceException.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
try {
String credentialId = request.getString("credentialId");
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
final StandardCredentials creds = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, Jenkins.getInstance(), Jenkins.getAuthentication(), (List<DomainRequirement>) null), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId)));
if (creds == null) {
throw new ServiceException.NotFoundException("No credentials found for: " + credentialId);
}
if (requirePush) {
String branch = request.getString("branch");
new GitBareRepoReadSaveRequest(scmSource, branch, null, branch, null, null).invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() {
@Override
public Void invoke(Repository repository) throws IOException, InterruptedException {
GitUtils.validatePushAccess(repository, repositoryUrl, creds);
return null;
}
});
} else {
List<ErrorMessage.Error> errors = GitUtils.validateCredentials(repositoryUrl, creds);
if (!errors.isEmpty()) {
throw new ServiceException.UnauthorizedException(errors.get(0).getMessage());
}
}
} catch (Exception e) {
return HttpResponses.errorWithoutStack(ServiceException.PRECONDITION_REQUIRED, e.getMessage());
}
return HttpResponses.okJSON();
}
Aggregations