Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import hudson.util.ListBoxModel;
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar;
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink;
import io.jenkins.plugins.gitlabbranchsource.helpers.Sleeper;
import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
Expand Down Expand Up @@ -118,6 +119,10 @@
private transient Project gitlabProject;
private Long projectId;

private static final Integer MAX_RETRIES = 5;

private static final Integer INITIAL_DELAY_MS = 5000;

/**
* The cache of {@link ObjectMetadataAction} instances for each open MR.
*/
Expand Down Expand Up @@ -226,17 +231,51 @@
public HashMap<String, AccessLevel> getMembers() {
HashMap<String, AccessLevel> members = new HashMap<>();
try {
GitLabApi gitLabApi = apiBuilder(this.getOwner(), serverName);
for (Member m : gitLabApi.getProjectApi().getAllMembers(projectPath)) {
for (Member m : getMembersWithRetries()) {
members.put(m.getUsername(), m.getAccessLevel());
}
} catch (GitLabApiException e) {
LOGGER.log(Level.WARNING, "Exception while fetching members" + e, e);
return new HashMap<>();
} catch (InterruptedException e) {
throw new RuntimeException(e);

Check warning on line 241 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 240-241 are not covered by tests
}
return members;
}

private List<Member> getMembersWithRetries() throws GitLabApiException, InterruptedException {
int delay = INITIAL_DELAY_MS;
int attemptNb = 0;
final Sleeper sleeper = new Sleeper();
GitLabApi gitLabApi = apiBuilder(this.getOwner(), serverName);
while (true) {
try {
return gitLabApi.getProjectApi().getAllMembers(projectPath);
} catch (GitLabApiException | RuntimeException e) {
if (isRateLimitException(e)) {

Check warning on line 255 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 255 is only partially covered, one branch is missing
sleeper.sleep(delay);
delay *= 2;
attemptNb++;
if (attemptNb > MAX_RETRIES) {
throw e;
}
continue;
}
throw e;

Check warning on line 264 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 264 is not covered by tests
}
}
}

private static boolean isRateLimitException(Exception e) {
if (e instanceof GitLabApiException) {

Check warning on line 270 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 270 is only partially covered, one branch is missing
return ((GitLabApiException) e).getHttpStatus() == 429;

Check warning on line 271 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 271 is only partially covered, one branch is missing
} else if (e.getCause() != null && e.getCause().getClass().isAssignableFrom(GitLabApiException.class)) {
GitLabApiException cause = (GitLabApiException) e.getCause();
return cause.getHttpStatus() == 429;
}
return false;

Check warning on line 276 in src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 272-276 are not covered by tests
}

public Long getProjectId() {
return projectId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.jenkins.plugins.gitlabbranchsource.helpers;

public class Sleeper {

public void sleep(int millis) throws InterruptedException {
Thread.sleep(millis);
}

Check warning on line 7 in src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/Sleeper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 3-7 are not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@
import hudson.security.AccessControlled;
import hudson.util.StreamTaskListener;
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper;
import io.jenkins.plugins.gitlabbranchsource.helpers.Sleeper;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import jenkins.branch.BranchSource;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMSourceOwner;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.MergeRequestApi;
import org.gitlab4j.api.ProjectApi;
import org.gitlab4j.api.RepositoryApi;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

Expand All @@ -39,6 +49,22 @@ public class GitLabSCMSourceTest {
@ClassRule
public static JenkinsRule j = new JenkinsRule();

private MockedStatic<GitLabHelper> utilities;

private MockedConstruction<Sleeper> sleeperMockedConstruction;

@Before
public void setUp() {
utilities = Mockito.mockStatic(GitLabHelper.class);
sleeperMockedConstruction = Mockito.mockConstruction(Sleeper.class);
}

@After
public void tearDown() {
utilities.close();
sleeperMockedConstruction.close();
}

@Test
public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
Expand All @@ -49,22 +75,103 @@ public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOEx
Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi);
Mockito.when(gitLabApi.getRepositoryApi()).thenReturn(repoApi);
Mockito.when(projectApi.getProject(any())).thenReturn(new Project());
try (MockedStatic<GitLabHelper> utilities = Mockito.mockStatic(GitLabHelper.class)) {
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
BranchSource source = new BranchSource(sb.build());
source.getSource()
.setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1)));
project.getSourcesList().add(source);
ByteArrayOutputStream out = new ByteArrayOutputStream();
final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8);
Set<SCMHead> scmHead = source.getSource().fetch(listener);
assertEquals(0, scmHead.size());
}
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
BranchSource source = new BranchSource(sb.build());
source.getSource()
.setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1)));
project.getSourcesList().add(source);
ByteArrayOutputStream out = new ByteArrayOutputStream();
final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8);
Set<SCMHead> scmHead = source.getSource().fetch(listener);
assertEquals(0, scmHead.size());
}

