Add self-hosted gitlab url to sync function

This commit is contained in:
Tuan Dang
2023-09-06 11:11:11 +01:00
parent d07b2dafc3
commit 1e20715511
+9 -5
View File
@@ -156,6 +156,7 @@ const syncSecrets = async ({
break; break;
case INTEGRATION_GITLAB: case INTEGRATION_GITLAB:
await syncSecretsGitLab({ await syncSecretsGitLab({
integrationAuth,
integration, integration,
secrets, secrets,
accessToken accessToken
@@ -1813,10 +1814,12 @@ const syncSecretsTravisCI = async ({
* @param {String} obj.accessToken - access token for GitLab integration * @param {String} obj.accessToken - access token for GitLab integration
*/ */
const syncSecretsGitLab = async ({ const syncSecretsGitLab = async ({
integrationAuth,
integration, integration,
secrets, secrets,
accessToken accessToken
}: { }: {
integrationAuth: IIntegrationAuth;
integration: IIntegration; integration: IIntegration;
secrets: Record<string, { value: string; comment?: string }>; secrets: Record<string, { value: string; comment?: string }>;
accessToken: string; accessToken: string;
@@ -1826,9 +1829,10 @@ const syncSecretsGitLab = async ({
value: string; value: string;
environment_scope: string; environment_scope: string;
} }
const gitLabApiUrl = integrationAuth.url ? `${integrationAuth.url}/api` : INTEGRATION_GITLAB_API_URL;
const getAllEnvVariables = async (integrationAppId: string, accessToken: string) => { const getAllEnvVariables = async (integrationAppId: string, accessToken: string) => {
const gitLabApiUrl = `${INTEGRATION_GITLAB_API_URL}/v4/projects/${integrationAppId}/variables`;
const headers = { const headers = {
Authorization: `Bearer ${accessToken}`, Authorization: `Bearer ${accessToken}`,
"Accept-Encoding": "application/json", "Accept-Encoding": "application/json",
@@ -1836,7 +1840,7 @@ const syncSecretsGitLab = async ({
}; };
let allEnvVariables: GitLabSecret[] = []; let allEnvVariables: GitLabSecret[] = [];
let url: string | null = `${gitLabApiUrl}?per_page=100`; let url: string | null = `${gitLabApiUrl}/v4/projects/${integrationAppId}/variables?per_page=100`;
while (url) { while (url) {
const response: any = await standardRequest.get(url, { headers }); const response: any = await standardRequest.get(url, { headers });
@@ -1864,7 +1868,7 @@ const syncSecretsGitLab = async ({
const existingSecret = getSecretsRes.find((s: any) => s.key == key); const existingSecret = getSecretsRes.find((s: any) => s.key == key);
if (!existingSecret) { if (!existingSecret) {
await standardRequest.post( await standardRequest.post(
`${INTEGRATION_GITLAB_API_URL}/v4/projects/${integration?.appId}/variables`, `${gitLabApiUrl}/v4/projects/${integration?.appId}/variables`,
{ {
key: key, key: key,
value: secrets[key].value, value: secrets[key].value,
@@ -1885,7 +1889,7 @@ const syncSecretsGitLab = async ({
// update secret // update secret
if (secrets[key].value !== existingSecret.value) { if (secrets[key].value !== existingSecret.value) {
await standardRequest.put( await standardRequest.put(
`${INTEGRATION_GITLAB_API_URL}/v4/projects/${integration?.appId}/variables/${existingSecret.key}?filter[environment_scope]=${integration.targetEnvironment}`, `${gitLabApiUrl}/v4/projects/${integration?.appId}/variables/${existingSecret.key}?filter[environment_scope]=${integration.targetEnvironment}`,
{ {
...existingSecret, ...existingSecret,
value: secrets[existingSecret.key].value value: secrets[existingSecret.key].value
@@ -1906,7 +1910,7 @@ const syncSecretsGitLab = async ({
for await (const sec of getSecretsRes) { for await (const sec of getSecretsRes) {
if (!(sec.key in secrets)) { if (!(sec.key in secrets)) {
await standardRequest.delete( await standardRequest.delete(
`${INTEGRATION_GITLAB_API_URL}/v4/projects/${integration?.appId}/variables/${sec.key}?filter[environment_scope]=${integration.targetEnvironment}`, `${gitLabApiUrl}/v4/projects/${integration?.appId}/variables/${sec.key}?filter[environment_scope]=${integration.targetEnvironment}`,
{ {
headers: { headers: {
Authorization: `Bearer ${accessToken}` Authorization: `Bearer ${accessToken}`