Skip to content

Commit e7d079d

Browse files
author
anandkumarpatel
committed
Merge pull request #569 from CodeNow/integrate-github-deployments-api
Deployments api
2 parents 8dd4498 + 4645350 commit e7d079d

File tree

16 files changed

+1564
-1352
lines changed

16 files changed

+1564
-1352
lines changed

configs/.env

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ HELLO_RUNNABLE_GITHUB_ID=10224339
3636
SLACK_BOT_IMAGE="https://avatars0.githubusercontent.com/u/2335750?v=3&s=200"
3737
SLACK_BOT_USERNAME="runnabot"
3838
HIPCHAT_BOT_USERNAME="runnabot"
39-
ENABLE_BUILDS_ON_GIT_PUSH=false
39+
ENABLE_GITHUB_HOOKS=false
4040
ENABLE_NOTIFICATIONS_ON_GIT_PUSH=false
41-
ENABLE_NEW_BRANCH_BUILDS_ON_GIT_PUSH=false
42-
ENABLE_GITHUB_PR_COMMENTS=false
41+
ENABLE_GITHUB_PR_STATUSES=false
4342
POLL_MONGO_TIMEOUT="30 minutes"
44-
GITHUB_SCOPE="user:email,repo,repo_deployment,read:repo_hook"
43+
GITHUB_SCOPE="user:email,repo,repo_deployment,read:repo_hook, repo:status"
4544
GITHUB_HOOK_SECRET="3V3RYTHINGisAW3S0ME!"
4645
DOCKER_IMAGE_BUILDER_CACHE="/git-cache"
4746
MONITOR_INTERVAL="1 minute"
4847
RUNNABOT_GITHUB_ACCESS_TOKEN="14ed935a9413a29dbb28774d0521a4fd958571c8"
4948
RUNNABOT_GITHUB_USERNAME="runnabot"
5049
MESSENGER_NAMESPACE="runnable:api:messenger:"
5150
BODY_PARSER_SIZE_LIMIT="600kb"
52-
GRAPH_DATABASE_TYPE="cayley"
53-
51+
GRAPH_DATABASE_TYPE="cayley"

configs/.env.production

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ ROUTE53_HOSTEDZONEID="Z3IHYG8VH3VMIJ"
2929
DNS_IPADDRESS=205.251.195.203
3030
CAYLEY="http://cayley.runnable.io"
3131
DOCKER_IMAGE_BUILDER_CACHE="/git-cache"
32-
ENABLE_BUILDS_ON_GIT_PUSH=true
32+
USER_CONTENT_DOMAIN=true
3333
ENABLE_NOTIFICATIONS_ON_GIT_PUSH=true
34-
ENABLE_NEW_BRANCH_BUILDS_ON_GIT_PUSH=false
35-
ENABLE_GITHUB_PR_COMMENTS=true
3634
GITHUB_DEPLOY_KEYS_POOL_SIZE=100
3735
HEAP_APP_ID=3017229846
3836
DOCKER_IMAGE_BUILDER_LAYER_CACHE="/layer-cache"

lib/models/apis/github.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,56 @@ Github.prototype.getPullRequestHeadCommit = function (shortRepo, number, cb) {
639639
});
640640
};
641641

