-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapps.js
183 lines (148 loc) · 4.56 KB
/
apps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const cliSelect = require('cli-select');
const chalk = require('chalk');
const openInBrowser = require('open');
const ora = require('ora');
const figures = require('figures');
const moment = require('moment');
const _ = require('lodash');
const currentBranchName = require('current-git-branch');
// Services
const SlackService = require('../services/slack.js');
const GithubService = require('../services/github.js');
const HerokuService = require('../services/heroku.js');
const NAME = 'apps';
/*
* [HELP] review-app help message
*/
const helpText =
`
${chalk.bold('NAME')}
skello-apps Manage active review apps
${chalk.bold('SYNOPSIS')}
${chalk.underline('skello')} ${chalk.underline(NAME)} ${chalk.underline('list')}
${chalk.underline('skello')} ${chalk.underline(NAME)} ${chalk.underline('push')}
${chalk.underline('skello')} ${chalk.underline(NAME)} ${chalk.underline('open')}
`;
/*
* [HANDLER]
*/
function handle(args) {
const [_, subCommand, branchName, ...tail] = args;
if (args.includes('--help')) {
console.log(helpText);
return;
}
switch(subCommand) {
case 'list':
list();
break;
case 'push':
push();
break;
case 'open':
open(branchName);
break;
default:
console.log(helpText);
}
}
/*
* [COMMAND] skello review-app list: list all active review app and permit to open them in browser
*/
async function list() {
const values = await getAppInfos();
console.log(`${chalk.red.bold('?')} Please choose an app to open:`);
cliSelect({
values: values,
valueRenderer: (value, selected) => {
if (selected) {
return `${chalk.blue.bold.underline(value.text)}`;
}
return `${value.text}`;
},
selected: chalk.blue(figures.circleFilled),
unselected: figures.circle,
}).then(response => {
console.log(`Opening ${response.value.pullRequest.head.ref} in browser...`);
openInBrowser(response.value.url);
})
.catch(error => null)
}
/*
* [COMMAND] skello review-app push: push all active review app in Slack
*/
async function push() {
const values = await getAppInfos();
let slackMessage = 'Here is an update on Review Apps:\n\n';
sortedValues = _.sortBy(values, value => moment(value.app.updated_at).format('X')).reverse();
slackMessage += sortedValues.reduce(function(memo, value) {
memo += `<${value.url}|${value.pullRequest.title}> (updated ${moment(value.app.updated_at).fromNow()}) | *${value.pullRequest.user.login}*\n`;
return memo;
}, '');
console.log('Sending review apps to slack...');
SlackService.notify(slackMessage);
}
/*
* [COMMAND] skello apps open <branch> | [current]: open a specific branch
*/
async function open(branchName) {
if (!branchName || branchName === '') {
console.log('Please enter a valid branch name or the current keyword.');
return;
}
if (branchName === 'current') {
openAppByBranchName(currentBranchName());
} else {
openAppByBranchName(branchName);
}
}
async function openAppByBranchName(branchName) {
const spinner = ora(`Loading app infos for ${branchName}...`).start();
const reviewApps = await HerokuService.listReviewApps();
const apps = await HerokuService.listApps();
spinner.stop();
const reviewApp = reviewApps.find(ra => ra.branch === branchName);
if (reviewApp) {
const app = apps.find(a => a.id === reviewApp.app.id);
if (app) {
openInBrowser(app.web_url);
} else {
console.log('Can\'t open review app.')
}
} else {
console.log('The review app doesn\t exist.');
}
}
async function getAppInfos() {
const spinner = ora('Loading review apps...').start();
const pulls = await GithubService.listPullRequests();
const apps = await HerokuService.listApps();
const reviewApps = await HerokuService.listReviewApps();
spinner.stop();
let values = reviewApps.map((reviewApp) => {
const app = apps.find(app => app.id === reviewApp.app.id);
const pullRequest = pulls.find(pull => reviewApp.branch === pull.head.ref);
const data = {
pullRequest: pullRequest,
reviewApp: reviewApp,
app: app
};
if (pullRequest) {
const isMerged = pullRequest.merged_at !== null;
const isClosed = pullRequest.closed_at !== null;
const text = `[${pullRequest.head.ref}] - Author: ${pullRequest.user.login} | Head: ${pullRequest.base.ref}`;
if (isMerged) text += ' [Merged]';
if (isClosed) text += ' [Closed]';
data.text = text;
data.url = app.web_url;
return data;
}
return null;
});
return values;
}
module.exports = {
NAME,
helpText,
handle,
};