Skip to content

Commit

Permalink
feat(): update commit status on github
Browse files Browse the repository at this point in the history
  • Loading branch information
Izak88 committed Aug 17, 2017
1 parent 8f5411b commit ea00cf0
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/api/db/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function create(): Promise<null> {
t.string('user_html_url');
t.string('username');
t.string('password');
t.string('access_token');
t.timestamps();
}))
.then(() => schema.createTableIfNotExists('builds', (t: knex.TableBuilder) => {
Expand Down
95 changes: 95 additions & 0 deletions src/api/github-commit-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as request from 'request';

function sendRequest(url: string, data: any, headers: any): Promise<any> {
return new Promise((resolve, reject) => {
let options = {
url: url,
method: 'POST',
headers: headers,
json: data
};

request(options, (err, response, body) => {
if (err) {
reject(err);
} else {
if (response.statusCode < 300 && response.statusCode >= 200) {
resolve(body);
} else {
reject({
statusCode: response.statusCode,
response: body
});
}
}
});
});
}

export function setGitHubStatusSuccess(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'success',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'context': 'continuous-integration/abstruse'
};

let header = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusPending(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'pending',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'context': 'continuous-integration/abstruse'
};

let header = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusError(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'error',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'context': 'continuous-integration/abstruse'
};

let header = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}

export function setGitHubStatusFailure(
gitUrl: string, abstruseUrl: string, token: string): Promise<any> {
let data = {
'state': 'failure',
'target_url': abstruseUrl,
'description': 'The Abstruse CI build succeeded',
'context': 'continuous-integration/abstruse'
};

let header = {
'Authorization': `token ${token}`,
'User-Agent': 'Abstruse'
};

return sendRequest(gitUrl, data, header);
}
30 changes: 29 additions & 1 deletion src/api/process-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { killContainer } from './docker';
import * as logger from './logger';
import { blue, yellow, green, cyan } from 'chalk';
import { getConfig, getHttpJsonResponse } from './utils';
import { setGitHubStatusError, setGitHubStatusFailure,
setGitHubStatusPending, setGitHubStatusSuccess } from './github-commit-status';

export interface BuildMessage {
type: string;
Expand Down Expand Up @@ -131,7 +133,13 @@ export function startBuild(data: any): Promise<any> {
data.build_id = build.id;
delete data.repositories_id;
delete data.pr;
insertBuildRun(data);
insertBuildRun(data)
.then(() => {
let gitUrl =
`https://api.github.com/repos/${data.head_full_name}/statuses/${data.sha}`;
let abstruseUrl = `${config.url}/build/${build.id}`;
return setGitHubStatusPending(gitUrl, abstruseUrl, repository.token);
});
const jobsCommands = generateCommands(repository.clone_url, repoDetails.config);

return jobsCommands.reduce((prev, commands, i) => {
Expand Down Expand Up @@ -319,6 +327,13 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
job_id: jobId,
data: 'jobFailed'
});
})
.then(() => getBuild(buildId))
.then(build => {
let gitUrl =
`https://api.github.com/repos/${build.head_full_name}/statuses/${build.sha}`;
let abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusFailure(gitUrl, abstruseUrl, build.repository.token);
});
}, () => {
dbJob.getLastRunId(jobId)
Expand All @@ -331,6 +346,13 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
getLastRunId(buildId)
.then(id => {
updateBuildRun({ id: id, end_time: new Date()});
})
.then(() => getBuild(buildId))
.then(build => {
let gitUrl =
`https://api.github.com/repos/${build.head_full_name}/statuses/${build.sha}`;
let abstruseUrl = `${config.url}/build/${buildId}`;
return setGitHubStatusSuccess(gitUrl, abstruseUrl, build.repository.token);
});
}
Promise.resolve();
Expand Down Expand Up @@ -384,6 +406,12 @@ export function restartBuild(buildId: number): Promise<any> {
return jobs.reduce((prev, curr) => {
return prev.then(() => queueJob(buildId, curr.id));
}, Promise.resolve());
})
.then(() => {
let gitUrl =
`https://api.github.com/repos/${build.head_full_name}/statuses/${build.sha}`;
let abstruseUrl = `${config.url}/build/${build.id}`;
return setGitHubStatusPending(gitUrl, abstruseUrl, build.repository.token);
});
});
}
Expand Down
1 change: 1 addition & 0 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as uuid from 'uuid';
import * as request from 'request';

const defaultConfig = {
url: null,
secret: 'thisIsSecret',
port: 6500,
wsport: 6501,
Expand Down
8 changes: 6 additions & 2 deletions src/api/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as express from 'express';
import * as crypto from 'crypto';
import { getConfig } from './utils';
import { getConfig, getFilePath } from './utils';
import { pingRepository, createPullRequest, synchronizePullRequest } from './db/repository';
import { startBuild } from './process-manager';
import { writeJsonFile } from './fs';

const config: any = getConfig();
export const webhooks = express.Router();
Expand Down Expand Up @@ -35,7 +36,10 @@ webhooks.post('/github', (req: express.Request, res: express.Response) => {

switch (ev) {
case 'ping':
pingRepository(payload)
let config: any = getConfig();
config.url = req.headers.host;
writeJsonFile(getFilePath('config.json'), config)
.then(() => pingRepository(payload))
.then(repo => res.status(200).json({ msg: 'ok' }))
.catch(err => res.status(400).json(err));
break;
Expand Down

0 comments on commit ea00cf0

Please sign in to comment.