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