Search in sources :

Example 1 with GitHubSCMNavigator

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator in project blueocean-plugin by jenkinsci.

the class GithubScmContentProvider method saveContent.

@SuppressWarnings("unchecked")
private Object saveContent(@Nonnull GithubScmSaveFileRequest githubRequest, @Nonnull Item item) {
    String apiUrl = GithubScm.DEFAULT_API_URI;
    String owner = null;
    String repo = null;
    String accessToken = null;
    String credentialId = null;
    if (item instanceof OrganizationFolder) {
        List<SCMNavigator> navigators = ((OrganizationFolder) item).getSCMNavigators();
        if (!navigators.isEmpty() && navigators.get(0) instanceof GitHubSCMNavigator) {
            GitHubSCMNavigator navigator = (GitHubSCMNavigator) navigators.get(0);
            if (navigator.getApiUri() != null) {
                apiUrl = navigator.getApiUri();
            }
            credentialId = navigator.getScanCredentialsId();
            owner = navigator.getRepoOwner();
        }
    } else if (item instanceof MultiBranchProject) {
        List<SCMSource> sources = ((MultiBranchProject) item).getSCMSources();
        if (!sources.isEmpty() && sources.get(0) instanceof GitHubSCMSource) {
            GitHubSCMSource source = (GitHubSCMSource) sources.get(0);
            if (source.getApiUri() != null) {
                apiUrl = source.getApiUri();
            }
            credentialId = source.getScanCredentialsId();
            owner = owner(source);
            repo = repo(source);
        }
    }
    if (credentialId != null) {
        StandardCredentials credentials = Connector.lookupScanCredentials((SCMSourceOwner) item, apiUrl, credentialId);
        if (credentials instanceof StandardUsernamePasswordCredentials) {
            accessToken = ((StandardUsernamePasswordCredentials) credentials).getPassword().getPlainText();
        } else {
            throw new ServiceException.BadRequestExpception("accessToken not found in pipeline: " + item.getFullName());
        }
    }
    return githubRequest.save(apiUrl, owner, repo, accessToken);
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) SCMNavigator(jenkins.scm.api.SCMNavigator) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) OrganizationFolder(jenkins.branch.OrganizationFolder) MultiBranchProject(jenkins.branch.MultiBranchProject) ArrayList(java.util.ArrayList) List(java.util.List) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 2 with GitHubSCMNavigator

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator in project blueocean-plugin by jenkinsci.

the class GithubPipelineUpdateRequest method getNavigator.

@SuppressWarnings("unchecked")
private GitHubSCMNavigator getNavigator(OrganizationFolder folder) throws IOException {
    String apiUrl = null;
    String credentialId = null;
    String orgName = null;
    StringBuilder sb = new StringBuilder();
    if (scmConfig != null) {
        apiUrl = scmConfig.getUri();
        credentialId = scmConfig.getCredentialId();
        if (scmConfig.getConfig().get("orgName") instanceof String) {
            orgName = (String) scmConfig.getConfig().get("orgName");
        }
    }
    for (SCMNavigator navigator : folder.getNavigators()) {
        if (navigator instanceof GitHubSCMNavigator) {
            GitHubSCMNavigator scmNavigator = (GitHubSCMNavigator) navigator;
            if (scmNavigator.getApiUri() != null && !scmNavigator.getApiUri().equals(apiUrl)) {
                apiUrl = scmNavigator.getApiUri();
            }
            if (scmNavigator.getScanCredentialsId() != null && !scmNavigator.getScanCredentialsId().equals(credentialId)) {
                credentialId = scmNavigator.getScanCredentialsId();
            }
            if (!scmNavigator.getRepoOwner().equals(orgName)) {
                orgName = scmNavigator.getRepoOwner();
            }
            GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
            GithubPipelineCreateRequest.validateCredentialId(credentialId, apiUrl);
            if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
                for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
                    sb.append(String.format("(%s\\b)?", r));
                    repos.add(r);
                }
            }
            if (sb.length() > 0) {
                gitHubSCMNavigator.setPattern(sb.toString());
            }
            return gitHubSCMNavigator;
        }
    }
    return null;
}
Also used : SCMNavigator(jenkins.scm.api.SCMNavigator) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) ArrayList(java.util.ArrayList) List(java.util.List) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator)

Example 3 with GitHubSCMNavigator

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator in project blueocean-plugin by jenkinsci.

the class GithubPipelineCreateRequest method create.

