Skip to content

Commit

Permalink
chore: remove duplicate accesstoken member
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Shearin <[email protected]>
  • Loading branch information
ashearin authored and jhoward-lm committed Feb 13, 2025
1 parent bd0f314 commit 6cad00c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,14 @@ public class GitLabSyncer extends AbstractIntegrationPoint implements Permission
private static final String GENERAL_GROUP = GENERAL_BASE_URL.getGroupName();

Check warning on line 43 in src/main/java/org/dependencytrack/integrations/gitlab/GitLabSyncer.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/dependencytrack/integrations/gitlab/GitLabSyncer.java#L43

Avoid unused private fields such as 'GENERAL_GROUP'.
private static final String ROLE_CLAIM_PREFIX = "https://gitlab.org/claims/groups/";

Check warning on line 44 in src/main/java/org/dependencytrack/integrations/gitlab/GitLabSyncer.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/dependencytrack/integrations/gitlab/GitLabSyncer.java#L44

Avoid unused private fields such as 'ROLE_CLAIM_PREFIX'.

private final String accessToken;
private final OidcUser user;
private GitLabClient gitLabClient;

public GitLabSyncer(final String accessToken, final OidcUser user, final GitLabClient gitlabClient) {
this.accessToken = accessToken;
public GitLabSyncer(final OidcUser user, final GitLabClient gitlabClient) {
this.user = user;
this.gitLabClient = gitlabClient;
}

public String getAccessToken() {
return accessToken;
}

@Override
public String name() {
return "GitLab";
Expand All @@ -76,24 +70,26 @@ public boolean isEnabled() {

@Override
public void synchronize() {
final URI gitLabUrl = URI.create(Config.getInstance().getProperty(Config.AlpineKey.OIDC_ISSUER));
gitLabClient = new GitLabClient(this, gitLabUrl, accessToken);

List<GitLabProject> gitLabProjects = gitLabClient.getGitLabProjects();
List<Project> projects = createProjects(gitLabProjects);
List<Team> teams = projects.stream()
.flatMap(project -> createProjectTeams(project).stream())
.toList();
List<String> teamNames = gitLabProjects.stream()
.map(gitLabProject -> "%s_%s".formatted(
gitLabProject.getFullPath(),
gitLabProject.getMaxAccessLevel().getStringValue().toString()))
.toList();

qm.addUserToTeams(qm.getOidcUser(user.getUsername()), teamNames);

teams = teams.stream().map(team -> qm.updateTeam(team)).toList();
projects = projects.stream().map(project -> qm.updateProject(project, false)).toList();
try {
List<GitLabProject> gitLabProjects = gitLabClient.getGitLabProjects();
List<Project> projects = createProjects(gitLabProjects);
List<Team> teams = projects.stream()
.flatMap(project -> createProjectTeams(project).stream())
.toList();
List<String> teamNames = gitLabProjects.stream()
.map(gitLabProject -> "%s_%s".formatted(
gitLabProject.getFullPath(),
gitLabProject.getMaxAccessLevel().getStringValue().toString()))
.toList();

qm.addUserToTeams(qm.getOidcUser(user.getUsername()), teamNames);

teams = teams.stream().map(team -> qm.updateTeam(team)).toList();
projects = projects.stream().map(project -> qm.updateProject(project, false)).toList();
} catch (IOException | URISyntaxException ex) {
LOGGER.error("An error occurred while querying GitLab GraphQL API", ex);
handleException(LOGGER, ex);
}
}

private List<Project> createProjects(List<GitLabProject> gitLabProjects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void inform(final Event event) {

try (QueryManager qm = new QueryManager()) {
GitLabClient gitLabClient = new GitLabClient(accessToken);
GitLabSyncer syncer = new GitLabSyncer(accessToken, user, gitLabClient);
GitLabSyncer syncer = new GitLabSyncer(user, gitLabClient);
syncer.setQueryManager(qm);
syncer.synchronize();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class GitLabSyncerTest extends PersistenceCapableTest {
*/
@Test
public void testIntegrationMetadata() {
GitLabSyncer extension = new GitLabSyncer("test_token", user, gitLabClient);
GitLabSyncer extension = new GitLabSyncer(user, gitLabClient);
Assert.assertEquals("GitLab", extension.name());
Assert.assertEquals("Synchronizes user permissions from connected GitLab instance", extension.description());
}
Expand All @@ -81,7 +81,7 @@ public void testIsEnabled() {
IConfigProperty.PropertyType.BOOLEAN,
null
);
GitLabSyncer extension = new GitLabSyncer("test_token", user, gitLabClient);
GitLabSyncer extension = new GitLabSyncer(user, gitLabClient);
extension.setQueryManager(qm);
Assert.assertTrue(extension.isEnabled());
}
Expand All @@ -99,7 +99,7 @@ public void testIsDisabled() {
IConfigProperty.PropertyType.BOOLEAN,
null
);
GitLabSyncer extension = new GitLabSyncer("test_token", user, gitLabClient);
GitLabSyncer extension = new GitLabSyncer(user, gitLabClient);
extension.setQueryManager(qm);
Assert.assertFalse(extension.isEnabled());
}
Expand All @@ -123,7 +123,7 @@ public void testSynchronizeSuccess() {
OidcUser testUser = new OidcUser();
testUser.setUsername("test_user");
GitLabClient mockClient = mock(GitLabClient.class);
GitLabSyncer extension = new GitLabSyncer("test_token", testUser, mockClient);
GitLabSyncer extension = new GitLabSyncer(testUser, mockClient);
extension.setQueryManager(qm);
try{
when(mockClient.getGitLabProjects()).thenReturn(Arrays.asList(new GitLabProject("project1", "this/test/project1", GitLabRole.MAINTAINER),
Expand Down

0 comments on commit 6cad00c

Please sign in to comment.