@veecode-platform/plugin-kong-service-manager
Version:
129 lines (126 loc) • 4.94 kB
JavaScript
import { readGitLabIntegrationConfigs } from '@backstage/integration';
import { formatHttpErrorMessage } from '../../utils/helpers/formatHttpErrorMessage.esm.js';
import { Base64 } from 'js-base64';
import { extractGitLabInfo } from '../../utils/helpers/extractGitlabInfo.esm.js';
import { generateBranchName } from '../../utils/helpers/generateBranchName.esm.js';
import YAML from 'js-yaml';
class GitLabManager {
constructor(scmAuthApi, configApi) {
this.scmAuthApi = scmAuthApi;
this.configApi = configApi;
}
async getToken(hostname = "gitlab.com") {
const { token } = await this.scmAuthApi.getCredentials({
url: `https://${hostname}/`,
additionalScope: {
customScopes: {
gitlab: ["api"]
}
}
});
return token;
}
async getApiBaseUrl(hostname) {
const configs = readGitLabIntegrationConfigs(
this.configApi.getOptionalConfigArray("integrations.gitlab") ?? []
);
const gitlabIntegrationConfig = configs.find((v) => v.host === hostname);
return gitlabIntegrationConfig?.apiBaseUrl || `https://${hostname}/api/v4`;
}
async fetchFromGitLab(url, token, options = {}) {
const response = await fetch(url, {
...options,
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
...options.headers
}
});
if (!response.ok) {
const errorMessage = await response.text();
const error = {
status: response.status,
message: errorMessage || `Error fetching ${url}`
};
throw new Error(formatHttpErrorMessage(`Error fetching ${url}`, error));
}
return response.json();
}
async getContent(location, filePath) {
const { host, group, repo, branch } = extractGitLabInfo(location);
const token = await this.getToken(host);
const baseUrl = await this.getApiBaseUrl(host);
const specs = await Promise.all(filePath.map(async (file) => {
const fileUrl = `${baseUrl}/projects/${encodeURIComponent(`${group}/${repo}`)}/repository/files/${encodeURIComponent(file)}?ref=${branch}`;
const fileResponse = await this.fetchFromGitLab(fileUrl, token).catch((e) => {
if (e.status === 404) {
throw new Error(formatHttpErrorMessage(`File not found: ${filePath}`, e));
}
throw e;
});
const decodedContent = Base64.decode(fileResponse.content);
return YAML.load(decodedContent);
}));
return specs;
}
async createPullRequest(filePath, location, fileContent, title, message) {
const { host, group, repo } = extractGitLabInfo(location);
const token = await this.getToken(host);
const baseUrl = await this.getApiBaseUrl(host);
const branchName = generateBranchName(title);
const projectUrl = `${baseUrl}/projects/${encodeURIComponent(`${group}/${repo}`)}`;
const project = await this.fetchFromGitLab(projectUrl, token);
const defaultBranch = project.default_branch ?? "main";
const branchUrl = `${baseUrl}/projects/${project.id}/repository/branches/${defaultBranch}`;
const parentBranch = await this.fetchFromGitLab(branchUrl, token);
const createBranchUrl = `${baseUrl}/projects/${project.id}/repository/branches`;
await this.fetchFromGitLab(createBranchUrl, token, {
method: "POST",
body: JSON.stringify({
branch: branchName,
ref: parentBranch.commit.id
})
});
const checkFileUrl = `${baseUrl}/projects/${project.id}/repository/files/${encodeURIComponent(filePath)}?ref=${defaultBranch}`;
const fileExists = await this.fetchFromGitLab(checkFileUrl, token).catch(() => null);
const createOrUpdateFileUrl = `${baseUrl}/projects/${project.id}/repository/files/${encodeURIComponent(filePath)}`;
if (fileExists) {
await this.fetchFromGitLab(createOrUpdateFileUrl, token, {
method: "PUT",
body: JSON.stringify({
branch: branchName,
content: Base64.encode(fileContent),
commit_message: title,
encoding: "base64"
})
});
} else {
await this.fetchFromGitLab(createOrUpdateFileUrl, token, {
method: "POST",
body: JSON.stringify({
branch: branchName,
content: Base64.encode(fileContent),
commit_message: title,
encoding: "base64"
})
});
}
const mergeRequestUrl = `${baseUrl}/projects/${project.id}/merge_requests`;
const pullRequestResponse = await this.fetchFromGitLab(mergeRequestUrl, token, {
method: "POST",
body: JSON.stringify({
source_branch: branchName,
target_branch: defaultBranch,
title,
description: message
})
});
return {
status: pullRequestResponse.web_url ? 201 : 404,
link: pullRequestResponse.web_url,
message: "Pull request created!"
};
}
}
export { GitLabManager };
//# sourceMappingURL=Gitlab.esm.js.map