public void run() { for (SCMSource scmSource : ((SCMSourceOwner) project).getSCMSources()) { if (scmSource instanceof GitSCMSource) { GitSCMSource gitSCMSource = (GitSCMSource) scmSource; try { if (new URIish(gitSCMSource.getRemote()).equals(new URIish(gitSCMSource.getRemote()))) { if (!gitSCMSource.isIgnoreOnPushNotifications()) { LOGGER.log(Level.FINE, "Notify scmSourceOwner {0} about changes for {1}", toArray(project.getName(), gitSCMSource.getRemote())); ((SCMSourceOwner) project).onSCMSourceUpdated(scmSource); } else { LOGGER.log(Level.FINE, "Ignore on push notification for scmSourceOwner {0} about changes for {1}", toArray(project.getName(), gitSCMSource.getRemote())); } } } catch (URISyntaxException e) { // nothing to do } } } } }
projectObserver.addSource(new GitSCMSource(getId() + "::" + kid.getName(), origin, "", "*", "", false)); projectObserver.complete();
@Override public boolean isMatch(SCMSource source) { if (source instanceof GitSCMSource) { GitSCMSource git = (GitSCMSource) source; GitSCMSourceContext ctx = new GitSCMSourceContext<>(null, SCMHeadObserver.none()) .withTraits(git.getTraits()); if (ctx.ignoreOnPushNotifications()) { return false; } URIish remote; try { remote = new URIish(git.getRemote()); } catch (URISyntaxException e) { // ignore return false; } if (GitStatus.looselyMatches(u, remote)) { notified[0] = true; return true; } return false; } return false; }
@Deprecated @Restricted(NoExternalUse.class) @RestrictedSince("3.4.0") public GitSCMSource(String id, String remote, String credentialsId, String remoteName, String rawRefSpecs, String includes, String excludes, boolean ignoreOnPushNotifications) { super(id); this.remote = remote; this.credentialsId = credentialsId; List<SCMSourceTrait> traits = new ArrayList<>(); traits.add(new BranchDiscoveryTrait()); if (!DEFAULT_INCLUDES.equals(includes) || !DEFAULT_EXCLUDES.equals(excludes)) { traits.add(new WildcardSCMHeadFilterTrait(includes, excludes)); } if (!DEFAULT_REMOTE_NAME.equals(remoteName) && StringUtils.isNotBlank(remoteName)) { traits.add(new RemoteNameSCMSourceTrait(remoteName)); } if (ignoreOnPushNotifications) { traits.add(new IgnoreOnPushNotificationTrait()); } RefSpecsSCMSourceTrait trait = asRefSpecsSCMSourceTrait(rawRefSpecs, remoteName); if (trait != null) { traits.add(trait); } setTraits(traits); }
@Override public void evaluate() throws Throwable { sampleRepo.init(); sampleRepo.git("checkout", "-b", "dev/main"); String script = "echo \"branch=${env.BRANCH_NAME}\"\n" + "node {\n" + " checkout scm\n" + " echo \"workspace=${pwd().replaceFirst('.+dev', 'dev')}\"\n" + "}"; sampleRepo.write("Jenkinsfile", script); sampleRepo.git("add", "Jenkinsfile"); sampleRepo.git("commit", "--all", "--message=flow"); WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p"); GitSCMSource source = new GitSCMSource(sampleRepo.toString()); source.setTraits(Collections.singletonList(new BranchDiscoveryTrait())); mp.getSourcesList().add(new BranchSource(source)); WorkflowJob p = scheduleAndFindBranchProject(mp, "dev%2Fmain"); assertEquals(1, mp.getItems().size()); story.j.waitUntilNoActivity(); WorkflowRun b1 = p.getLastBuild(); assertEquals(1, b1.getNumber()); story.j.assertLogContains("branch=dev/main", b1); story.j.assertLogContains("workspace=dev_main", b1); verifyProject(p); sampleRepo.write("Jenkinsfile", script.replace("branch=", "Branch=")); } });
@Restricted(DoNotUse.class) @DataBoundSetter public void setBrowser(GitRepositoryBrowser browser) { List<SCMSourceTrait> traits = new ArrayList<>(this.traits); for (Iterator<SCMSourceTrait> iterator = traits.iterator(); iterator.hasNext(); ) { if (iterator.next() instanceof GitBrowserSCMSourceTrait) { iterator.remove(); } } if (browser != null) { traits.add(new GitBrowserSCMSourceTrait(browser)); } setTraits(traits); }
@Issue("JENKINS-46547") @Test public void multiplePipelinesExecutedInLibraryShouldFail() throws Exception { otherRepo.init(); otherRepo.write("vars/fromLib.groovy", pipelineSourceFromResources("libForMultiplePipelinesExecutedInLibrary")); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); LibraryConfiguration firstLib = new LibraryConfiguration("from-lib", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))); GlobalLibraries.get().setLibraries(Arrays.asList(firstLib)); expect(Result.FAILURE, "pipelineDefinedInLibrary") .logContains("java.lang.IllegalStateException: Only one pipeline { ... } block can be executed in a single run") .go(); }
traits.add(new IgnoreOnPushNotificationTrait()); RefSpecsSCMSourceTrait trait = asRefSpecsSCMSourceTrait(rawRefSpecs, remoteName); if (trait != null) { traits.add(trait); setTraits(traits);
@Restricted(DoNotUse.class) @DataBoundSetter public void setGitTool(String gitTool) { List<SCMSourceTrait> traits = new ArrayList<>(this.traits); gitTool = Util.fixEmptyAndTrim(gitTool); for (Iterator<SCMSourceTrait> iterator = traits.iterator(); iterator.hasNext(); ) { if (iterator.next() instanceof GitToolSCMSourceTrait) { iterator.remove(); } } if (gitTool != null) { traits.add(new GitToolSCMSourceTrait(gitTool)); } setTraits(traits); }
@Test public void serializationLibrariesGString() throws Exception { otherRepo.init(); otherRepo.write("vars/myecho.groovy", "def call() {echo 'something special'}"); otherRepo.write("vars/myecho.txt", "Says something very special!"); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); LibraryConfiguration firstLib = new LibraryConfiguration("echo-utils", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))); thirdRepo.init(); thirdRepo.write("vars/whereFrom.groovy", "def call() {echo 'from another library'}"); thirdRepo.write("vars/whereFrom.txt", "Says where it's from!"); thirdRepo.git("add", "vars"); thirdRepo.git("commit", "--message=init"); LibraryConfiguration secondLib = new LibraryConfiguration("test", new SCMSourceRetriever(new GitSCMSource(null, thirdRepo.toString(), "", "*", "", true))); secondLib.setDefaultVersion("master"); GlobalLibraries.get().setLibraries(Arrays.asList(firstLib, secondLib)); expect("serializationLibrariesGString") .logContains("something special", "from another library") .go(); }
@Issue("JENKINS-40642") @Test public void libraryAnnotation() throws Exception { otherRepo.init(); otherRepo.write("vars/myecho.groovy", "def call() {echo 'something special'}"); otherRepo.write("vars/myecho.txt", "Says something very special!"); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); GlobalLibraries.get().setLibraries(Collections.singletonList( new LibraryConfiguration("echo-utils", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))))); expect("libraryAnnotation") .logContains("something special") .go(); }
@Issue("JENKINS-46547") @Test public void pipelineDefinedInLibrary() throws Exception { otherRepo.init(); otherRepo.write("vars/fromLib.groovy", pipelineSourceFromResources("libForPipelineDefinedInLibrary")); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); LibraryConfiguration firstLib = new LibraryConfiguration("from-lib", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))); GlobalLibraries.get().setLibraries(Arrays.asList(firstLib)); expect("pipelineDefinedInLibrary") .logContains("[Pipeline] { (One)", "[Pipeline] { (Two)") .logNotContains("World") .go(); }
@Issue("JENKINS-38110") @Test public void librariesDirective() throws Exception { otherRepo.init(); otherRepo.write("vars/myecho.groovy", "def call() {echo 'something special'}"); otherRepo.write("vars/myecho.txt", "Says something very special!"); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); LibraryConfiguration firstLib = new LibraryConfiguration("echo-utils", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))); thirdRepo.init(); thirdRepo.write("vars/whereFrom.groovy", "def call() {echo 'from another library'}"); thirdRepo.write("vars/whereFrom.txt", "Says where it's from!"); thirdRepo.git("add", "vars"); thirdRepo.git("commit", "--message=init"); LibraryConfiguration secondLib = new LibraryConfiguration("whereFrom", new SCMSourceRetriever(new GitSCMSource(null, thirdRepo.toString(), "", "*", "", true))); secondLib.setDefaultVersion("master"); GlobalLibraries.get().setLibraries(Arrays.asList(firstLib, secondLib)); expect("librariesDirective") .logContains("something special", "from another library") .go(); }
@Issue("JENKINS-40657") @Test public void libraryObjectDefinedOutsidePipeline() throws Exception { otherRepo.init(); otherRepo.write("src/org/foo/Zot.groovy", "package org.foo;\n" + "\n" + "class Zot implements Serializable {\n" + " def steps\n" + " Zot(steps){\n" + " this.steps = steps\n" + " }\n" + " def echo(msg) {\n" + " steps.echo \"${msg}\"\n" + " }\n" + "}\n"); otherRepo.git("add", "src"); otherRepo.git("commit", "--message=init"); GlobalLibraries.get().setLibraries(Collections.singletonList( new LibraryConfiguration("zot-stuff", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))))); expect("libraryObjectDefinedOutsidePipeline") .logContains("hello"); }
@Issue("JENKINS-43035") @Test public void libraryObjectImportInWhenExpr() throws Exception { otherRepo.init(); otherRepo.write("src/org/foo/Zot.groovy", "package org.foo;\n" + "\n" + "class Zot implements Serializable {\n" + " def steps\n" + " Zot(steps){\n" + " this.steps = steps\n" + " }\n" + " def echo(msg) {\n" + " steps.echo \"${msg}\"\n" + " }\n" + "}\n"); otherRepo.git("add", "src"); otherRepo.git("commit", "--message=init"); GlobalLibraries.get().setLibraries(Collections.singletonList( new LibraryConfiguration("zot-stuff", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))))); expect("libraryObjectImportInWhenExpr") .logContains("hello") .go(); }
@Issue("JENKINS-40657") @Test public void libraryObjectInScript() throws Exception { otherRepo.init(); otherRepo.write("src/org/foo/Zot.groovy", "package org.foo;\n" + "\n" + "class Zot implements Serializable {\n" + " def steps\n" + " Zot(steps){\n" + " this.steps = steps\n" + " }\n" + " def echo(msg) {\n" + " steps.echo \"${msg}\"\n" + " }\n" + "}\n"); otherRepo.git("add", "src"); otherRepo.git("commit", "--message=init"); GlobalLibraries.get().setLibraries(Collections.singletonList( new LibraryConfiguration("zot-stuff", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))))); expect("libraryObjectInScript") .logContains("hello") .go(); }
@Issue("JENKINS-46547") @Test public void pipelineDefinedInLibraryInFolder() throws Exception { otherRepo.init(); otherRepo.write("vars/fromLib.groovy", pipelineSourceFromResources("libForPipelineDefinedInLibrary")); otherRepo.git("add", "vars"); otherRepo.git("commit", "--message=init"); LibraryConfiguration firstLib = new LibraryConfiguration("from-lib", new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true))); Folder folder = j.jenkins.createProject(Folder.class, "libInFolder"); folder.getProperties().add(new FolderLibraries(Collections.singletonList(firstLib))); expect("pipelineDefinedInLibrary") .inFolder(folder) .logContains("[Pipeline] { (One)", "[Pipeline] { (Two)") .logNotContains("World") .go(); }