branch-remover
Version:
A small application for quickly removing unnecessary branches from GitHub.
142 lines • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitHubProvider = void 0;
const rest_1 = require("@octokit/rest");
const Types_1 = require("./Types");
class GitHubProvider {
constructor(auth) {
Object.getOwnPropertyNames(new Types_1.Auth(null, null, null)).forEach((x) => {
if (!auth[x]) {
throw new Error(`The "${x}" property is required. The value must not be empty.`);
}
});
this._client = new rest_1.Octokit({
auth: auth.token,
});
this._auth = auth;
this.getListBranches = this.getListBranches.bind(this);
this.removeBranch = this.removeBranch.bind(this);
this.getPullRequestStatus = this.getPullRequestStatus.bind(this);
this.getLastCommit = this.getLastCommit.bind(this);
}
get owner() {
return this._auth.owner;
}
get repo() {
return this._auth.repo;
}
get name() {
return 'GitHub';
}
async getListBranches() {
const result = new Array();
const getList = async (page) => {
const { data } = await this._client.repos.listBranches({
owner: this.owner,
repo: this.repo,
per_page: 100,
page,
});
return data.map((x) => {
return {
name: x.name,
lastCommitHash: x.commit.sha,
};
});
};
let page = 1;
let list = await getList(page);
while (list.length) {
result.push(...list);
list = await getList(++page);
}
return result;
}
async getBranch(branchName, lastCommitHash) {
const { data } = await this._client.search.issuesAndPullRequests({
q: `repo:${this.owner}/${this.repo} is:pr head:${branchName}` + (lastCommitHash ? ` hash:${lastCommitHash}` : ''),
sort: 'updated',
order: 'desc',
});
const additionalInfo = new Map();
let updatedDate = null;
let mergedDate = null;
let merged = false;
let hasUncommittedChanges = false;
let status;
let item;
if (data.items.length) {
for (const x of data.items) {
status = await this.getPullRequestStatus(x.number);
if (status.sourceBranchName === branchName) {
item = x;
updatedDate = new Date(status.updatedDate);
hasUncommittedChanges = updatedDate > status.mergedDate;
break;
}
}
}
if (item) {
updatedDate = new Date(item.updated_at);
if (item.state === 'closed') {
merged = status.merged;
mergedDate = status.mergedDate;
}
if (item.pull_request) {
additionalInfo.set('Pull Request Url', item.pull_request.html_url);
}
}
else {
const commit = await this.getLastCommit(branchName);
updatedDate = new Date(commit.date);
}
return {
name: branchName,
merged,
mergedDate,
updatedDate,
hasUncommittedChanges,
additionalInfo,
url: `https://github.com/${this._auth.owner}/${this._auth.repo}/tree/${branchName}`,
};
}
async removeBranch(branchName) {
await this._client.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: `heads/${branchName}`,
});
}
async getPullRequestStatus(pullRequestNumber) {
const { data } = await this._client.pulls.get({
owner: this.owner,
repo: this.repo,
pull_number: pullRequestNumber,
});
return {
sourceBranchName: data.head.ref,
targetBranchName: data.base.ref,
merged: data.merged,
mergeable: data.mergeable,
mergeableState: data.mergeable_state,
mergedDate: data.merged_at && new Date(data.merged_at),
mergeCommitHash: data.merge_commit_sha,
closedDate: data.closed_at && new Date(data.closed_at),
updatedDate: data.updated_at && new Date(data.updated_at),
createdDate: new Date(data.created_at),
};
}
async getLastCommit(branchName) {
const { data } = await this._client.repos.getCommit({
owner: this.owner,
repo: this.repo,
ref: branchName,
});
return {
hash: data.sha,
date: new Date(data.commit.committer.date),
};
}
}
exports.GitHubProvider = GitHubProvider;
//# sourceMappingURL=GitHubProvider.js.map