]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13475 Get GitHub ClientId API
authorMark Rekveld <mark.rekveld@sonarsource.com>
Fri, 12 Jun 2020 07:59:42 +0000 (09:59 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 1 Jul 2020 20:05:53 +0000 (20:05 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/DbVersion84.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumns.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest/schema.sql [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-alm_integrations.proto

index a4f0badbb027df5c57c001e0f43298494136435e..6ec8f86de63e0b4539850e1251ba9be7b46b819d 100644 (file)
@@ -69,6 +69,19 @@ public class AlmSettingDto {
    */
   private String personalAccessToken;
 
+  /**
+   * Application client Id of the GitHub instance. Max size is 40.
+   * This column will only be fed when alm is GitHub.
+   * It will be null when the ALM is Azure DevOps or Bitbucket.
+   */
+  private String clientId;
+  /**
+   * Application client secret of the GitHub instance. Max size is 80.
+   * This column will only be fed when alm is GitHub.
+   * It will be null when the ALM is Azure DevOps or Bitbucket.
+   */
+  private String clientSecret;
+
   private long updatedAt;
   private long createdAt;
 
@@ -147,6 +160,26 @@ public class AlmSettingDto {
     return this;
   }
 
+  @CheckForNull
+  public String getClientId() {
+    return clientId;
+  }
+
+  public AlmSettingDto setClientId(@Nullable String clientId) {
+    this.clientId = clientId;
+    return this;
+  }
+
+  @CheckForNull
+  public String getClientSecret() {
+    return clientSecret;
+  }
+
+  public AlmSettingDto setClientSecret(@Nullable String clientSecret) {
+    this.clientSecret = clientSecret;
+    return this;
+  }
+
   long getUpdatedAt() {
     return updatedAt;
   }
index 5a2a43e3f31842a8c47ec5f94c155da34941f207..ee93b8c97b25715a977edf1f2f2cf9a497f78936 100644 (file)
@@ -11,6 +11,8 @@
     a.app_id as "appId",
     a.private_key as "privateKey",
     a.pat as "personalAccessToken",
+    a.client_id as "clientId",
+    a.client_secret as "clientSecret",
     a.created_at as "createdAt",
     a.updated_at as "updatedAt"
   </sql>
@@ -55,6 +57,8 @@
       app_id,
       private_key,
       pat,
+      client_id,
+      client_secret,
       created_at,
       updated_at
     )
@@ -66,6 +70,8 @@
       #{dto.appId, jdbcType=VARCHAR},
       #{dto.privateKey, jdbcType=VARCHAR},
       #{dto.personalAccessToken, jdbcType=VARCHAR},
+      #{dto.clientId, jdbcType=VARCHAR},
+      #{dto.clientSecret, jdbcType=VARCHAR},
       #{dto.createdAt, jdbcType=BIGINT},
       #{dto.updatedAt, jdbcType=BIGINT}
     )
@@ -79,6 +85,8 @@
       app_id = #{dto.appId, jdbcType=VARCHAR},
       private_key = #{dto.privateKey, jdbcType=VARCHAR},
       pat = #{dto.personalAccessToken, jdbcType=VARCHAR},
+      client_id = #{dto.clientId, jdbcType=VARCHAR},
+      client_secret = #{dto.clientSecret, jdbcType=VARCHAR},
       updated_at = #{dto.updatedAt, jdbcType=BIGINT}
     </set>
     <where>
index f0f66ad073915d98aba6b404b1fd3b2ab088d360..f6ee2bfd5ab481cde4ba57c00b413189aac55760 100644 (file)
@@ -70,7 +70,9 @@ CREATE TABLE "ALM_SETTINGS"(
     "PRIVATE_KEY" VARCHAR(2000),
     "PAT" VARCHAR(2000),
     "UPDATED_AT" BIGINT NOT NULL,
-    "CREATED_AT" BIGINT NOT NULL
+    "CREATED_AT" BIGINT NOT NULL,
+    "CLIENT_ID" VARCHAR(80),
+    "CLIENT_SECRET" VARCHAR(80)
 );
 ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID");
 CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE");
index 040d8d14711ca4a08aaac7959dfcc54219002985..feba4d4a25c207fa8ad8944523f1e17068c54c2f 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.server.platform.db.migration.version.v84.activerules.MakeActive
 import org.sonar.server.platform.db.migration.version.v84.activerules.MakeActiveRulesUuidColumnNotNullable;
 import org.sonar.server.platform.db.migration.version.v84.activerules.PopulateActiveRuleParametersActiveRuleUuid;
 import org.sonar.server.platform.db.migration.version.v84.activerules.PopulateActiveRulesUuid;
+import org.sonar.server.platform.db.migration.version.v84.alm.AddClientIdAndClientSecretColumns;
 import org.sonar.server.platform.db.migration.version.v84.ceactivity.AddPrimaryKeyOnUuidColumnOfCeActivityTable;
 import org.sonar.server.platform.db.migration.version.v84.ceactivity.DropIdColumnOfCeActivityTable;
 import org.sonar.server.platform.db.migration.version.v84.ceactivity.DropPrimaryKeyOnIdColumnOfCeActivityTable;
@@ -781,7 +782,10 @@ public class DbVersion84 implements DbVersion {
       .add(3804, "Populate 'need_issue_sync' of 'project_branches'", PopulateProjectBranchesNeedIssueSync.class)
       .add(3805, "Make 'need_issue_sync' of 'project_branches' not null", MakeProjectBranchesNeedIssueSyncNonNull.class)
 
-      .add(3806, "Drop local webhooks", DropLocalWebhooks.class)
+      .add(3806, "Drop local webhooks", DropLocalWebhooks.class)  
+
+      // Migration of ALM_SETTINGS table
+      .add(3807, "Add columns 'CLIENT_ID' and 'CLIENT_SECRET' to 'ALM_SETTINGS' table", AddClientIdAndClientSecretColumns.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumns.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumns.java
new file mode 100644 (file)
index 0000000..072ea82
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v84.alm;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class AddClientIdAndClientSecretColumns extends DdlChange {
+
+  public AddClientIdAndClientSecretColumns(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AddColumnsBuilder(getDialect(), "alm_settings")
+      .addColumn(newVarcharColumnDefBuilder()
+        .setColumnName("client_id")
+        .setIsNullable(true)
+        .setDefaultValue(null)
+        .setLimit(80)
+        .build())
+      .addColumn(newVarcharColumnDefBuilder()
+        .setColumnName("client_secret")
+        .setIsNullable(true)
+        .setDefaultValue(null)
+        .setLimit(80)
+        .build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest.java
new file mode 100644 (file)
index 0000000..f12ceed
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v84.alm;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+import org.sonar.server.platform.db.migration.step.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class AddClientIdAndClientSecretColumnsTest {
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(AddClientIdAndClientSecretColumnsTest.class, "schema.sql");
+
+  private MigrationStep underTest = new AddClientIdAndClientSecretColumns(db.database());
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    db.assertColumnDefinition("alm_settings", "client_id", Types.VARCHAR, 80, true);
+    db.assertColumnDefinition("alm_settings", "client_secret", Types.VARCHAR, 80, true);
+  }
+
+  @Test
+  public void migration_is_not_re_entrant() throws SQLException {
+    underTest.execute();
+
+    assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest/schema.sql
new file mode 100644 (file)
index 0000000..645a8ff
--- /dev/null
@@ -0,0 +1,13 @@
+CREATE TABLE "ALM_SETTINGS"(
+    "UUID" VARCHAR(40) NOT NULL,
+    "ALM_ID" VARCHAR(40) NOT NULL,
+    "KEE" VARCHAR(200) NOT NULL,
+    "URL" VARCHAR(2000),
+    "APP_ID" VARCHAR(80),
+    "PRIVATE_KEY" VARCHAR(2000),
+    "PAT" VARCHAR(2000),
+    "UPDATED_AT" BIGINT NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "ALM_SETTINGS" ADD CONSTRAINT "PK_ALM_SETTINGS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "UNIQ_ALM_SETTINGS" ON "ALM_SETTINGS"("KEE");
index 6a0e7667703e6079f5b1577bda93744c2cf9c1ea..f3849b096627af3a05b6ebd0dd7ea5419e6a5ff6 100644 (file)
@@ -47,3 +47,8 @@ message BBSProject {
   optional string key = 1;
   optional string name = 2;
 }
+
+// WS api/alm_integrations/get_github_client_id
+message GithubClientIdWsResponse {
+  optional string clientId = 1;
+}