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;
}
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;
}
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]);
}
}
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"));
}
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"));
}
Aggregations