This commit introduces the ability to show previews for PRs by any author. It works as follows: - The build artifacts of all PRs are uploaded to the preview server. - Automatically verified PRs (i.e. from trusted authors or having a specific label) are deployed and publicly accessible as usual. - PRs that could not be automatically verified are stored for later use (after re-verification). - A PR can be marked as "trusted" and make its preview publicly accessible by adding the GitHub label specified in the `AIO_TRUSTED_PR_LABEL` env var of the preview server. At the moment, there is no automatic mechanism for notifying the preview server about changes to the PR's verification status. The PR's "visibility" will be checked and updated every time a new build is uploaded.
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
// Imports
|
|
import {assertNotMissingOrEmpty} from '../common/utils';
|
|
import {GithubApi} from './github-api';
|
|
|
|
// Interfaces - Types
|
|
export interface PullRequest {
|
|
number: number;
|
|
user: {login: string};
|
|
labels: {name: string}[];
|
|
}
|
|
|
|
export type PullRequestState = 'all' | 'closed' | 'open';
|
|
|
|
// Classes
|
|
export class GithubPullRequests extends GithubApi {
|
|
// Constructor
|
|
constructor(githubToken: string, protected repoSlug: string) {
|
|
super(githubToken);
|
|
assertNotMissingOrEmpty('repoSlug', repoSlug);
|
|
}
|
|
|
|
// Methods - Public
|
|
public addComment(pr: number, body: string): Promise<void> {
|
|
if (!(pr > 0)) {
|
|
throw new Error(`Invalid PR number: ${pr}`);
|
|
} else if (!body) {
|
|
throw new Error(`Invalid or empty comment body: ${body}`);
|
|
}
|
|
|
|
return this.post<void>(`/repos/${this.repoSlug}/issues/${pr}/comments`, null, {body});
|
|
}
|
|
|
|
public fetch(pr: number): Promise<PullRequest> {
|
|
// Using the `/issues/` URL, because the `/pulls/` one does not provide labels.
|
|
return this.get<PullRequest>(`/repos/${this.repoSlug}/issues/${pr}`);
|
|
}
|
|
|
|
public fetchAll(state: PullRequestState = 'all'): Promise<PullRequest[]> {
|
|
console.log(`Fetching ${state} pull requests...`);
|
|
|
|
const pathname = `/repos/${this.repoSlug}/pulls`;
|
|
const params = {state};
|
|
|
|
return this.getPaginated<PullRequest>(pathname, params);
|
|
}
|
|
}
|