|
| 1 | +import * as OctokitApi from '@octokit/rest'; |
| 2 | + |
| 3 | +// TODO: Consider using local git information for the data to avoid worrying about rate limits. */ |
| 4 | +/** Class to act as an interface to the GitHub API. */ |
| 5 | +export class GitHub { |
| 6 | + // TODO: Use an authentication token to increase rate limits. |
| 7 | + /** Octokit API instance that can be used to make Github API calls. */ |
| 8 | + private _api = new OctokitApi(); |
| 9 | + |
| 10 | + /** Owner of the repository to query. */ |
| 11 | + private _owner = 'angular'; |
| 12 | + |
| 13 | + /** Name of the repository to query. */ |
| 14 | + private _name = 'components'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Retrieves merged patch-eligible pull requests that have been merged since the date. |
| 18 | + * Results are sorted by merge date. |
| 19 | + */ |
| 20 | + async getPatchPullRequestsSince(dateSince: string): Promise<OctokitApi.PullsGetResponse[]> { |
| 21 | + const query = 'base:master is:pr -label:"target: minor" -label:"target: major" is:merged' + |
| 22 | + ` merged:>${dateSince}`; |
| 23 | + const result = await this._search(query); |
| 24 | + |
| 25 | + // Load information for each pull request. Waits for each pull request response until loading |
| 26 | + // the next pull request to avoid GitHub's abuse detection (too many calls in a short amount |
| 27 | + // of time). |
| 28 | + const pullRequests: OctokitApi.PullsGetResponse[] = []; |
| 29 | + for (let i = 0; i < result.items.length; i++) { |
| 30 | + pullRequests.push(await this.loadPullRequest(result.items[i].number)); |
| 31 | + } |
| 32 | + |
| 33 | + // Sort by merge date. |
| 34 | + pullRequests.sort((a, b) => (a.merged_at < b.merged_at) ? -1 : 1); |
| 35 | + return pullRequests; |
| 36 | + } |
| 37 | + |
| 38 | + /** Loads the information for the provided pull request number. */ |
| 39 | + async loadPullRequest(prNumber: number): Promise<OctokitApi.PullsGetResponse> { |
| 40 | + const response = await this._api.pulls.get({ |
| 41 | + owner: this._owner, |
| 42 | + repo: this._name, |
| 43 | + pull_number: prNumber, |
| 44 | + }); |
| 45 | + return response.data; |
| 46 | + } |
| 47 | + |
| 48 | + /** Gets the commit information for the given SHA. */ |
| 49 | + async getCommit(sha: string): Promise<OctokitApi.ReposGetCommitResponse> { |
| 50 | + const response = await this._api.repos.getCommit({ |
| 51 | + owner: this._owner, |
| 52 | + repo: this._name, |
| 53 | + ref: sha, |
| 54 | + }); |
| 55 | + |
| 56 | + return response.data; |
| 57 | + } |
| 58 | + |
| 59 | + /** Retrieves the list of latest commits from the branch. */ |
| 60 | + async listCommits(branch: string): Promise<OctokitApi.ReposListCommitsResponse> { |
| 61 | + const response = await this._api.repos.listCommits({ |
| 62 | + owner: this._owner, |
| 63 | + repo: this._name, |
| 64 | + sha: branch, |
| 65 | + }); |
| 66 | + |
| 67 | + return response.data; |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: Handle pagination in case there are more than 100 results. |
| 71 | + /** Gets a suggestion for the latest patch branch. */ |
| 72 | + async getPatchBranchSuggestion(): Promise<string> { |
| 73 | + const response = await this._api.repos.listBranches({owner: this._owner, repo: this._name}); |
| 74 | + |
| 75 | + // Matches branch names that have two digits separated by period and ends with an x |
| 76 | + const patchBranches = |
| 77 | + response.data.map(branch => branch.name).filter(name => !!/^\d+\.\d+\.x$/g.exec(name)); |
| 78 | + return patchBranches.pop() || ''; |
| 79 | + } |
| 80 | + |
| 81 | + // TODO: Handle pagination in case there are more than 100 results. |
| 82 | + /** Searches the repository using the provided query. */ |
| 83 | + private async _search(query: string): Promise<{items: any[]}> { |
| 84 | + const scopedQuery = `repo:${this._owner}/${this._name} ${query}`; |
| 85 | + const result = await this._api.search.issuesAndPullRequests({per_page: 100, q: scopedQuery}); |
| 86 | + return result.data; |
| 87 | + } |
| 88 | +} |
0 commit comments