Skip to content

Instantly share code, notes, and snippets.

@josephschmitt
Created November 7, 2019 15:22
Show Gist options
  • Select an option

  • Save josephschmitt/9fd2e6e987e2d72ed18e0c2f02a0a70e to your computer and use it in GitHub Desktop.

Select an option

Save josephschmitt/9fd2e6e987e2d72ed18e0c2f02a0a70e to your computer and use it in GitHub Desktop.
import {ChecksUpdateResponse, ChecksUpdateParamsOutput, ChecksCreateResponse, Response, ReposCompareCommitsResponse} from '@octokit/rest';
import {WebhookPayloadCheckRun, WebhookPayloadCheckSuite, WebhookPayloadPullRequest} from '@octokit/webhooks';
import {GitHubAPI} from 'probot/lib/github';
import {Context} from 'probot/lib/context';
import {Logger} from 'probot/lib';
export default class CheckRun {
public context: Context;
public github: GitHubAPI;
public payload: WebhookPayloadCheckSuite | WebhookPayloadCheckRun | WebhookPayloadPullRequest;
public pullRequest: PullRequest | undefined;
public logger: Logger;
public action: string;
public name: string;
public id: number | undefined;
constructor(name: string, event: Context) {
this.name = name;
this.context = event;
this.github = event.github;
this.payload = event.payload;
this.action = event.payload.action;
this.logger = event.log;
}
get options() {
const payloadData = (this.payload as WebhookPayloadCheckSuite).check_suite || (this.payload as WebhookPayloadCheckRun).check_run;
const pr = (this.payload as WebhookPayloadPullRequest).pull_request;
return {
owner: this.payload.repository.owner.login,
repo: this.payload.repository.name,
head_sha: payloadData && payloadData.head_sha || pr && pr.head.sha,
name: this.name,
};
}
async start(): Promise<Response<ChecksCreateResponse>|false> {
try {
const resp: Response<ChecksCreateResponse> = await this.github.checks.create(Object.assign({
status: 'queued' as 'queued',
}, this.options));
this.id = resp.data.id;
return resp;
} catch (e) {
this.logger.warn(e);
return false;
}
}
async update(
status: 'queued' | 'in_progress' | 'completed' = 'in_progress',
conclusion?: 'success' | 'failure' | 'neutral' | 'cancelled' | 'timed_out'
| 'action_required',
output?: ChecksUpdateParamsOutput,
details_url?: string
): Promise<Response<ChecksUpdateResponse>> {
return this.github.checks.update({
...this.options,
check_run_id: this.id as number,
status,
conclusion,
...status === 'completed' && {completed_at: new Date().toISOString()},
...output && {output},
details_url,
});
}
async finish(success: boolean, output?: ChecksUpdateParamsOutput):
Promise<Response<ChecksUpdateResponse>> {
return this.github.checks.update({
...this.options,
check_run_id: this.id as number,
conclusion: success ? 'success' as 'success' : 'failure' as 'failure',
completed_at: new Date().toISOString(),
...success ? {} : {output},
});
}
async neutral(): Promise<Response<ChecksUpdateResponse>> {
return this.github.checks.update({
...this.options,
check_run_id: this.id as number,
conclusion: 'neutral',
completed_at: new Date().toISOString(),
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment