-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
17 lines (15 loc) · 963 Bytes
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { client } from 'octonode'; // eslint-disable-line import/extensions
import { success, failure, githubSuccessPayload, githubFailurePayload } from './libs/response-lib';
import { isAValidPullRequest, eventIsAPullRequest, updatePullRequestStatus } from './libs/github-service';
export async function githubCheck(event, context, callback) { // eslint-disable-line import/prefer-default-export
const githubClient = client(process.env.GITHUB_TOKEN);
const body = JSON.parse(event.body);
if (!eventIsAPullRequest(body)) return callback(null, success('Event is not a Pull Request'));
const payload = isAValidPullRequest(body) ? githubSuccessPayload() : githubFailurePayload();
try {
await updatePullRequestStatus(githubClient, payload, body.repository, body.pull_request);
return callback(null, success(`Process finished with state: ${payload.state}`));
} catch (e) {
return callback(null, failure('Process finished with error'));
}
}