forked from getgamba/deploy_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (72 loc) · 2.77 KB
/
index.js
File metadata and controls
85 lines (72 loc) · 2.77 KB
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
const request = require('request-promise')
const querystring = require('querystring')
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();
const SLACK_TOKEN = process.env['slackToken']
const GITHUB_TOKEN = process.env['githubToken']
const GITHUB_HOST = "api.github.com"
const GITHUB_PATH = "/repos/getgamba/gamba"
const GITHUB_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Content-Type': 'application/json',
'Authorization': `token ${GITHUB_TOKEN}`
}
const GithubAPI = {
create_pull_req: function(branch, target) {
const payload = {
'title': `deployment ${branch} to ${target}`,
'head': branch,
'base': `deployment/${target}`
}
return request({
uri: `https://${GITHUB_HOST}${GITHUB_PATH}/pulls`,
headers: GITHUB_HEADERS,
method: 'POST',
json: true,
body: payload
})
},
merge_pull_req: function(id) {
return request({
uri: `https://${GITHUB_HOST}${GITHUB_PATH}/pulls/${id}/merge`,
headers: GITHUB_HEADERS,
method: 'PUT',
json: true,
body: null
})
}
}
exports.deploy_handler = function(event, context, callback) {
const branch = event['branch']
const target = event['target']
GithubAPI.create_pull_req(branch, target)
.then((res)=> GithubAPI.merge_pull_req(res.number))
.catch(()=> undefined)
callback(null)
}
exports.slack_handler = function(event, context, callback) {
if (!event['body']) return null
const params = querystring.parse(event['body'])
if (params.token != SLACK_TOKEN)
return null
const command_text = params['text']
let match = command_text.match(/([-_.+0-9a-zA-Z]*) *to +(production|staging|app|android|ios|codepush|web)/)
if (match) {
let branch = match[1] || 'master'
let target = match[2]
let params = {
FunctionName: 'arn:aws:lambda:ap-northeast-1:374317207117:function:gamba_deploy_runner',
InvocationType: "Event",
Payload: JSON.stringify({ branch, target })
}
lambda.invoke(params, (err, data)=> console.log(err, data))
callback(null, {
response_type: 'in_channel',
text: `これから${branch}を${target}にデプロイしまーす!(๑˃̵ᴗ˂̵) \nhttps://circleci.com/gh/getgamba/gamba\nお疲れ様でしたー\(^o^)/`,
})
} else {
callback(null, {
text: `意味わかんなーい(≧∀≦)\n/deploy [<branch_name>] to <production|staging|ios|android|app|codepush|web>\nこんな感じで話しかけてねー♡ `
})
}
}