Search in sources :

Example 1 with SCMSourceOwner

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));
}
Also used : SCMSourceOwner(jenkins.scm.api.SCMSourceOwner) GiteaServer(org.jenkinsci.plugin.gitea.servers.GiteaServer) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) GiteaAuth(org.jenkinsci.plugin.gitea.client.api.GiteaAuth) AbortException(hudson.AbortException)

Example 2 with SCMSourceOwner

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();
}
Also used : User(hudson.model.User) SCMSourceOwner(jenkins.scm.api.SCMSourceOwner) GitSCMFileSystem(jenkins.plugins.git.GitSCMFileSystem) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) Repository(org.eclipse.jgit.lib.Repository) AbstractGitSCMSource(jenkins.plugins.git.AbstractGitSCMSource) List(java.util.List) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Aggregations

StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 SCMSourceOwner (jenkins.scm.api.SCMSourceOwner)2 AbortException (hudson.AbortException)1 User (hudson.model.User)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 List (java.util.List)1 AbstractGitSCMSource (jenkins.plugins.git.AbstractGitSCMSource)1 GitSCMFileSystem (jenkins.plugins.git.GitSCMFileSystem)1 JSONException (net.sf.json.JSONException)1 Repository (org.eclipse.jgit.lib.Repository)1 GiteaAuth (org.jenkinsci.plugin.gitea.client.api.GiteaAuth)1 GiteaServer (org.jenkinsci.plugin.gitea.servers.GiteaServer)1