@Test
public void testGetMembersWithNoRetries() throws GitLabApiException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi);
Member mockMember = Mockito.mock(Member.class);
Mockito.when(mockMember.getUsername()).thenReturn("example.user");
Mockito.when(mockMember.getAccessLevel()).thenReturn(AccessLevel.DEVELOPER);
SCMSourceOwner mockOwner = Mockito.mock(SCMSourceOwner.class);
Mockito.when(projectApi.getAllMembers("group/project")).thenReturn(List.of(mockMember));
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
GitLabSCMSource source = sb.build();
source.setOwner(mockOwner);
assertEquals(Map.of("example.user", AccessLevel.DEVELOPER), source.getMembers());
Sleeper sleeper = sleeperMockedConstruction.constructed().get(0);
Mockito.verifyNoInteractions(sleeper);
}

@Test
public void testGetMembersWithAllRetries() throws GitLabApiException, InterruptedException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi);
SCMSourceOwner mockOwner = Mockito.mock(SCMSourceOwner.class);
GitLabApiException rateLimitException = new GitLabApiException("Rate limit", 429);
Mockito.when(projectApi.getAllMembers("group/project")).thenThrow(rateLimitException);
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
GitLabSCMSource source = sb.build();
source.setOwner(mockOwner);
assertEquals(Map.of(), source.getMembers());
Sleeper sleeper = sleeperMockedConstruction.constructed().get(0);
Mockito.verify(sleeper, Mockito.times(1)).sleep(5000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(10000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(20000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(40000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(80000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(160000);
Mockito.verifyNoMoreInteractions(sleeper);
}

@Test
public void testGetMembersWithSomeRetries() throws GitLabApiException, InterruptedException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi);
GitLabApiException rateLimitException = new GitLabApiException("Rate limit", 429);
Member mockMember = Mockito.mock(Member.class);
Mockito.when(mockMember.getUsername()).thenReturn("example.user");
Mockito.when(mockMember.getAccessLevel()).thenReturn(AccessLevel.DEVELOPER);
SCMSourceOwner mockOwner = Mockito.mock(SCMSourceOwner.class);
AtomicInteger counter = new AtomicInteger();
Mockito.when(projectApi.getAllMembers("group/project")).thenAnswer((input) -> {
if (counter.getAndIncrement() < 3) {
throw rateLimitException;
}
return List.of(mockMember);
});
utilities
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString()))
.thenReturn(gitLabApi);
GitLabServers.get().addServer(new GitLabServer("", SERVER, ""));
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project");
GitLabSCMSource source = sb.build();
source.setOwner(mockOwner);
assertEquals(Map.of("example.user", AccessLevel.DEVELOPER), source.getMembers());
Sleeper sleeper = sleeperMockedConstruction.constructed().get(0);
Mockito.verify(sleeper, Mockito.times(1)).sleep(5000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(10000);
Mockito.verify(sleeper, Mockito.times(1)).sleep(20000);
Mockito.verifyNoMoreInteractions(sleeper);
}
}
Loading