@roadiehq/backstage-plugin-github-pull-requests
Version:
111 lines (108 loc) • 3.02 kB
JavaScript
import { readGithubIntegrationConfigs } from '@backstage/integration';
import { Octokit } from '@octokit/rest';
import { DateTime } from 'luxon';
class GithubPullRequestsClient {
configApi;
scmAuthApi;
constructor(options) {
this.configApi = options.configApi;
this.scmAuthApi = options.scmAuthApi;
}
async getOctokit(hostname = "github.com") {
const { token } = await this.scmAuthApi.getCredentials({
url: `https://${hostname}/`,
additionalScope: {
customScopes: {
github: ["repo"]
}
}
});
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 listPullRequests({
search = "",
owner,
repo,
pageSize = 5,
page,
hostname
}) {
const octokit = await this.getOctokit(hostname);
const pullRequestResponse = await octokit.search.issuesAndPullRequests({
q: `${search} in:title type:pr repo:${owner}/${repo}`,
per_page: pageSize,
page
});
return {
pullRequestsData: pullRequestResponse.data
};
}
async getRepositoryData({
hostname,
url
}) {
const octokit = await this.getOctokit(hostname);
const response = await octokit.request({ url });
return {
htmlUrl: response.data.html_url,
fullName: response.data.full_name,
additions: response.data.additions,
deletions: response.data.deletions,
changedFiles: response.data.changed_files
};
}
async getCommitDetailsData({
hostname,
owner,
repo,
number
}) {
const octokit = await this.getOctokit(hostname);
const { data: commits } = await octokit.pulls.listCommits({
owner,
repo,
pull_number: number
});
const firstCommit = commits[0];
return {
firstCommitDate: new Date(firstCommit.commit.author.date)
};
}
async searchPullRequest({
query,
hostname
}) {
const octokit = await this.getOctokit(hostname);
const pullRequestResponse = await octokit.search.issuesAndPullRequests({
q: query,
per_page: 100,
page: 1
});
return pullRequestResponse.data.items.map((pr) => ({
id: pr.id,
state: pr.state,
draft: pr.draft ?? false,
merged: pr.pull_request?.merged_at ?? void 0,
repositoryUrl: pr.repository_url,
pullRequest: {
htmlUrl: pr.pull_request?.html_url || void 0,
created_at: DateTime.fromISO(pr.created_at).toRelative() || void 0
},
title: pr.title,
number: pr.number,
user: {
login: pr.user?.login,
htmlUrl: pr.user?.html_url
},
comments: pr.comments,
htmlUrl: pr.html_url
}));
}
}
export { GithubPullRequestsClient };
//# sourceMappingURL=GithubPullRequestsClient.esm.js.map