Skip to content

Commit

Permalink
Merge pull request #1139 from Dohbedoh/JENKINS-64803
Browse files Browse the repository at this point in the history
[JENKINS-64803] Fix tags retrieval with folder scoped credentials
  • Loading branch information
MarkEWaite committed Oct 22, 2021
2 parents f4a08e0 + 2c44edb commit 726274d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -156,6 +156,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Expand Up @@ -326,15 +326,37 @@ private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest>
@NonNull C context,
@NonNull TaskListener listener,
boolean prune)
throws IOException, InterruptedException {
return doRetrieve(retriever, context, listener, prune, getOwner(), false);
}

@NonNull
private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest> T doRetrieve(Retriever<T> retriever,
@NonNull C context,
@NonNull TaskListener listener,
boolean prune,
@CheckForNull Item retrieveContext)
throws IOException, InterruptedException {
return doRetrieve(retriever, context, listener, prune, retrieveContext, false);
}

@NonNull
private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest> T doRetrieve(Retriever<T> retriever,
@NonNull C context,
@NonNull TaskListener listener,
boolean prune,
boolean delayFetch)
throws IOException, InterruptedException {
return doRetrieve(retriever, context, listener, prune, false);
return doRetrieve(retriever, context, listener, prune, getOwner(), delayFetch);
}

@NonNull
private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest> T doRetrieve(Retriever<T> retriever,
@NonNull C context,
@NonNull TaskListener listener,
boolean prune, boolean delayFetch)
boolean prune,
@CheckForNull Item retrieveContext,
boolean delayFetch)
throws IOException, InterruptedException {
String cacheEntry = getCacheEntry();
Lock cacheLock = getCacheLock(cacheEntry);
Expand All @@ -347,7 +369,7 @@ private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest>
git.using(tool.getGitExe());
}
GitClient client = git.getClient();
client.addDefaultCredentials(getCredentials());
client.addDefaultCredentials(getCredentials(retrieveContext));
if (!client.hasGitRepo(false)) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
Expand Down Expand Up @@ -971,7 +993,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
}
},
context,
listener, pruneRefs);
listener, pruneRefs, retrieveContext);
}
// Pokémon!... Got to catch them all
listener.getLogger().printf("Could not find %s in remote references. "
Expand Down Expand Up @@ -1017,7 +1039,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
}
},
context,
listener, pruneRefs);
listener, pruneRefs, retrieveContext);
}

/**
Expand Down
Expand Up @@ -6,6 +6,7 @@
import hudson.scm.ChangeLogSet;
import hudson.scm.EditType;
import org.jenkinsci.plugins.gitclient.JGitTool;
import org.junit.BeforeClass;
import org.junit.Test;

import java.net.URL;
Expand All @@ -20,26 +21,23 @@

public class TFS2013GitRepositoryBrowserTest {

private static final String projectName = "fisheyeProjectName";

private final String repoUrl;
private final GitChangeSetSample sample;

public TFS2013GitRepositoryBrowserTest() {
this.repoUrl = "http://tfs/tfs/project/_git/repo";
sample = new GitChangeSetSample(false);
private static final String repoUrl = "http://tfs/tfs/project/_git/repo";
private static final GitChangeSetSample sample = new GitChangeSetSample(false);

@BeforeClass
public static void setUp() {
GitSCM scm = new GitSCM(
Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)),
new ArrayList<>(),
null, JGitTool.MAGIC_EXENAME,
Collections.<GitSCMExtension>emptyList());

AbstractProject project = mock(AbstractProject.class);
AbstractBuild build = mock(AbstractBuild.class);

when(project.getScm()).thenReturn(scm);
when(build.getProject()).thenReturn(project);
when(build.getParent()).thenReturn(project);

sample.changeSet.setParent(ChangeLogSet.createEmpty((Run) build));
}
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java
@@ -1,5 +1,13 @@
package jenkins.plugins.git;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.FilePath;
Expand Down Expand Up @@ -53,11 +61,15 @@
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.TestJGitAPIImpl;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.hamcrest.MatcherAssert.*;
Expand All @@ -67,6 +79,15 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -267,6 +288,52 @@ public void retrieveRevisions() throws Exception {
assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("dev", "master", "annotated", "lightweight"));
}

@Issue("JENKINS-64803")
@Test
public void retrieveTags_folderScopedCredentials() throws Exception {
sampleRepo.init();
sampleRepo.git("checkout", "-b", "dev");
sampleRepo.write("file", "modified");
sampleRepo.git("commit", "--all", "--message=dev");
sampleRepo.git("tag", "lightweight");
GitSCMSource source = new GitSCMSource(sampleRepo.toString());
TaskListener listener = StreamTaskListener.fromStderr();

// Create a Folder and add a folder credentials
Folder f = r.jenkins.createProject(Folder.class, "test");
Iterable<CredentialsStore> stores = CredentialsProvider.lookupStores(f);
CredentialsStore folderStore = null;
for (CredentialsStore s : stores) {
if (s.getProvider() instanceof FolderCredentialsProvider && s.getContext() == f) {
folderStore = s;
break;
}
}
assert folderStore != null;
String fCredentialsId = "fcreds";
StandardCredentials fCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL,
fCredentialsId, "fcreds", "user", "password");
folderStore.addCredentials(Domain.global(), fCredentials);
folderStore.save();
WorkflowJob p = f.createProject(WorkflowJob.class, "wjob");

source.setTraits(new ArrayList<>());
source.setCredentialsId(fCredentialsId);

Git git = mock(Git.class, CALLS_REAL_METHODS);
GitClient gitClient = spy(git.getClient());
// Spy on GitClient methods
try (MockedStatic<Git> gitMock = mockStatic(Git.class, CALLS_REAL_METHODS)) {
gitMock.when(() -> Git.with(any(), any())).thenReturn(git);
doReturn(gitClient).when(git).getClient();
SCMRevision rev = source.fetch("lightweight", listener, p);
assertThat(rev, notNullValue());
assertThat(rev.getHead().toString(), equalTo("SCMHead{'lightweight'}"));
verify(gitClient, times(0)).addDefaultCredentials(null);
verify(gitClient, atLeastOnce()).addDefaultCredentials(fCredentials);
}
}

@Issue("JENKINS-47824")
@Test
public void retrieveByName() throws Exception {
Expand Down

0 comments on commit 726274d

Please sign in to comment.