Skip to content

Commit e8f8517

Browse files
committed
Address feedback
1 parent 50b1a59 commit e8f8517

File tree

6 files changed

+38
-35
lines changed

6 files changed

+38
-35
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {payloadGithubStatus} from './payload-github-status';

tools/dashboard/functions/github-status.ts renamed to tools/dashboard/functions/github/github-status.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import {config} from 'firebase-functions';
2+
13
const request = require('request');
2-
const {version, name} = require('./package.json');
4+
const {version, name} = require('../package.json');
5+
6+
/** API token for the Github repository. Required to set the github status on commits and PRs. */
7+
const repoToken = config().repoToken;
38

49
/** Data that must be specified to set a Github PR status. */
510
export type GithubStatusData = {
@@ -10,7 +15,7 @@ export type GithubStatusData = {
1015
};
1116

1217
/** Function that sets a Github commit status */
13-
export function setGithubStatus(commitSHA: string, authToken: string, data: GithubStatusData) {
18+
export function setGithubStatus(commitSHA: string, data: GithubStatusData) {
1419
const state = data.result ? 'success' : 'failure';
1520

1621
const requestData = JSON.stringify({
@@ -21,7 +26,7 @@ export function setGithubStatus(commitSHA: string, authToken: string, data: Gith
2126
});
2227

2328
const headers = {
24-
'Authorization': `token ${authToken}`,
29+
'Authorization': `token ${repoToken}`,
2530
'User-Agent': `${name}/${version}`,
2631
'Content-Type': 'application/json',
2732
'Content-Length': Buffer.byteLength(requestData)

tools/dashboard/functions/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@ require('ts-node').register({
1212
project: path.join(__dirname, 'tsconfig.json')
1313
});
1414

15-
const functionExports = require('./dashboard-functions');
16-
17-
// Re-export every firebase function from TypeScript
18-
Object.keys(functionExports).forEach(fnName => {
19-
module.exports[fnName] = functionExports[fnName];
20-
});
15+
// Export all functions from the TypeScript source.
16+
Object.assign(exports, require('./functions'));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {verify} from 'jsonwebtoken';
2+
import {config} from 'firebase-functions';
3+
4+
/** The JWT secret. This is used to validate JWT. */
5+
const jwtSecret = config().jwtSecret;
6+
7+
/** The repo slug. This is used to validate the JWT is sent from correct repo. */
8+
const repoSlug = config().repoSlug;
9+
10+
export function verifyToken(token: string): boolean {
11+
try {
12+
const tokenPayload = verify(token, jwtSecret, {issuer: 'Travis CI, GmbH'});
13+
14+
if (tokenPayload.slug !== repoSlug) {
15+
console.log(`JWT slugs are not matching. Expected ${repoSlug}`);
16+
}
17+
18+
return true;
19+
} catch (e) {
20+
return false;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import {https, config} from 'firebase-functions';
2-
import {verify} from 'jsonwebtoken';
3-
import {setGithubStatus} from './github-status';
4-
5-
/** The repo slug. This is used to validate the JWT is sent from correct repo. */
6-
const repoSlug = config().repoSlug;
7-
8-
/** API token for the Github repository. Required to set the github status on commits and PRs. */
9-
const repoToken = config().repoToken;
10-
11-
/** The JWT secret. This is used to validate JWT. */
12-
const jwtSecret = config().jwtSecret;
1+
import {https} from 'firebase-functions';
2+
import {verifyToken} from './jwt/verify-token';
3+
import {setGithubStatus} from './github/github-status';
134

145
export const payloadGithubStatus = https.onRequest(async (request, response) => {
156
const authToken = request.header('auth-token');
@@ -28,7 +19,7 @@ export const payloadGithubStatus = https.onRequest(async (request, response) =>
2819
return response.status(400).json({message: 'No valid payload diff has been specified.'});
2920
}
3021

31-
await setGithubStatus(commitSha, repoToken, {
22+
await setGithubStatus(commitSha, {
3223
result: true,
3324
name: 'Library Payload',
3425
url: `https://travis-ci.org/angular/material2/jobs/${process.env['TRAVIS_JOB_ID']}`,
@@ -37,15 +28,3 @@ export const payloadGithubStatus = https.onRequest(async (request, response) =>
3728

3829
response.json({message: 'Payload Github status successfully set.'});
3930
});
40-
41-
function verifyToken(token: string): boolean {
42-
try {
43-
const tokenPayload = verify(token, jwtSecret, {issuer: 'Travis CI, GmbH'});
44-
if (tokenPayload.slug !== repoSlug) {
45-
console.log(`JWT slugs are not matching. Expected ${repoSlug}`);
46-
}
47-
return true;
48-
} catch (e) {
49-
return false;
50-
}
51-
}

tools/dashboard/functions/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"outDir": "../../../dist/dashboard-functions/"
1212
},
1313
"files": [
14-
"./dashboard-functions.ts"
14+
"./functions.ts"
1515
]
1616
}

0 commit comments

Comments
 (0)