-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (146 loc) · 4.86 KB
/
index.js
File metadata and controls
171 lines (146 loc) · 4.86 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
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
'use strict'
const fetch = require('node-fetch')
const apiRoot = 'https://api.codeship.com/v2'
class CodeshipNode {
constructor ({ orgUuid, orgName, username, password }) {
if ((!orgUuid && !orgName) || !username || !password) {
console.warn('Error: Credentials not supplied to CodeshipNode', { orgUuid, orgName, username, password })
}
this.orgUuid = orgUuid
this.orgName = orgName
this.username = username
this.password = password
// Make sure the interface is as expected
this.builds = {
get: (uuid) => this.checkAuth()
.then(() => this.getBuild(uuid)),
list: (projectUuid) => this.checkAuth()
.then(() => this.listBuilds(projectUuid)),
restart: (uuid, projectUuid) => this.checkAuth()
.then(() => this.restartBuild(uuid, projectUuid))
}
this.projects = {
get: (uuid) => this.checkAuth()
.then(() => this.getProject(uuid)),
list: () => this.checkAuth()
.then(() => this.listProjects())
}
this.last = {
projectUuid: null,
buildUuid: null
}
}
auth ({ username, password } = this) {
const encodedInfo = Buffer.from(`${username}:${password}`).toString('base64')
const url = `${apiRoot}/auth`
// const body = 'grant_type=client_credentials'
const method = 'POST'
const headers = {
'Authorization': `Basic ${encodedInfo}`,
'content-type': 'application/x-www-form-urlencoded'
}
const options = { method, headers }
return fetch(url, options)
.then(response => response.json())
.then(json => {
this.saveOrganisation(json)
const token = this.saveToken(json)
return token
})
.catch(error => {
console.error(`Error authorising`, error)
return Promise.reject(error)
})
}
checkAuth () {
// Doesn't contain expiry check
return this.token ? Promise.resolve() : this.auth()
}
saveOrganisation ({ organizations = [] }) {
const { orgUuid, orgName } = this
const organisation = organizations.find(({ uuid, name }) => (uuid === orgUuid || name === orgName))
this.orgUuid = organisation.uuid
this.orgName = organisation.name
}
saveToken ({ access_token: token }) {
this.token = token
return token
}
getBuild (uuid, projectUuid = this.last.projectUuid) {
const { orgUuid } = this
this.last.buildUuid = uuid
const url = `/organizations/${orgUuid}/projects/${projectUuid}/builds/{uuid}`
return this.request({ url })
.then(({ build }) => (build))
}
getProject (uuid) {
const { orgUuid } = this
this.last.projectUuid = uuid
const url = `/organizations/${orgUuid}/projects/${uuid}`
return this.request({ url })
.then(({ project }) => (project))
}
listBuilds (projectUuid, { perPage = 30, page = 1 } = {}) {
const { orgUuid } = this
this.last.projectUuid = projectUuid
const query = { per_page: perPage, page }
const url = `/organizations/${orgUuid}/projects/${projectUuid}/builds`
return this.request({ url, query })
.then((response) => {
const { builds, page, per_page: perPage } = response
if (!this.hasMore(response)) {
return builds
}
return this.listBuilds(projectUuid, { page: page + 1, perPage })
.then((moreBuilds) => [...builds, ...moreBuilds])
})
.then((builds) => (builds))
}
listProjects () {
const { orgUuid } = this
const url = `/organizations/${orgUuid}/projects`
return this.request({ url })
.then(({ projects }) => (projects))
}
restartBuild (uuid, projectUuid = this.last.projectUuid) {
const { orgUuid } = this
this.last.buildUuid = uuid
const url = `/organizations/${orgUuid}/projects/${projectUuid}/builds/${uuid}/restart`
const method = 'POST'
return this.request({ url, method })
}
hasMore ({ page, per_page: perPage, total }) {
const totalPages = Math.ceil(total / perPage)
return page < totalPages
}
formatQuery ({ query }) {
const formattedQuery = Object.keys(query).map(key => {
const value = encodeURIComponent(query[key])
return `${key}=${value}`
})
return `?${formattedQuery.join('&')}`
}
request ({ method = 'GET', url, query }) {
// Check auth
if (!this.token) {
return this.auth()
.then(() => this.request({ method, url }))
}
const { token } = this
let fullUrl = `${apiRoot}${url}`
if (query) {
const formattedQuery = this.formatQuery({ query })
fullUrl = `${fullUrl}?${formattedQuery}`
}
const headers = {
'Authorization': `Bearer ${token}`
}
return fetch(fullUrl, { headers, method })
.then(response => (response.status === 202 ? null : response.json()))
.catch(error => {
console.error(`Error making request to ${fullUrl}`, error)
return Promise.reject(error)
})
}
}
module.exports = CodeshipNode