From 39d0d57a4979ba62e8b1c5effb97033113d3daaa Mon Sep 17 00:00:00 2001 From: Mark Rekveld Date: Fri, 12 Jun 2020 09:59:42 +0200 Subject: [PATCH] SONAR-13475 Get GitHub ClientId API --- .../sonar/db/alm/setting/AlmSettingDto.java | 33 ++++++++++++ .../sonar/db/alm/setting/AlmSettingMapper.xml | 8 +++ server/sonar-db-dao/src/schema/schema-sq.ddl | 4 +- .../db/migration/version/v84/DbVersion84.java | 6 ++- .../AddClientIdAndClientSecretColumns.java | 52 +++++++++++++++++++ ...AddClientIdAndClientSecretColumnsTest.java | 51 ++++++++++++++++++ .../schema.sql | 13 +++++ .../main/protobuf/ws-alm_integrations.proto | 5 ++ 8 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumns.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest/schema.sql diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java index a4f0badbb02..6ec8f86de63 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java @@ -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; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml index 5a2a43e3f31..ee93b8c97b2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml @@ -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" @@ -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} diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index f0f66ad0739..f6ee2bfd5ab 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -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"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/DbVersion84.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/DbVersion84.java index 040d8d14711..feba4d4a25c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/DbVersion84.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/DbVersion84.java @@ -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 index 00000000000..072ea82eea7 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumns.java @@ -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 index 00000000000..f12ceede0db --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest.java @@ -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 index 00000000000..645a8ff17ae --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v84/alm/AddClientIdAndClientSecretColumnsTest/schema.sql @@ -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"); diff --git a/sonar-ws/src/main/protobuf/ws-alm_integrations.proto b/sonar-ws/src/main/protobuf/ws-alm_integrations.proto index 6a0e7667703..f3849b09662 100644 --- a/sonar-ws/src/main/protobuf/ws-alm_integrations.proto +++ b/sonar-ws/src/main/protobuf/ws-alm_integrations.proto @@ -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; +} -- 2.39.5