-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (103 loc) · 3.3 KB
/
index.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
let BaseModule
try {
BaseModule = require(require('requireg').resolve('huestatus/src/Module'))
} catch (e) {
throw new Error('A HueStatus installation is required -- npm install -g huestatus')
}
const Travis = require('travis-ci')
/**
* Module
* @extends BaseModule
*/
class HueTravis extends BaseModule {
/**
* Generate instance name based on repo and repo
* @return {String} [description]
*/
generateInstanceName () {
return `Travis: ${this.config.organisation}/${this.config.repo || '*'}`
}
/**
* Start method, called on huestatus start. Loops through api polling
*/
async start () {
this._setUpConfig()
setInterval(this._pollTravis.bind(this), this.config.pollInterval || 2000)
}
/**
* Check for config variables and create Travis API Client
* @throws error when required config value is not set
*/
_setUpConfig () {
['organisation'].map(configItem => {
if (!this.config[configItem]) {
throw new Error(`Travis ${configItem} config value not set`)
}
})
this.travis = new Travis({ version: '2.0.0' })
}
/**
* Make a request to the travis API, check for unresolved issues, then filter out already assigned issues
* @return {Promise}
*/
async _pollTravis () {
if (!this.config.repo) {
return this._pollAll()
}
return this._pollSingle()
}
/**
* Make a request to the travis API, check for unresolved issues, then filter out already assigned issues
* @return {Promise}
*/
async _pollSingle () {
const res = await new Promise((resolve, reject) => {
this.travis.repos(this.config.organisation, this.config.repo).get((err, res) => {
if (err) {
reject(err)
}
resolve(res)
})
})
return this.repoReport(res.repo)
}
/**
* Make a request to the travis API, check for unresolved repos, then filter out already assigned repos
* @return {Promise}
*/
async _pollAll () {
const res = await new Promise((resolve, reject) => {
this.travis.repos(this.config.organisation).get((err, res) => {
if (err) {
reject(err)
}
resolve(res)
})
})
const activeRepos = res.repos.filter(repo => repo.active)
let array = []
array = array.concat(activeRepos.filter(repo => repo.last_build_state === 'failed'))
array = array.concat(activeRepos.filter(repo => ['created', 'started'].includes(repo.last_build_state)))
array = array.concat(activeRepos.filter(repo => !['created', 'started', 'passed', 'failed'].includes(repo.last_build_state)))
array = array.concat(activeRepos.filter(repo => repo.last_build_state === 'passed'))
return this.repoReport(array[0])
}
/**
* Report the status of a repo to HueStatus
* @param {Object} repo Travis Repo object
* @return {Promise}
*/
async repoReport (repo) {
if (repo.last_build_state === 'failed') {
return this.change('alert', `${repo.slug} Failing`)
}
if (repo.last_build_state === 'passed') {
return this.change('ok', `${repo.slug} Passing`)
}
if (['created', 'started'].includes(repo.last_build_state)) {
return this.change('working', `${repo.slug} Building`)
}
return this.change('warning', `${repo.slug} ${repo.last_build_state || 'Unknown Status'}`)
}
}
module.exports = HueTravis