Java Code Examples for jenkins.plugins.git.AbstractGitSCMSource#SCMRevisionImpl
The following examples show how to use
jenkins.plugins.git.AbstractGitSCMSource#SCMRevisionImpl .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GithubNotificationConfig.java From github-autostatus-plugin with MIT License | 6 votes |
/** * Extracts the branch info from a build. * * @param build the build * @return true if branch info was extracted; false otherwise */ private Boolean extractBranchInfo(Run<?, ?> build) { SCMRevisionAction scmRevisionAction = build.getAction(SCMRevisionAction.class); if (null == scmRevisionAction) { return false; } SCMRevision revision = scmRevisionAction.getRevision(); if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) { branchName = ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHead().getName(); } else if (revision instanceof PullRequestSCMRevision) { PullRequestSCMHead pullRequestSCMHead = (PullRequestSCMHead) ((PullRequestSCMRevision) revision).getHead(); branchName = pullRequestSCMHead.getSourceBranch(); } return true; }
Example 2
Source File: GitHubSCMProbe.java From github-branch-source-plugin with MIT License | 6 votes |
@Override public SCMFile getRoot() { if (repo == null) { return null; } synchronized (this) { if (!open) { return null; } } String ref; if (revision != null) { if (revision.getHead() instanceof PullRequestSCMHead) { ref = this.ref; } else if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl){ ref = ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(); } else { ref = this.ref; } } else { ref = this.ref; } return new GitHubSCMFile(this, repo, ref); }
Example 3
Source File: GitHubSCMFileSystem.java From github-branch-source-plugin with MIT License | 6 votes |
/** * Constructor. * * @param gitHub the {@link GitHub} * @param repo the {@link GHRepository} * @param refName the ref name, e.g. {@code heads/branchName}, {@code tags/tagName}, {@code pull/N/head} or the SHA. * @param rev the optional revision. * @throws IOException if I/O errors occur. */ protected GitHubSCMFileSystem(GitHub gitHub, GHRepository repo, String refName, @CheckForNull SCMRevision rev) throws IOException { super(rev); this.gitHub = gitHub; this.open = true; this.repo = repo; if (rev != null) { if (rev.getHead() instanceof PullRequestSCMHead) { PullRequestSCMRevision prRev = (PullRequestSCMRevision) rev; PullRequestSCMHead pr = (PullRequestSCMHead) prRev.getHead(); if (pr.isMerge()) { this.ref = prRev.getMergeHash(); } else { this.ref = prRev.getPullHash(); } } else if (rev instanceof AbstractGitSCMSource.SCMRevisionImpl) { this.ref = ((AbstractGitSCMSource.SCMRevisionImpl) rev).getHash(); } else { this.ref = refName; } } else { this.ref = refName; } }
Example 4
Source File: GithubNotificationConfig.java From github-autostatus-plugin with MIT License | 5 votes |
/** * Extracts the SHA for the build. * * @param build the build * @return true if SHA was extracted; false if it could not be extracted */ private Boolean extractCommitSha(Run<?, ?> build) { SCMRevisionAction scmRevisionAction = build.getAction(SCMRevisionAction.class); if (null == scmRevisionAction) { log(Level.INFO, "Could not find commit sha - status will not be provided for this build"); return false; } SCMRevision revision = scmRevisionAction.getRevision(); if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) { this.shaString = ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(); } else if (revision instanceof PullRequestSCMRevision) { this.shaString = ((PullRequestSCMRevision) revision).getPullHash(); } return true; }
Example 5
Source File: GitHubStatusNotificationStep.java From pipeline-githubnotify-step-plugin with MIT License | 5 votes |
private String tryToInferSha() { SCMRevisionAction action = run.getAction(SCMRevisionAction.class); if (action != null) { SCMRevision revision = action.getRevision(); if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) { return ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(); } else if (revision instanceof PullRequestSCMRevision) { return ((PullRequestSCMRevision) revision).getPullHash(); } else { throw new IllegalArgumentException(UNABLE_TO_INFER_COMMIT); } } else { throw new IllegalArgumentException(UNABLE_TO_INFER_COMMIT); } }
Example 6
Source File: ChangeSCMRevision.java From gerrit-code-review-plugin with Apache License 2.0 | 4 votes |
ChangeSCMRevision(@Nonnull ChangeSCMHead head, @Nonnull String patchsetHash) { super(head, new AbstractGitSCMSource.SCMRevisionImpl(head.getTarget(), patchsetHash)); this.patchsetHash = patchsetHash; this.isFilteredByPendingChecks = !head.getPendingCheckerUuids().isEmpty(); }
Example 7
Source File: BranchSCMHead.java From github-branch-source-plugin with MIT License | 4 votes |
public MigrationImpl() { super(GitHubSCMSource.class, SCMHead.class, AbstractGitSCMSource.SCMRevisionImpl.class); }
Example 8
Source File: BranchSCMHead.java From github-branch-source-plugin with MIT License | 4 votes |
@Override public SCMRevision migrate(@NonNull GitHubSCMSource source, @NonNull AbstractGitSCMSource.SCMRevisionImpl revision) { return new AbstractGitSCMSource.SCMRevisionImpl(migrate(source, revision.getHead()), revision.getHash()); }
Example 9
Source File: GitHubSCMFileSystem.java From github-branch-source-plugin with MIT License | 4 votes |
/** * {@inheritDoc} */ @Override public boolean changesSince(SCMRevision revision, @NonNull OutputStream changeLogStream) throws UnsupportedOperationException, IOException, InterruptedException { if (Objects.equals(getRevision(), revision)) { // special case where somebody is asking one of two stupid questions: // 1. what has changed between the latest and the latest // 2. what has changed between the current revision and the current revision return false; } int count = 0; FastDateFormat iso = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZ"); StringBuilder log = new StringBuilder(1024); String endHash; if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) { endHash = ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash().toLowerCase(Locale.ENGLISH); } else { endHash = null; } // this is the format expected by GitSCM, so we need to format each GHCommit with the same format // commit %H%ntree %T%nparent %P%nauthor %aN <%aE> %ai%ncommitter %cN <%cE> %ci%n%n%w(76,4,4)%s%n%n%b for (GHCommit commit: repo.queryCommits().from(ref).pageSize(GitSCM.MAX_CHANGELOG).list()) { if (commit.getSHA1().toLowerCase(Locale.ENGLISH).equals(endHash)) { break; } log.setLength(0); log.append("commit ").append(commit.getSHA1()).append('\n'); log.append("tree ").append(commit.getTree().getSha()).append('\n'); log.append("parent"); for (String parent: commit.getParentSHA1s()) { log.append(' ').append(parent); } log.append('\n'); GHCommit.ShortInfo info = commit.getCommitShortInfo(); log.append("author ") .append(info.getAuthor().getName()) .append(" <") .append(info.getAuthor().getEmail()) .append("> ") .append(iso.format(info.getAuthoredDate())) .append('\n'); log.append("committer ") .append(info.getCommitter().getName()) .append(" <") .append(info.getCommitter().getEmail()) .append("> ") .append(iso.format(info.getCommitDate())) .append('\n'); log.append('\n'); String msg = info.getMessage(); if (msg.endsWith("\r\n")) { msg = msg.substring(0, msg.length() - 2); } else if (msg.endsWith("\n")) { msg = msg.substring(0, msg.length() - 1); } msg = msg.replace("\r\n", "\n").replace("\r", "\n").replace("\n", "\n "); log.append(" ").append(msg).append('\n'); changeLogStream.write(log.toString().getBytes(StandardCharsets.UTF_8)); changeLogStream.flush(); count++; if (count >= GitSCM.MAX_CHANGELOG) { break; } } return count > 0; }
Example 10
Source File: PullRequestSCMRevision.java From github-branch-source-plugin with MIT License | 4 votes |
PullRequestSCMRevision(@NonNull PullRequestSCMHead head, @NonNull String baseHash, @NonNull String pullHash, String mergeHash) { super(head, new AbstractGitSCMSource.SCMRevisionImpl(head.getTarget(), baseHash)); this.baseHash = baseHash; this.pullHash = pullHash; this.mergeHash = mergeHash; }
Example 11
Source File: GitHubSCMBuilderTest.java From github-branch-source-plugin with MIT License | 4 votes |
@Test public void given__server_branch_rev_anon__when__build__then__scmBuilt() throws Exception { createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git"); BranchSCMHead head = new BranchSCMHead("test-branch"); AbstractGitSCMSource.SCMRevisionImpl revision = new AbstractGitSCMSource.SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); source.setCredentialsId(null); GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); assertThat(instance.credentialsId(), is(nullValue())); assertThat(instance.head(), is(head)); assertThat(instance.revision(), is(revision)); assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); assertThat("expecting guess value until withGitHubRemote called", instance.remote(), is("https://github.test/tester/test-repo.git")); assertThat(instance.browser(), instanceOf(GithubWeb.class)); assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo")); instance.withGitHubRemote(); assertThat(instance.remote(), is("https://github.test/tester/test-repo.git")); GitSCM actual = instance.build(); assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo")); assertThat(actual.getGitTool(), nullValue()); assertThat(actual.getUserRemoteConfigs(), hasSize(1)); UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); assertThat(config.getName(), is("origin")); assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); assertThat(config.getUrl(), is("https://github.test/tester/test-repo.git")); assertThat(config.getCredentialsId(), is(nullValue())); RemoteConfig origin = actual.getRepositoryByName("origin"); assertThat(origin, notNullValue()); assertThat(origin.getURIs(), hasSize(1)); assertThat(origin.getURIs().get(0).toString(), is("https://github.test/tester/test-repo.git")); assertThat(origin.getFetchRefSpecs(), hasSize(1)); assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); assertThat(actual.getExtensions(), containsInAnyOrder( instanceOf(GitSCMSourceDefaults.class), instanceOf(BuildChooserSetting.class) )); BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); assertThat(chooser.getBuildChooser(), instanceOf(AbstractGitSCMSource.SpecificRevisionBuildChooser.class)); AbstractGitSCMSource.SpecificRevisionBuildChooser revChooser = (AbstractGitSCMSource.SpecificRevisionBuildChooser) chooser.getBuildChooser(); Collection<Revision> revisions = revChooser .getCandidateRevisions(false, "test-branch", Mockito.mock(GitClient.class), new LogTaskListener( Logger.getAnonymousLogger(), Level.FINEST), null, null); assertThat(revisions, hasSize(1)); assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); }
Example 12
Source File: GitHubSCMBuilderTest.java From github-branch-source-plugin with MIT License | 4 votes |
@Test public void given__server_branch_rev_userpass__when__build__then__scmBuilt() throws Exception { createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git"); BranchSCMHead head = new BranchSCMHead("test-branch"); AbstractGitSCMSource.SCMRevisionImpl revision = new AbstractGitSCMSource.SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); source.setCredentialsId("user-pass"); GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); assertThat(instance.credentialsId(), is("user-pass")); assertThat(instance.head(), is(head)); assertThat(instance.revision(), is(revision)); assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); assertThat("expecting guess value until withGitHubRemote called", instance.remote(), is("https://github.test/tester/test-repo.git")); assertThat(instance.browser(), instanceOf(GithubWeb.class)); assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo")); instance.withGitHubRemote(); assertThat(instance.remote(), is("https://github.test/tester/test-repo.git")); GitSCM actual = instance.build(); assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo")); assertThat(actual.getGitTool(), nullValue()); assertThat(actual.getUserRemoteConfigs(), hasSize(1)); UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); assertThat(config.getName(), is("origin")); assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); assertThat(config.getUrl(), is("https://github.test/tester/test-repo.git")); assertThat(config.getCredentialsId(), is("user-pass")); RemoteConfig origin = actual.getRepositoryByName("origin"); assertThat(origin, notNullValue()); assertThat(origin.getURIs(), hasSize(1)); assertThat(origin.getURIs().get(0).toString(), is("https://github.test/tester/test-repo.git")); assertThat(origin.getFetchRefSpecs(), hasSize(1)); assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); assertThat(actual.getExtensions(), containsInAnyOrder( instanceOf(GitSCMSourceDefaults.class), instanceOf(BuildChooserSetting.class) )); BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); assertThat(chooser.getBuildChooser(), instanceOf(AbstractGitSCMSource.SpecificRevisionBuildChooser.class)); AbstractGitSCMSource.SpecificRevisionBuildChooser revChooser = (AbstractGitSCMSource.SpecificRevisionBuildChooser) chooser.getBuildChooser(); Collection<Revision> revisions = revChooser .getCandidateRevisions(false, "test-branch", Mockito.mock(GitClient.class), new LogTaskListener( Logger.getAnonymousLogger(), Level.FINEST), null, null); assertThat(revisions, hasSize(1)); assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); }
Example 13
Source File: GitHubSCMBuilderTest.java From github-branch-source-plugin with MIT License | 4 votes |
@Test public void given__server_branch_rev_userkey__when__build__then__scmBuilt() throws Exception { createGitHubSCMSourceForTest(true, "https://github.test/tester/test-repo.git"); BranchSCMHead head = new BranchSCMHead("test-branch"); AbstractGitSCMSource.SCMRevisionImpl revision = new AbstractGitSCMSource.SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); source.setCredentialsId("user-key"); GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); assertThat(instance.credentialsId(), is("user-key")); assertThat(instance.head(), is(head)); assertThat(instance.revision(), is(revision)); assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); assertThat("expecting guess value until withGitHubRemote called", instance.remote(), is("https://github.test/tester/test-repo.git")); assertThat(instance.browser(), instanceOf(GithubWeb.class)); assertThat(instance.browser().getRepoUrl(), is("https://github.test/tester/test-repo")); instance.withGitHubRemote(); assertThat(instance.remote(), is("[email protected]:tester/test-repo.git")); GitSCM actual = instance.build(); assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); assertThat(actual.getBrowser().getRepoUrl(), is("https://github.test/tester/test-repo")); assertThat(actual.getGitTool(), nullValue()); assertThat(actual.getUserRemoteConfigs(), hasSize(1)); UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); assertThat(config.getName(), is("origin")); assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); assertThat(config.getUrl(), is("[email protected]:tester/test-repo.git")); assertThat(config.getCredentialsId(), is("user-key")); RemoteConfig origin = actual.getRepositoryByName("origin"); assertThat(origin, notNullValue()); assertThat(origin.getURIs(), hasSize(1)); assertThat(origin.getURIs().get(0).toString(), is("[email protected]:tester/test-repo.git")); assertThat(origin.getFetchRefSpecs(), hasSize(1)); assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); assertThat(actual.getExtensions(), containsInAnyOrder( instanceOf(GitSCMSourceDefaults.class), instanceOf(BuildChooserSetting.class) )); BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); assertThat(chooser.getBuildChooser(), instanceOf(AbstractGitSCMSource.SpecificRevisionBuildChooser.class)); AbstractGitSCMSource.SpecificRevisionBuildChooser revChooser = (AbstractGitSCMSource.SpecificRevisionBuildChooser) chooser.getBuildChooser(); Collection<Revision> revisions = revChooser .getCandidateRevisions(false, "test-branch", Mockito.mock(GitClient.class), new LogTaskListener( Logger.getAnonymousLogger(), Level.FINEST), null, null); assertThat(revisions, hasSize(1)); assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); }
Example 14
Source File: GitHubSCMFileSystemTest.java From github-branch-source-plugin with MIT License | 4 votes |
public GitHubSCMFileSystemTest(String revision) { this.revision = revision == null ? null : new AbstractGitSCMSource.SCMRevisionImpl(master, revision); }
Example 15
Source File: CommitStatusUpdater.java From gitlab-plugin with GNU General Public License v2.0 | 4 votes |
private static List<GitLabBranchBuild> retrieveGitlabProjectIds(Run<?, ?> build, EnvVars environment) { LOGGER.log(Level.INFO, "Retrieving gitlab project ids"); final List<GitLabBranchBuild> result = new ArrayList<>(); GitLabWebHookCause gitlabCause = build.getCause(GitLabWebHookCause.class); if (gitlabCause != null) { return Collections.singletonList(new GitLabBranchBuild( gitlabCause.getData().getSourceProjectId().toString(), gitlabCause.getData().getLastCommit())); } // Check upstream causes for GitLabWebHookCause List<GitLabBranchBuild> builds = findBuildsFromUpstreamCauses(build.getCauses()); if (!builds.isEmpty()) { return builds; } final GitLabClient gitLabClient = getClient(build); if (gitLabClient == null) { LOGGER.log(Level.WARNING, "No gitlab client found."); return result; } final List<BuildData> buildDatas = build.getActions(BuildData.class); if (CollectionUtils.isEmpty(buildDatas)) { LOGGER.log(Level.INFO, "Build does not contain build data."); return result; } if (buildDatas.size() == 1) { addGitLabBranchBuild(result, getBuildRevision(build), buildDatas.get(0).getRemoteUrls(), environment, gitLabClient); } else { final SCMRevisionAction scmRevisionAction = build.getAction(SCMRevisionAction.class); if (scmRevisionAction == null) { LOGGER.log(Level.INFO, "Build does not contain SCM revision action."); return result; } final SCMRevision scmRevision = scmRevisionAction.getRevision(); String scmRevisionHash = null; if (scmRevision instanceof AbstractGitSCMSource.SCMRevisionImpl) { if (scmRevision == null) { LOGGER.log(Level.INFO, "Build does not contain SCM revision object."); return result; } scmRevisionHash = ((AbstractGitSCMSource.SCMRevisionImpl) scmRevision).getHash(); for (final BuildData buildData : buildDatas) { for (final Entry<String, Build> buildByBranchName : buildData.getBuildsByBranchName().entrySet()) { if (buildByBranchName.getValue().getSHA1().equals(ObjectId.fromString(scmRevisionHash))) { addGitLabBranchBuild(result, scmRevisionHash, buildData.getRemoteUrls(), environment, gitLabClient); } } } } } return result; }
Example 16
Source File: GitLabCommitStatusPublisherTest.java From gitlab-plugin with GNU General Public License v2.0 | 4 votes |
private AbstractBuild mockBuildWithLibrary(String gitLabConnection, Result result, String... remoteUrls) { AbstractBuild build = mock(AbstractBuild.class); List<BuildData> buildDatas = new ArrayList<>(); BuildData buildData = mock(BuildData.class); SCMRevisionAction scmRevisionAction = mock(SCMRevisionAction.class); AbstractGitSCMSource.SCMRevisionImpl revisionImpl = mock(AbstractGitSCMSource.SCMRevisionImpl.class); when(build.getAction(SCMRevisionAction.class)).thenReturn(scmRevisionAction); when(scmRevisionAction.getRevision()).thenReturn(revisionImpl); when(revisionImpl.getHash()).thenReturn(SHA1); Revision revision = mock(Revision.class); when(revision.getSha1String()).thenReturn(SHA1); when(buildData.getLastBuiltRevision()).thenReturn(revision); when(buildData.getRemoteUrls()).thenReturn(new HashSet<>(Arrays.asList(remoteUrls))); Build gitBuild = mock(Build.class); when(gitBuild.getMarked()).thenReturn(revision); when(gitBuild.getSHA1()).thenReturn(ObjectId.fromString(SHA1)); when(buildData.getLastBuild(any(ObjectId.class))).thenReturn(gitBuild); Map<String, Build> buildsByBranchName = new HashMap<>(); buildsByBranchName.put("develop", gitBuild); when(buildData.getBuildsByBranchName()).thenReturn(buildsByBranchName); buildDatas.add(buildData); //Second build data (@librabry) BuildData buildDataLib = mock(BuildData.class); Revision revisionLib = mock(Revision.class); when(revisionLib.getSha1String()).thenReturn("SHALIB"); when(buildDataLib.getLastBuiltRevision()).thenReturn(revisionLib); Build gitBuildLib = mock(Build.class); when(gitBuildLib.getMarked()).thenReturn(revisionLib); when(buildDataLib.getLastBuild(any(ObjectId.class))).thenReturn(gitBuildLib); buildDatas.add(buildDataLib); when(build.getActions(BuildData.class)).thenReturn(buildDatas); when(build.getResult()).thenReturn(result); when(build.getUrl()).thenReturn(BUILD_URL); AbstractProject<?, ?> project = mock(AbstractProject.class); when(project.getProperty(GitLabConnectionProperty.class)).thenReturn(new GitLabConnectionProperty(gitLabConnection)); when(build.getProject()).thenReturn(project); EnvVars environment = mock(EnvVars.class); when(environment.expand(anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { return (String) invocation.getArguments()[0]; } }); try { when(build.getEnvironment(any(TaskListener.class))).thenReturn(environment); } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } return build; }