Search in sources :

Example 1 with SCMNavigator

use of jenkins.scm.api.SCMNavigator 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 SCMNavigator

use of jenkins.scm.api.SCMNavigator 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)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 SCMNavigator (jenkins.scm.api.SCMNavigator)2 GitHubSCMNavigator (org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 MultiBranchProject (jenkins.branch.MultiBranchProject)1 OrganizationFolder (jenkins.branch.OrganizationFolder)1 GitHubSCMSource (org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)1