From bf1cf3c5fe977bcb845d86eadca0b2b8b8d30020 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Thu, 19 Oct 2023 14:41:22 +0200 Subject: [PATCH] SONAR-20700 Trigger a GH permission sync after project creation --- .../github/ImportGithubProjectActionIT.java | 5 +-- .../ce/queue/BranchReportSubmitterIT.java | 2 +- .../server/ce/queue/ReportSubmitterIT.java | 9 +++-- .../ws/github/ImportGithubProjectAction.java | 13 ++------ .../almsettings/ws/GithubProjectCreator.java | 14 +++++++- .../ws/GithubProjectCreatorFactory.java | 15 +++++---- .../ws/GithubProjectCreatorFactoryTest.java | 8 +++-- .../ws/GithubProjectCreatorTest.java | 33 +++++++++++++++---- 8 files changed, 67 insertions(+), 32 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java index 1d0ec3fe34d..685b8066580 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java @@ -137,8 +137,9 @@ public class ImportGithubProjectActionIT { private final GithubPermissionConverter githubPermissionConverter = mock(); private final GithubProjectCreatorFactory gitHubProjectCreatorFactory = new GithubProjectCreatorFactory(db.getDbClient(), - null, appClient, projectDefaultVisibility, projectKeyGenerator, userSession, componentUpdater, gitHubSettings, githubPermissionConverter, userPermissionUpdater, permissionService); - private final WsActionTester ws = new WsActionTester(new ImportGithubProjectAction(db.getDbClient(), managedProjectService, userSession, + null, appClient, projectDefaultVisibility, projectKeyGenerator, userSession, componentUpdater, gitHubSettings, githubPermissionConverter, userPermissionUpdater, permissionService, + managedProjectService); + private final WsActionTester ws = new WsActionTester(new ImportGithubProjectAction(db.getDbClient(), userSession, componentUpdater, importHelper, newCodeDefinitionResolver, defaultBranchNameResolver, gitHubProjectCreatorFactory)); @Before diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java index 7829cc02765..8030a43131e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java @@ -108,7 +108,7 @@ public class BranchReportSubmitterIT { private final BranchSupport branchSupport = spy(new BranchSupport(branchSupportDelegate)); private final DevOpsProjectCreatorFactory devOpsProjectCreatorFactory = new GithubProjectCreatorFactory(db.getDbClient(), null, - null, projectDefaultVisibility, null, userSession, componentUpdater, null, null, null, null); + null, projectDefaultVisibility, null, userSession, componentUpdater, null, null, null, null, null); private final ManagedInstanceService managedInstanceService = mock(); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), branchSupport, diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java index 3278b7cb776..6ca6de49c97 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java @@ -62,6 +62,7 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.management.ManagedInstanceService; +import org.sonar.server.management.ManagedProjectService; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionServiceImpl; import org.sonar.server.permission.PermissionTemplateService; @@ -119,12 +120,14 @@ public class ReportSubmitterIT { private final GitHubSettings gitHubSettings = mock(); private final ProjectKeyGenerator projectKeyGenerator = mock(); private final PermissionUpdater permissionUpdater = mock(); - private PermissionService permissionService = new PermissionServiceImpl(mock()); + private final PermissionService permissionService = new PermissionServiceImpl(mock()); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), mock(System2.class), permissionTemplateService, new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver, mock(PermissionUpdater.class), permissionService); + private final ManagedProjectService managedProjectService = mock(); private final GithubProjectCreatorFactory githubProjectCreatorFactory = new GithubProjectCreatorFactory(db.getDbClient(), githubGlobalSettingsValidator, - githubApplicationClient, projectDefaultVisibility, projectKeyGenerator, userSession, componentUpdater, gitHubSettings, null, permissionUpdater, permissionService); + githubApplicationClient, projectDefaultVisibility, projectKeyGenerator, userSession, componentUpdater, gitHubSettings, null, permissionUpdater, permissionService, + managedProjectService); private final DevOpsProjectCreatorFactory devOpsProjectCreatorFactory = new DelegatingDevOpsProjectCreatorFactory(Set.of(githubProjectCreatorFactory)); @@ -368,7 +371,7 @@ public class ReportSubmitterIT { managedInstanceService.isInstanceExternallyManaged(), userSession, mock(), null); DevOpsProjectCreator devOpsProjectCreator = spy(new GithubProjectCreator(db.getDbClient(), githubApplicationClient, null, projectKeyGenerator, - componentUpdater, permissionUpdater, permissionService, githubProjectCreationParameters)); + componentUpdater, permissionUpdater, permissionService, managedProjectService, githubProjectCreationParameters)); doReturn(Optional.of(devOpsProjectCreator)).when(devOpsProjectCreatorFactorySpy).getDevOpsProjectCreator(any(), eq(characteristics)); return devOpsProjectCreator; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java index 6d83128baa5..f3e70c84595 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java @@ -42,7 +42,6 @@ import org.sonar.server.almsettings.ws.DevOpsProjectDescriptor; import org.sonar.server.almsettings.ws.GithubProjectCreatorFactory; import org.sonar.server.component.ComponentCreationData; import org.sonar.server.component.ComponentUpdater; -import org.sonar.server.management.ManagedProjectService; import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver; import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.user.UserSession; @@ -65,7 +64,6 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { private final DbClient dbClient; - private final ManagedProjectService managedProjectService; private final UserSession userSession; private final ComponentUpdater componentUpdater; private final ImportHelper importHelper; @@ -77,12 +75,11 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { private final GithubProjectCreatorFactory githubProjectCreatorFactory; @Inject - public ImportGithubProjectAction(DbClient dbClient, ManagedProjectService managedProjectService, UserSession userSession, + public ImportGithubProjectAction(DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater, ImportHelper importHelper, NewCodeDefinitionResolver newCodeDefinitionResolver, DefaultBranchNameResolver defaultBranchNameResolver, GithubProjectCreatorFactory githubProjectCreatorFactory) { this.dbClient = dbClient; - this.managedProjectService = managedProjectService; this.userSession = userSession; this.componentUpdater = componentUpdater; this.importHelper = importHelper; @@ -145,9 +142,9 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null"); DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.GITHUB, url, repositoryKey); - Optional devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, almSettingDto, accessToken, devOpsProjectDescriptor); + DevOpsProjectCreator devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, almSettingDto, accessToken, devOpsProjectDescriptor); CreationMethod creationMethod = getCreationMethod(ALM_IMPORT, userSession.isAuthenticatedBrowserSession()); - ComponentCreationData componentCreationData = devOpsProjectCreator.get().createProjectAndBindToDevOpsPlatform(dbSession, creationMethod, null); + ComponentCreationData componentCreationData = devOpsProjectCreator.createProjectAndBindToDevOpsPlatform(dbSession, creationMethod, null); checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue); @@ -161,10 +158,6 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction { } componentUpdater.commitAndIndex(dbSession, componentCreationData); - - String userUuid = requireNonNull(userSession.getUuid()); - managedProjectService.queuePermissionSyncTask(userUuid, mainBranchDto.getUuid(), projectDto.getUuid()); - return toCreateResponse(projectDto); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java index d3e56a58f28..a25d443ca16 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java @@ -35,6 +35,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.alm.setting.ProjectAlmSettingDto; +import org.sonar.db.component.BranchDto; import org.sonar.db.project.CreationMethod; import org.sonar.db.project.ProjectDto; import org.sonar.db.provisioning.GithubPermissionsMappingDto; @@ -46,6 +47,7 @@ import org.sonar.server.component.ComponentCreationData; import org.sonar.server.component.ComponentCreationParameters; import org.sonar.server.component.ComponentUpdater; import org.sonar.server.component.NewComponent; +import org.sonar.server.management.ManagedProjectService; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionUpdater; import org.sonar.server.permission.UserPermissionChange; @@ -66,6 +68,7 @@ public class GithubProjectCreator implements DevOpsProjectCreator { private final ComponentUpdater componentUpdater; private final PermissionUpdater permissionUpdater; private final PermissionService permissionService; + private final ManagedProjectService managedProjectService; private final GithubProjectCreationParameters githubProjectCreationParameters; private final DevOpsProjectDescriptor devOpsProjectDescriptor; private final UserSession userSession; @@ -77,7 +80,7 @@ public class GithubProjectCreator implements DevOpsProjectCreator { public GithubProjectCreator(DbClient dbClient, GithubApplicationClient githubApplicationClient, GithubPermissionConverter githubPermissionConverter, ProjectKeyGenerator projectKeyGenerator, ComponentUpdater componentUpdater, PermissionUpdater permissionUpdater, PermissionService permissionService, - GithubProjectCreationParameters githubProjectCreationParameters) { + ManagedProjectService managedProjectService, GithubProjectCreationParameters githubProjectCreationParameters) { this.dbClient = dbClient; this.githubApplicationClient = githubApplicationClient; @@ -86,6 +89,7 @@ public class GithubProjectCreator implements DevOpsProjectCreator { this.componentUpdater = componentUpdater; this.permissionUpdater = permissionUpdater; this.permissionService = permissionService; + this.managedProjectService = managedProjectService; this.githubProjectCreationParameters = githubProjectCreationParameters; userSession = githubProjectCreationParameters.userSession(); almSettingDto = githubProjectCreationParameters.almSettingDto(); @@ -167,6 +171,9 @@ public class GithubProjectCreator implements DevOpsProjectCreator { ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow(); createProjectAlmSettingDto(dbSession, repository, projectDto, almSettingDto); addScanPermissionToCurrentUser(dbSession, projectDto); + + BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow(); + syncProjectPermissionsWithGithub(projectDto, mainBranchDto); return componentCreationData; } @@ -176,6 +183,11 @@ public class GithubProjectCreator implements DevOpsProjectCreator { permissionUpdater.apply(dbSession, Set.of(scanPermission)); } + private void syncProjectPermissionsWithGithub(ProjectDto projectDto, BranchDto mainBranchDto) { + String userUuid = requireNonNull(userSession.getUuid()); + managedProjectService.queuePermissionSyncTask(userUuid, mainBranchDto.getUuid(), projectDto.getUuid()); + } + private ComponentCreationData createProject(DbSession dbSession, @Nullable String projectKey, GithubApplicationClient.Repository repository, CreationMethod creationMethod) { NewComponent projectComponent = newComponentBuilder() .setKey(Optional.ofNullable(projectKey).orElse(getUniqueProjectKey(repository))) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java index 5a4245117f4..e6387b65e9c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java @@ -37,6 +37,7 @@ import org.sonar.db.alm.setting.ALM; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.server.almintegration.ws.ProjectKeyGenerator; import org.sonar.server.component.ComponentUpdater; +import org.sonar.server.management.ManagedProjectService; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionUpdater; import org.sonar.server.permission.UserPermissionChange; @@ -63,10 +64,12 @@ public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory private final PermissionUpdater permissionUpdater; private final PermissionService permissionService; + private final ManagedProjectService managedProjectService; + public GithubProjectCreatorFactory(DbClient dbClient, GithubGlobalSettingsValidator githubGlobalSettingsValidator, GithubApplicationClient githubApplicationClient, ProjectDefaultVisibility projectDefaultVisibility, ProjectKeyGenerator projectKeyGenerator, UserSession userSession, ComponentUpdater componentUpdater, GitHubSettings gitHubSettings, GithubPermissionConverter githubPermissionConverter, - PermissionUpdater permissionUpdater, PermissionService permissionService) { + PermissionUpdater permissionUpdater, PermissionService permissionService, ManagedProjectService managedProjectService) { this.dbClient = dbClient; this.githubGlobalSettingsValidator = githubGlobalSettingsValidator; this.githubApplicationClient = githubApplicationClient; @@ -78,6 +81,7 @@ public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory this.githubPermissionConverter = githubPermissionConverter; this.permissionUpdater = permissionUpdater; this.permissionService = permissionService; + this.managedProjectService = managedProjectService; } @Override @@ -121,18 +125,17 @@ public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory almSettingDto, projectDefaultVisibility.get(dbSession).isPrivate(), gitHubSettings.isProvisioningEnabled(), userSession, appInstallationToken, authAppInstallationToken.orElse(null)); return new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, permissionUpdater, permissionService, - githubProjectCreationParameters); + managedProjectService, githubProjectCreationParameters); } - public Optional getDevOpsProjectCreator(DbSession dbSession, AlmSettingDto almSettingDto, AccessToken accessToken, + public DevOpsProjectCreator getDevOpsProjectCreator(DbSession dbSession, AlmSettingDto almSettingDto, AccessToken accessToken, DevOpsProjectDescriptor devOpsProjectDescriptor) { Optional authAppInstallationToken = getAuthAppInstallationTokenIfNecessary(devOpsProjectDescriptor); GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor, almSettingDto, projectDefaultVisibility.get(dbSession).isPrivate(), gitHubSettings.isProvisioningEnabled(), userSession, accessToken, authAppInstallationToken.orElse(null)); - return Optional.of( - new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, permissionUpdater, permissionService, - githubProjectCreationParameters) + return new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, permissionUpdater, permissionService, + managedProjectService, githubProjectCreationParameters ); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java index f5fc66ed371..498ece5fbf5 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java @@ -39,6 +39,7 @@ import org.sonar.db.alm.setting.ALM; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.server.almintegration.ws.ProjectKeyGenerator; import org.sonar.server.component.ComponentUpdater; +import org.sonar.server.management.ManagedProjectService; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionUpdater; import org.sonar.server.permission.UserPermissionChange; @@ -101,6 +102,8 @@ public class GithubProjectCreatorFactoryTest { private PermissionService permissionService; @Mock private PermissionUpdater permissionUpdater; + @Mock + private ManagedProjectService managedProjectService; @InjectMocks private GithubProjectCreatorFactory githubProjectCreatorFactory; @@ -200,8 +203,7 @@ public class GithubProjectCreatorFactoryTest { mockSuccessfulGithubInteraction(); - DevOpsProjectCreator devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, mockAlmSettingDto, appInstallationToken, GITHUB_PROJECT_DESCRIPTOR) - .orElseThrow(); + DevOpsProjectCreator devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, mockAlmSettingDto, appInstallationToken, GITHUB_PROJECT_DESCRIPTOR); GithubProjectCreator expectedGithubProjectCreator = getExpectedGithubProjectCreator(mockAlmSettingDto, false, false); assertThat(devOpsProjectCreator).usingRecursiveComparison().isEqualTo(expectedGithubProjectCreator); @@ -218,7 +220,7 @@ public class GithubProjectCreatorFactoryTest { GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor, almSettingDto, projectsArePrivateByDefault, isInstanceManaged, userSession, appInstallationToken, authAppInstallToken); return new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, permissionUpdater, permissionService, - githubProjectCreationParameters); + managedProjectService, githubProjectCreationParameters); } private AlmSettingDto mockAlmSettingDto(boolean repoAccess) { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorTest.java index a39261b2945..85bb57adbce 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorTest.java @@ -45,6 +45,7 @@ import org.sonar.db.alm.setting.ALM; import org.sonar.db.alm.setting.AlmSettingDto; import org.sonar.db.alm.setting.ProjectAlmSettingDao; import org.sonar.db.alm.setting.ProjectAlmSettingDto; +import org.sonar.db.component.BranchDto; import org.sonar.db.project.CreationMethod; import org.sonar.db.project.ProjectDto; import org.sonar.db.provisioning.GithubPermissionsMappingDto; @@ -54,6 +55,7 @@ import org.sonar.server.component.ComponentCreationData; import org.sonar.server.component.ComponentCreationParameters; import org.sonar.server.component.ComponentUpdater; import org.sonar.server.component.NewComponent; +import org.sonar.server.management.ManagedProjectService; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionServiceImpl; import org.sonar.server.permission.PermissionUpdater; @@ -83,6 +85,8 @@ public class GithubProjectCreatorTest { private static final String ALM_SETTING_KEY = "github_config_1"; private static final String USER_LOGIN = "userLogin"; private static final String USER_UUID = "userUuid"; + private static final String BRANCH_UUID = "branchUuid1"; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private DbClient dbClient; @Mock @@ -103,10 +107,13 @@ public class GithubProjectCreatorTest { private UserSession userSession; @Mock private AlmSettingDto almSettingDto; - private PermissionService permissionService = new PermissionServiceImpl(mock()); + private final PermissionService permissionService = new PermissionServiceImpl(mock()); @Mock private PermissionUpdater permissionUpdater; + @Mock + private ManagedProjectService managedProjectService; + private GithubProjectCreator githubProjectCreator; @Captor @@ -129,7 +136,7 @@ public class GithubProjectCreatorTest { when(githubProjectCreationParameters.almSettingDto()).thenReturn(almSettingDto); githubProjectCreator = new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, - permissionUpdater, permissionService, githubProjectCreationParameters); + permissionUpdater, permissionService, managedProjectService, githubProjectCreationParameters); } @Test @@ -331,16 +338,27 @@ public class GithubProjectCreatorTest { assertThat(componentCreationParameters.isManaged()).isTrue(); assertThat(componentCreationParameters.newComponent().isPrivate()).isTrue(); + verifyScanPermissionWasAddedToUser(actualComponentCreationData); + verifyProjectSyncTaskWasCreated(actualComponentCreationData); + + verify(projectAlmSettingDao).insertOrUpdate(any(), projectAlmSettingDtoCaptor.capture(), eq(ALM_SETTING_KEY), eq(REPOSITORY_NAME), eq(projectKey)); + ProjectAlmSettingDto projectAlmSettingDto = projectAlmSettingDtoCaptor.getValue(); + assertAlmSettingsDtoContainsCorrectInformation(almSettingDto, requireNonNull(componentCreationData.projectDto()), projectAlmSettingDto); + } + + private void verifyProjectSyncTaskWasCreated(ComponentCreationData componentCreationData) { + String projectUuid = requireNonNull( componentCreationData.projectDto()).getUuid(); + String mainBranchUuid = requireNonNull(componentCreationData.mainBranchDto()).getUuid(); + verify(managedProjectService).queuePermissionSyncTask(USER_UUID, mainBranchUuid, projectUuid); + } + + private void verifyScanPermissionWasAddedToUser(ComponentCreationData actualComponentCreationData) { verify(permissionUpdater).apply(any(), permissionChangesCaptor.capture()); UserPermissionChange permissionChange = permissionChangesCaptor.getValue().iterator().next(); assertThat(permissionChange.getUserId().getUuid()).isEqualTo(userSession.getUuid()); assertThat(permissionChange.getUserId().getLogin()).isEqualTo(userSession.getLogin()); assertThat(permissionChange.getPermission()).isEqualTo(UserRole.SCAN); assertThat(permissionChange.getProjectUuid()).isEqualTo(actualComponentCreationData.projectDto().getUuid()); - - verify(projectAlmSettingDao).insertOrUpdate(any(), projectAlmSettingDtoCaptor.capture(), eq(ALM_SETTING_KEY), eq(REPOSITORY_NAME), eq(projectKey)); - ProjectAlmSettingDto projectAlmSettingDto = projectAlmSettingDtoCaptor.getValue(); - assertAlmSettingsDtoContainsCorrectInformation(almSettingDto, requireNonNull(componentCreationData.projectDto()), projectAlmSettingDto); } private void mockGitHubRepository() { @@ -357,6 +375,9 @@ public class GithubProjectCreatorTest { ComponentCreationData componentCreationData = mock(); ProjectDto projectDto = mockProjectDto(projectKey); when(componentCreationData.projectDto()).thenReturn(projectDto); + BranchDto branchDto = mock(); + when(branchDto.getUuid()).thenReturn(BRANCH_UUID); + when(componentCreationData.mainBranchDto()).thenReturn(branchDto); when(componentUpdater.createWithoutCommit(any(), componentCreationParametersCaptor.capture())).thenReturn(componentCreationData); return componentCreationData; } -- 2.39.5