]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-220 add WS api/autoscan/github_repository
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 2 Dec 2018 14:53:17 +0000 (15:53 +0100)
committerSonarTech <sonartech@sonarsource.com>
Wed, 12 Dec 2018 19:21:03 +0000 (20:21 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmAppInstallMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/authentication/CredentialsExternalAuthentication.java

index 63a6846f8a465c5016448934dff475d0f7c19337..6629c36a2269242582a9ce8950770c0df1a663ae 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
@@ -62,6 +63,12 @@ public class AlmAppInstallDao implements Dao {
     return Optional.ofNullable(mapper.selectByInstallationId(alm.getId(), installationId));
   }
 
+  public Optional<AlmAppInstallDto> selectByOrganization(DbSession dbSession, ALM alm, OrganizationDto organization) {
+    AlmAppInstallMapper mapper = getMapper(dbSession);
+    return Optional.ofNullable(mapper.selectByOrganizationUuid(alm.getId(), organization.getUuid()));
+  }
+
+
   public List<AlmAppInstallDto> selectUnboundByUserExternalId(DbSession dbSession, String userExternalId) {
     return getMapper(dbSession).selectUnboundByUserExternalId(userExternalId);
   }
index 11668864c4ef3aefcb13fb131eb590f541f5d992..637626a24f9a71c5a827113f005d3a672445ee02 100644 (file)
@@ -35,6 +35,9 @@ public interface AlmAppInstallMapper {
   @CheckForNull
   AlmAppInstallDto selectByUuid(@Param("uuid") String uuid);
 
+  @CheckForNull
+  AlmAppInstallDto selectByOrganizationUuid(@Param("almId") String almId, @Param("organizationUuid") String organizationUuid);
+
   List<AlmAppInstallDto> selectUnboundByUserExternalId(@Param("userExternalId") String userExternalId);
 
   void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId,
index fbfb720b8662f11110ce5db08ca47f35d7c8d7cc..b29b7ec7268296dfc5ac669a2fcb155db6f0ada0 100644 (file)
       uuid = #{uuid, jdbcType=VARCHAR}
   </select>
 
+  <select id="selectByOrganizationUuid" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
+    select <include refid="sqlColumns"/>
+    from
+    alm_app_installs aai
+    inner join organization_alm_bindings oab on oab.alm_app_install_uuid = aai.uuid
+    where
+    oab.alm_id = #{almId, jdbcType=VARCHAR}
+    and oab.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+  </select>
+
   <select id="selectUnboundByUserExternalId" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
     select <include refid="sqlColumns" />
     from
     alm_app_installs aai
-    left outer join organization_alm_bindings bind on bind.alm_app_install_uuid =  aai.uuid
+    left outer join organization_alm_bindings bind on bind.alm_app_install_uuid = aai.uuid
     where
     aai.user_external_id = #{userExternalId, jdbcType=VARCHAR}
     and bind.uuid is null
index 7cfd08bad9233e6063d38d7628ec39784f13bb84..8b2ce910ec226e70487a9bfe0df8532e642bb0c0 100644 (file)
@@ -149,6 +149,20 @@ public class AlmAppInstallDaoTest {
     assertThat(underTest.selectUnboundByUserExternalId(dbSession, user2.getExternalId())).isEmpty();
   }
 
+  @Test
+  public void selectByOrganization() {
+    OrganizationDto organization = db.organizations().insert();
+    db.getDbClient().almAppInstallDao().insertOrUpdate(db.getSession(), ALM.GITHUB, "the-owner", false, "123456", null);
+    // could be improved, insertOrUpdate should return the DTO with its uuid
+    Optional<AlmAppInstallDto> install = db.getDbClient().almAppInstallDao().selectByOwnerId(db.getSession(), ALM.GITHUB, "the-owner");
+    db.getDbClient().organizationAlmBindingDao().insert(db.getSession(), organization, install.get(), "xxx", "xxx");
+    db.commit();
+
+    assertThat(underTest.selectByOrganization(db.getSession(), GITHUB, organization).get().getUuid()).isEqualTo(install.get().getUuid());
+    assertThat(underTest.selectByOrganization(db.getSession(), BITBUCKETCLOUD, organization)).isEmpty();
+    assertThat(underTest.selectByOrganization(db.getSession(), GITHUB, new OrganizationDto().setUuid("other-organization"))).isEmpty();
+  }
+
   @Test
   public void insert_throws_NPE_if_alm_is_null() {
     expectAlmNPE();
index d6cbd92ca359c9f85565cb4161353df867b9a970..97ba8d33aa4f0eb2a6950219f857ef5baa7872cd 100644 (file)
@@ -88,7 +88,7 @@ public class CredentialsExternalAuthentication implements Startable {
     if (realm == null) {
       return Optional.empty();
     }
-    return Optional.of(doAuthenticate(sanitize(credentials), request, method));
+    return Optional.of(doAuthenticate(fixCase(credentials), request, method));
   }
 
   private UserDto doAuthenticate(Credentials credentials, HttpServletRequest request, AuthenticationEvent.Method method) {
@@ -153,7 +153,7 @@ public class CredentialsExternalAuthentication implements Startable {
         .build());
   }
 
-  private Credentials sanitize(Credentials credentials) {
+  private Credentials fixCase(Credentials credentials) {
     if (config.getBoolean("sonar.authenticator.downcase").orElse(false)) {
       return new Credentials(credentials.getLogin().toLowerCase(Locale.ENGLISH), credentials.getPassword());
     }