-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (75 loc) · 3.07 KB
/
index.js
File metadata and controls
87 lines (75 loc) · 3.07 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
86
87
'use strict'
const CodeshipAPI = require('codeship-node-v2')
const orgUuid = process.env.CODESHIP_ORG_UUID
const orgName = process.env.CODESHIP_ORG_NAME
const username = process.env.CODESHIP_USERNAME
const password = process.env.CODESHIP_PASSWORD
const repositoryName = process.env.CODESHIP_REPOSITORY_NAME
const branchName = process.env.CODESHIP_BRANCH_NAME
const testing = process.env.CODESHIP_TESTING === 'true'
const includeTags = process.env.CODESHIP_INCLUDE_TAGS === 'true'
const headers = { 'Content-Type': 'application/json' }
const Codeship = new CodeshipAPI({ orgUuid, orgName, username, password })
const tagRegex = /v(\d+\.\d+\.\d+)/
function restartBuild (build) {
console.log(`${build.uuid} is last build of branch ${build.branch}`)
if (testing) {
const message = 'Aborting early because of CODESHIP_TESTING flag'
return Promise.reject(new Error(message))
}
return Codeship.builds.restart(build.uuid)
.then(() => (build.uuid))
}
function restartLastBuild (repositoryName, branchName = 'master') {
console.log(`Ready to query Codeship for ${repositoryName}:${branchName}`)
return Codeship.projects.list()
.then(projects => {
console.log(`${projects.length} project(s) found`)
const project = projects.find(project => project.name === repositoryName || project.repository_name === repositoryName)
return !project ? Promise.reject(new Error(`Project ${repositoryName} could not be found`)) : project
})
.then(project => Codeship.builds.list(project.uuid).then(builds => ({ project, builds })))
.then(({ project, builds }) => {
console.log(`${builds.length} build(s) found for ${project.repository_provider}:${project.repository_name}`)
console.log(`Filtering builds to target ${branchName}`)
const buildsMap = builds.reduce((builds, build) => {
if (!includeTags && tagRegex.test(build.branch)) {
return builds
}
if (branchName === '*' || branchName === build.branch) {
builds[build.branch] = [...(builds[build.branch] || []), build]
}
return builds
}, {})
return Object.keys(buildsMap).map(key => buildsMap[key].shift())
})
.then(builds => Promise.all(builds.map(build => restartBuild(build))))
}
function apiGatewayResponse ({body, code}) {
return {
isBase64Encoded: false,
statusCode: code,
headers: headers,
body: JSON.stringify(body)
}
}
exports.handler = function (event, context, callback) {
return restartLastBuild(repositoryName, branchName)
.then(newBuilds => {
const action = newBuilds.length > 1 ? 'builds were restarted' : 'build was restarted'
console.info(`${newBuilds.length} ${action}`, JSON.stringify(newBuilds))
const code = 200
const body = newBuilds
const response = apiGatewayResponse({body, code})
return callback(null, response)
})
.catch(error => {
console.info('Error:', error)
const body = {
error: error.message
}
const code = 400
const response = apiGatewayResponse({body, code})
return callback(null, response)
})
}