Skip to content

Commit

Permalink
Add function to update github commit status
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Feb 2, 2017
1 parent 8b8b63a commit 1de495c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion tools/gulp/tasks/screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {readdirSync, statSync, existsSync, mkdirSync} from 'fs';
import {openScreenshotsCloudStorage, openFirebaseScreenshotsDatabase} from '../task_helpers';
import * as path from 'path';
import * as admin from 'firebase-admin';
const request = require('request');
const imageDiff = require('image-diff');

const SCREENSHOT_DIR = './screenshots';
Expand All @@ -17,6 +18,7 @@ task('screenshots', () => {
return getScreenFilenames(database)
.then((filenames: string[]) => downloadAllGolds(filenames, database, prNumber))
.then((results: boolean) => updateResult(database, prNumber, results))
.then((result: boolean) => updateGithubStatus(result, prNumber))
.then(() => setScreenFilenames(database, prNumber))
.then(() => uploadScreenshots(prNumber, 'diff'))
.then(() => uploadScreenshots(prNumber, 'test'))
Expand All @@ -32,7 +34,7 @@ function updateFileResult(database: admin.database.Database, prNumber: string,

function updateResult(database: admin.database.Database, prNumber: string,
result: boolean): admin.Promise<void> {
return database.ref(FIREBASE_REPORT).child(`${prNumber}/result`).set(result);
return database.ref(FIREBASE_REPORT).child(`${prNumber}/result`).set(result).then(() => result);
}

function updateTravis(database: admin.database.Database,
Expand Down Expand Up @@ -150,3 +152,39 @@ function diffScreenshot(filename: string, database: admin.database.Database,
return updateFileResult(database, reportKey, filenameKey, false).then(() => false);
}
}

function decode(value: string): string {
return value.split('').reverse().join('');
}

function updateGithubStatus(result: boolean, prNumber: string) {
let state = result ? 'success' : 'failure';
let sha = process.env['TRAVIS_PULL_REQUEST_SHA'];
let token = decode(process.env['MATERIAL2_GITHUB_STATUS_TOKEN']);

let data = JSON.stringify({
"state": state,
"target_url": `http://material2-screenshots.firebaseapp.com/${prNumber}`,
"context": "screenshot-diff",
"description": `Screenshot test ${state}`
});

let headers = {
'Authorization': `token ${token}`,
'User-Agent': 'ScreenshotDiff/1.0.0',
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
};

return new admin.Promise((resolve, reject) => {
request({
url: `https://api.github.com/repos/angular/material2/statuses/${sha}`,
method: 'POST',
form: data,
headers: headers
}, function (error: any, response: any, body: any){
resolve(response.statusCode);
console.log(response.statusCode);
});
});
}
2 changes: 1 addition & 1 deletion tools/gulp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "es2015"],
"lib": ["es6", "es2015", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
Expand Down

0 comments on commit 1de495c

Please sign in to comment.