Search in sources :

Example 1 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class GitPipelineUpdateRequest method getGitScmSource.

@SuppressWarnings("unchecked")
private BranchSource getGitScmSource(MultiBranchProject mbp) {
    String sourceUri = null;
    String credentialId = null;
    if (scmConfig != null) {
        sourceUri = scmConfig.getUri();
        List<ErrorMessage.Error> errors = new ArrayList<>();
        StandardCredentials credentials = null;
        if (scmConfig.getCredentialId() != null) {
            credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
            if (credentials == null) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
            }
        }
        if (sourceUri != null) {
            errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
        }
        credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();
        if (!errors.isEmpty()) {
            throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").addAll(errors));
        }
    }
    PersistedList<BranchSource> sources = mbp.getSourcesList();
    for (BranchSource source : sources) {
        if (source.getSource() instanceof GitSCMSource) {
            GitSCMSource gitSCMSource = (GitSCMSource) source.getSource();
            String remote = gitSCMSource.getRemote();
            if (sourceUri != null && !sourceUri.equals(gitSCMSource.getRemote())) {
                remote = sourceUri;
            }
            String cred = gitSCMSource.getCredentialsId();
            if (!gitSCMSource.getCredentialsId().equals(credentialId)) {
                cred = credentialId;
            }
            GitSCMSource s = new GitSCMSource(gitSCMSource.getId(), remote, cred, gitSCMSource.getIncludes(), gitSCMSource.getExcludes(), gitSCMSource.isIgnoreOnPushNotifications());
            s.setOwner(mbp);
            return new BranchSource(s);
        }
    }
    if (sourceUri != null) {
        //if no scm sources in this MBP project, add a new one using passed sourceUri
        return new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) GitSCMSource(jenkins.plugins.git.GitSCMSource) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BranchSource(jenkins.branch.BranchSource) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 2 with BranchSource

use of jenkins.branch.BranchSource in project gitlab-branch-source-plugin by Argelbargel.

the class GitLabSCMItemListener method updateBranchBuildStrategies.

private boolean updateBranchBuildStrategies(MultiBranchProject<?, ?> project) {
    boolean changed = false;
    for (BranchSource branchSource : project.getSources()) {
        if (GitLabSCMBranchBuildStrategy.INSTANCE.isApplicable(branchSource)) {
            List<BranchBuildStrategy> strategies = new ArrayList<>(branchSource.getBuildStrategies());
            if (!strategies.contains(GitLabSCMBranchBuildStrategy.INSTANCE)) {
                strategies.add(GitLabSCMBranchBuildStrategy.INSTANCE);
                branchSource.setBuildStrategies(strategies);
                changed = true;
            }
        }
    }
    return changed;
}
Also used : BranchBuildStrategy(jenkins.branch.BranchBuildStrategy) ArrayList(java.util.ArrayList) BranchSource(jenkins.branch.BranchSource)

Example 3 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class SseEventTest method multiBranchJobEventsWithCustomOrg.

@Test
public void multiBranchJobEventsWithCustomOrg() throws Exception {
    MockFolder folder = j.createFolder("TestOrgFolderName");
    assertNotNull(folder);
    setupScm();
    final OneShotEvent success = new OneShotEvent();
    final Boolean[] pipelineNameMatched = { null };
    final Boolean[] mbpPipelineNameMatched = { null };
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            if ("job".equals(message.get(jenkins_channel))) {
                if ("org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject".equals(message.get(jenkins_object_type))) {
                    if ("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        mbpPipelineNameMatched[0] = true;
                    } else {
                        mbpPipelineNameMatched[0] = false;
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                    if ("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        pipelineNameMatched[0] = true;
                    } else {
                        pipelineNameMatched[0] = false;
                    }
                }
            }
            if (pipelineNameMatched[0] != null && mbpPipelineNameMatched[0] != null) {
                success.signal();
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");
    final WorkflowMultiBranchProject mp = folder.createProject(WorkflowMultiBranchProject.class, "pipeline1");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    WorkflowJob p = mp.getItem("master");
    if (p == null) {
        mp.getIndexing().writeWholeLogTo(System.out);
        fail("master project not found");
    }
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(2, mp.getItems().size());
    success.block(5000);
    con.close();
    if (success.isSignaled()) {
        assertTrue(pipelineNameMatched[0]);
        assertTrue(mbpPipelineNameMatched[0]);
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) MockFolder(org.jvnet.hudson.test.MockFolder) BranchSource(jenkins.branch.BranchSource) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Example 4 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method encodedStepDescription.

@Test
public void encodedStepDescription() throws Exception {
    setupScm("pipeline {\n" + "  agent any\n" + "  stages {\n" + "    stage('Build') {\n" + "      steps {\n" + "          sh 'echo \"\\033[32m some text \\033[0m\"'    \n" + "      }\n" + "    }\n" + "  }\n" + "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    j.waitUntilNoActivity();
    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());
    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));
    Assert.assertEquals(1, stages.size());
    Assert.assertEquals("Build", stages.get(0).getDisplayName());
    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(1, resp.size());
    Assert.assertEquals("Build", resp.get(0).get("displayName"));
    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(2, resp.size());
    assertNotNull(resp.get(0).get("displayName"));
    assertEquals("Shell Script", resp.get(1).get("displayName"));
    assertEquals("echo \"\u001B[32m some text \u001B[0m\"", resp.get(1).get("displayDescription"));
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) RunList(hudson.util.RunList) List(java.util.List) ExtensionList(hudson.ExtensionList) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 5 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class MultiBranchTest method testMultiBranchPipelineBranchUnsecurePermissions.

@Test
public void testMultiBranchPipelineBranchUnsecurePermissions() throws IOException, ExecutionException, InterruptedException {
    MockFolder folder1 = j.createFolder("folder1");
    WorkflowMultiBranchProject mp = folder1.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    Map r = get("/organizations/jenkins/pipelines/folder1/pipelines/p/");
    Map<String, Boolean> permissions = (Map<String, Boolean>) r.get("permissions");
    Assert.assertTrue(permissions.get("create"));
    Assert.assertTrue(permissions.get("read"));
    Assert.assertTrue(permissions.get("start"));
    Assert.assertTrue(permissions.get("stop"));
    r = get("/organizations/jenkins/pipelines/folder1/pipelines/p/branches/master/");
    permissions = (Map<String, Boolean>) r.get("permissions");
    Assert.assertTrue(permissions.get("create"));
    Assert.assertTrue(permissions.get("start"));
    Assert.assertTrue(permissions.get("stop"));
    Assert.assertTrue(permissions.get("read"));
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) MockFolder(org.jvnet.hudson.test.MockFolder) BranchSource(jenkins.branch.BranchSource) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Map(java.util.Map) Test(org.junit.Test)

Aggregations

BranchSource (jenkins.branch.BranchSource)42 GitSCMSource (jenkins.plugins.git.GitSCMSource)39 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)39 SCMSource (jenkins.scm.api.SCMSource)35 Test (org.junit.Test)34 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)32 Map (java.util.Map)26 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)25 List (java.util.List)16 ArrayList (java.util.ArrayList)15 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)13 User (hudson.model.User)6 Queue (hudson.model.Queue)5 MockFolder (org.jvnet.hudson.test.MockFolder)5 ExtensionList (hudson.ExtensionList)3 FreeStyleProject (hudson.model.FreeStyleProject)3 RunList (hudson.util.RunList)3 ImmutableList (com.google.common.collect.ImmutableList)2 Cause (hudson.model.Cause)2 OneShotEvent (hudson.util.OneShotEvent)2