Files
angular__angular/tools/dashboard/functions/github/github-status.ts
T
Paul Gschwendtner 964033ce8d build: add dashboard firebase functions (#4806)
* build: add dashboard firebase functions

* Adds a new folder for the dashboard project that for now only contains firebase functions.
* The firebase functions will be used to securely make API calls to the Github API (e.g payload status)
* Functions will be automatically deployed as same as screenshot functions.

* Address feedback
2017-05-31 16:18:34 -07:00

44 lines
1.2 KiB
TypeScript

import {config} from 'firebase-functions';
const request = require('request');
const {version, name} = require('../package.json');
/** API token for the Github repository. Required to set the github status on commits and PRs. */
const repoToken = config().repoToken;
/** Data that must be specified to set a Github PR status. */
export type GithubStatusData = {
result: boolean;
name: string;
description: string;
url: string;
};
/** Function that sets a Github commit status */
export function setGithubStatus(commitSHA: string, data: GithubStatusData) {
const state = data.result ? 'success' : 'failure';
const requestData = JSON.stringify({
state: state,
target_url: data.url,
context: data.name,
description: data.description
});
const headers = {
'Authorization': `token ${repoToken}`,
'User-Agent': `${name}/${version}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestData)
};
return new Promise((resolve) => {
request({
url: `https://api.github.com/repos/angular/material2/statuses/${commitSHA}`,
method: 'POST',
form: requestData,
headers: headers
}, (error: any, response: any) => resolve(response.statusCode));
});
}