642+
643+
Github.prototype.createDeployment = function (shortRepo, query, cb) {
644+
debug('createDeployment', formatArgs(arguments));
645+
var split = shortRepo.split('/');
646+
query.user = split[0];
647+
query.repo = split[1];
648+
this.deployments.create(query, function (err, deployment) {
649+
if (err) {
650+
err = (err.code === 404) ?
651+
Boom.notFound('Cannot find repo or ref: ' + shortRepo, { err: err, report: false }) :
652+
Boom.create(502, 'Failed to repo or ref ' + shortRepo, { err: err });
653+
return cb(err);
654+
}
655+
cb(null, deployment);
656+
});
657+
};
658+
659+
Github.prototype.createDeploymentStatus = function (shortRepo, query, cb) {
660+
debug('createDeploymentStatus', formatArgs(arguments));
661+
var split = shortRepo.split('/');
662+
query.user = split[0];
663+
query.repo = split[1];
664+
this.deployments.createStatus(query, function (err, deployment) {
665+
if (err) {
666+
err = (err.code === 404) ?
667+
Boom.notFound('Cannot find repo, ref or deployment: ' + shortRepo,
668+
{ err: err, report: false }) :
669+
Boom.create(502, 'Failed to repo, ref or deployment ' + shortRepo, { err: err });
670+
return cb(err);
671+
}
672+
cb(null, deployment);
673+
});
674+
};
675+
676+
Github.prototype.createBuildStatus = function (shortRepo, query, cb) {
677+
debug('createBuildStatus', formatArgs(arguments));
678+
var split = shortRepo.split('/');
679+
query.user = split[0];
680+
query.repo = split[1];
681+
this.statuses.create(query, function (err, status) {
682+
if (err) {
683+
err = (err.code === 404) ?
684+
Boom.notFound('Cannot find repo or sha: ' + shortRepo, { err: err, report: false }) :
685+
Boom.create(502, 'Failed to repo or sha ' + shortRepo, { err: err });
686+
return cb(err);
687+
}
688+
cb(null, status);
689+
});
690+
};
691+
642692
// Check if user is collaborator for the repo
643693
Github.prototype.isCollaborator = function (shortRepo, username, cb) {
644694
debug('isCollaborator', formatArgs(arguments));

lib/models/apis/pullrequest.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
'use strict';
2+
var Github = require('models/apis/github');
3+
var debug = require('debug')('runnable-api:models:pullrequest');
4+
var formatArgs = require('format-args');
5+
6+
function PullRequest (githubToken) {
7+
this.github = new Github({token: githubToken});
8+
}
9+
10+
PullRequest.prototype.buildStarted = function (pullRequestInfo, targetUrl, cb) {
11+
debug('buildStarted', formatArgs(arguments));
12+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
13+
return cb(null);
14+
}
15+
var payload = {
16+
state: 'pending',
17+
description: 'PR-' + pullRequestInfo.number + ' is building on Runnable.',
18+
// we use url to differentiate between several runnable builds
19+
context: targetUrl,
20+
target_url: targetUrl,
21+
sha: pullRequestInfo.commit
22+
};
23+
this.github.createBuildStatus(pullRequestInfo.repo, payload, cb);
24+
};
25+
26+
PullRequest.prototype.buildSucceeded = function (pullRequestInfo, targetUrl, cb) {
27+
debug('buildSucceeded', formatArgs(arguments));
28+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
29+
return cb(null);
30+
}
31+
var payload = {
32+
state: 'success',
33+
description: 'PR-' + pullRequestInfo.number + ' is ready to run on Runnable.',
34+
// we use url to differentiate between several runnable builds
35+
context: targetUrl,
36+
target_url: targetUrl,
37+
sha: pullRequestInfo.commit
38+
};
39+
this.github.createBuildStatus(pullRequestInfo.repo, payload, cb);
40+
};
41+
42+
PullRequest.prototype.buildErrored = function (pullRequestInfo, targetUrl, cb) {
43+
debug('buildErrored', formatArgs(arguments));
44+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
45+
return cb(null);
46+
}
47+
var payload = {
48+
state: 'error',
49+
description: 'PR-' + pullRequestInfo.number + ' has failed to build on Runnable.',
50+
// we use url to differentiate between several runnable builds
51+
context: targetUrl,
52+
target_url: targetUrl,
53+
sha: pullRequestInfo.commit
54+
};
55+
this.github.createBuildStatus(pullRequestInfo.repo, payload, cb);
56+
};
57+
58+
PullRequest.prototype.serverSelectionStatus = function (pullRequestInfo, targetUrl, cb) {
59+
debug('buildStarted', formatArgs(arguments));
60+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
61+
return cb(null);
62+
}
63+
var payload = {
64+
state: 'pending',
65+
description: 'Select a server to build PR-' + pullRequestInfo.number,
66+
// we use url to differentiate between several runnable builds
67+
context: targetUrl,
68+
target_url: targetUrl,
69+
sha: pullRequestInfo.commit
70+
};
71+
this.github.createBuildStatus(pullRequestInfo.repo, payload, cb);
72+
};
73+
74+
75+
PullRequest.prototype.createDeployment = function (pullRequestInfo, serverName, payload, cb) {
76+
debug('createDeployment', formatArgs(arguments));
77+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
78+
return cb(null);
79+
}
80+
var description = 'Deploying PR-' + pullRequestInfo.number + ' to ' +
81+
serverName + ' on Runnable.';
82+
var query = {
83+
auto_merge: false,
84+
environment: 'runnable',
85+
description: description,
86+
ref: pullRequestInfo.commit,
87+
payload: JSON.stringify(payload || {}),
88+
required_contexts: [] // we skip check on all `contexts` since we still can deploy
89+
};
90+
this.github.createDeployment(pullRequestInfo.repo, query, cb);
91+
};
92+
93+
94+
PullRequest.prototype.deploymentStarted =
95+
function (pullRequestInfo, deploymentId, serverName, targetUrl, cb) {
96+
debug('deploymentStarted', formatArgs(arguments));
97+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
98+
return cb(null);
99+
}
100+
var description = 'Deploying PR-' + pullRequestInfo.number + ' to ' +
101+
serverName + ' on Runnable.';
102+
var payload = {
103+
id: deploymentId,
104+
state: 'pending',
105+
target_url: targetUrl,
106+
description: description
107+
};
108+
this.github.createDeploymentStatus(pullRequestInfo.repo, payload, cb);
109+
};
110+
111+
PullRequest.prototype.deploymentSucceeded =
112+
function (pullRequestInfo, deploymentId, serverName, targetUrl, cb) {
113+
debug('deploymentSucceeded', formatArgs(arguments));
114+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
115+
return cb(null);
116+
}
117+
var description = 'Deployed PR-' + pullRequestInfo.number +
118+
' to ' + serverName + ' on Runnable.';
119+
var payload = {
120+
id: deploymentId,
121+
state: 'success',
122+
target_url: targetUrl,
123+
description: description
124+
};
125+
this.github.createDeploymentStatus(pullRequestInfo.repo, payload, cb);
126+
};
127+
128+
PullRequest.prototype.deploymentErrored =
129+
function (pullRequestInfo, deploymentId, serverName, targetUrl, cb) {
130+
debug('deploymentErrored', formatArgs(arguments));
131+
if (process.env.ENABLE_GITHUB_PR_STATUSES !== 'true') {
132+
return cb(null);
133+
}
134+
var description = 'Failed to deploy PR-' + pullRequestInfo.number +
135+
' to ' + serverName + ' on Runnable.';
136+
var payload = {
137+
id: deploymentId,
138+
state: 'error',
139+
target_url: targetUrl,
140+
description: description
141+
};
142+
this.github.createDeploymentStatus(pullRequestInfo.repo, payload, cb);
143+
};
144+
145+
module.exports = PullRequest;

0 commit comments

Comments
 (0)