@SuppressWarnings("unchecked")
@Override
public BluePipeline create(Reachable parent) throws IOException {
    String apiUrl = null;
    //default
    String orgName = getName();
    String credentialId = null;
    StringBuilder sb = new StringBuilder();
    List<String> repos = new ArrayList<>();
    if (scmConfig != null) {
        apiUrl = StringUtils.defaultIfBlank(scmConfig.getUri(), GithubScm.DEFAULT_API_URI);
        if (scmConfig.getConfig().get("orgName") instanceof String) {
            orgName = (String) scmConfig.getConfig().get("orgName");
        }
        credentialId = scmConfig.getCredentialId();
        if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
            for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
                sb.append(String.format("(%s\\b)?", r));
                repos.add(r);
            }
        }
    }
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
    }
    TopLevelItem item = null;
    try {
        if (credentialId != null) {
            validateCredentialId(credentialId, apiUrl);
        }
        item = create(Jenkins.getInstance(), getName(), DESCRIPTOR, CustomOrganizationFolderDescriptor.class);
        if (item instanceof OrganizationFolder) {
            if (credentialId != null) {
                //Find domain attached to this credentialId, if present check if it's BlueOcean specific domain then
                //add the properties otherwise simply use it
                Domain domain = CredentialsUtils.findDomain(credentialId, authenticatedUser);
                if (domain == null) {
                    //this should not happen since validateCredentialId found the credential
                    throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
                }
                if (domain.test(new BlueOceanDomainRequirement())) {
                    ((OrganizationFolder) item).addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
                }
            }
            GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
            if (sb.length() > 0) {
                gitHubSCMNavigator.setPattern(sb.toString());
            }
            // cick of github scan build
            OrganizationFolder organizationFolder = (OrganizationFolder) item;
            organizationFolder.getNavigators().replace(gitHubSCMNavigator);
            if (repos.size() == 1) {
                SCMSourceEvent.fireNow(new SCMSourceEventImpl(repos.get(0), item, apiUrl, gitHubSCMNavigator));
            } else {
                organizationFolder.scheduleBuild(new Cause.UserIdCause());
            }
            return new GithubOrganizationFolder(organizationFolder, parent.getLink());
        }
    } catch (Exception e) {
        String msg = String.format("Error creating pipeline %s: %s", getName(), e.getMessage());
        logger.error(msg, e);
        if (item != null) {
            try {
                item.delete();
            } catch (InterruptedException e1) {
                logger.error(String.format("Error creating pipeline %s: %s", getName(), e1.getMessage()), e1);
                throw new ServiceException.UnexpectedErrorException("Error cleaning up pipeline " + getName() + " due to error: " + e.getMessage(), e);
            }
        }
        if (e instanceof ServiceException) {
            throw e;
        }
        throw new ServiceException.UnexpectedErrorException(msg, e);
    }
    return null;
}
Also used : User(hudson.model.User) ArrayList(java.util.ArrayList) TopLevelItem(hudson.model.TopLevelItem) Cause(hudson.model.Cause) ArrayList(java.util.ArrayList) List(java.util.List) CustomOrganizationFolderDescriptor(jenkins.branch.CustomOrganizationFolderDescriptor) OrganizationFolder(jenkins.branch.OrganizationFolder) BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Domain(com.cloudbees.plugins.credentials.domains.Domain) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage)

Example 4 with GitHubSCMNavigator

use of org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator in project blueocean-plugin by jenkinsci.

the class GithubPipelineUpdateRequest method update.

@Nonnull
@Override
public BluePipeline update(BluePipeline pipeline) throws IOException {
    ACL acl = Jenkins.getInstance().getACL();
    Authentication a = Jenkins.getAuthentication();
    if (!acl.hasPermission(a, Item.CONFIGURE)) {
        throw new ServiceException.ForbiddenException(String.format("Failed to update Git pipeline: %s. User %s doesn't have Job configure permission", pipeline.getName(), a.getName()));
    }
    User user = User.current();
    if (user == null) {
        throw new ServiceException.UnauthorizedException("User is not authenticated");
    }
    Item item = Jenkins.getInstance().getItemByFullName(pipeline.getFullName());
    if (item instanceof OrganizationFolder) {
        OrganizationFolder folder = (OrganizationFolder) item;
        GitHubSCMNavigator gitHubSCMNavigator = getNavigator(folder);
        if (gitHubSCMNavigator != null) {
            folder.getNavigators().replace(gitHubSCMNavigator);
            if (repos.size() == 1) {
                SCMSourceEvent.fireNow(new GithubPipelineCreateRequest.SCMSourceEventImpl(repos.get(0), item, gitHubSCMNavigator.getApiUri(), gitHubSCMNavigator));
            } else {
                folder.scheduleBuild(new Cause.UserIdCause());
            }
        }
    }
    return pipeline;
}
Also used : Item(hudson.model.Item) User(hudson.model.User) OrganizationFolder(jenkins.branch.OrganizationFolder) Authentication(org.acegisecurity.Authentication) Cause(hudson.model.Cause) ACL(hudson.security.ACL) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) Nonnull(javax.annotation.Nonnull)

Aggregations

GitHubSCMNavigator (org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 OrganizationFolder (jenkins.branch.OrganizationFolder)3 Cause (hudson.model.Cause)2 User (hudson.model.User)2 SCMNavigator (jenkins.scm.api.SCMNavigator)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 Item (hudson.model.Item)1 TopLevelItem (hudson.model.TopLevelItem)1 ACL (hudson.security.ACL)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 IOException (java.io.IOException)1 Nonnull (javax.annotation.Nonnull)1 CustomOrganizationFolderDescriptor (jenkins.branch.CustomOrganizationFolderDescriptor)1