@veecode-platform/plugin-kong-service-manager
Version:
140 lines (137 loc) • 4.1 kB
JavaScript
import { extractGitHubInfo } from '../../utils/helpers/extractGithubInfo.esm.js';
import { readGithubIntegrationConfigs } from '@backstage/integration';
import { Octokit } from '@octokit/rest';
import { formatHttpErrorMessage } from '../../utils/helpers/formatHttpErrorMessage.esm.js';
import { Base64 } from 'js-base64';
import { generateBranchName } from '../../utils/helpers/generateBranchName.esm.js';
import YAML from 'js-yaml';
class GithubManager {
constructor(scmAuthApi, configApi) {
this.scmAuthApi = scmAuthApi;
this.configApi = configApi;
}
async getOctokit(hostname = "github.com") {
const { token } = await this.scmAuthApi.getCredentials({
url: `https://${hostname}/`,
additionalScope: {
repoWrite: true
}
});
const configs = readGithubIntegrationConfigs(
this.configApi.getOptionalConfigArray("integrations.github") ?? []
);
const githubIntegrationConfig = configs.find((v) => v.host === hostname);
const baseUrl = githubIntegrationConfig?.apiBaseUrl;
return new Octokit({ auth: token, baseUrl });
}
async getContent(location, filePath) {
const { host, owner, repo, branch } = extractGitHubInfo(location);
const octokit = await this.getOctokit(host);
const specs = Promise.all(filePath.map(async (file) => {
const response = await octokit.repos.getContent({
owner,
repo,
path: file,
branch
});
const data = response.data;
const yamlContent = YAML.load(
Buffer.from(data.content, "base64").toString("utf-8")
);
return yamlContent;
}));
return specs;
}
async createPullRequest(filePath, location, fileContent, title, message) {
const { host, owner, repo } = extractGitHubInfo(location);
const octokit = await this.getOctokit(host);
const branchName = generateBranchName(title);
const repoData = await octokit.repos.get({
owner,
repo
}).catch((e) => {
throw new Error(formatHttpErrorMessage("Couldn't fetch repo data", e));
});
const parentRef = await octokit.git.getRef({
owner,
repo,
ref: `heads/${repoData.data.default_branch}`
}).catch((e) => {
throw new Error(
formatHttpErrorMessage("Couldn't fetch default branch data", e)
);
});
await octokit.git.createRef({
owner,
repo,
ref: `refs/heads/${branchName}`,
sha: parentRef.data.object.sha
}).catch((e) => {
throw new Error(
formatHttpErrorMessage(
`Couldn't create a new branch with name '${branchName}'`,
e
)
);
});
let fileSha = void 0;
try {
const { data } = await octokit.repos.getContent({
owner,
repo,
path: filePath,
ref: branchName
});
if (!Array.isArray(data) && data.sha) {
fileSha = data.sha;
}
} catch (e) {
if (e.status !== 404) {
throw new Error(
formatHttpErrorMessage(
`Couldn't fetch existing file ${filePath} in the repo`,
e
)
);
}
}
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path: filePath,
message: title,
content: Base64.encode(fileContent),
branch: branchName,
sha: fileSha
}).catch((e) => {
throw new Error(
formatHttpErrorMessage(
`Couldn't create a commit with ${filePath} file added`,
e
)
);
});
const pullRequestResponse = await octokit.pulls.create({
owner,
repo,
title,
head: branchName,
message,
base: repoData.data.default_branch
}).catch((e) => {
throw new Error(
formatHttpErrorMessage(
`Couldn't create a pull request for ${branchName} branch`,
e
)
);
});
return {
status: pullRequestResponse.status,
link: pullRequestResponse.data.html_url,
message: "Pull request Created!"
};
}
}
export { GithubManager };
//# sourceMappingURL=Github.esm.js.map