This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.coffee
107 lines (79 loc) · 2.62 KB
/
app.coffee
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
#
# Contains routes and polling jobs
#
request = require 'request'
express = require 'express'
jenkinsapi = require 'jenkins-api'
cronJob = require('cron').CronJob
env = require './env'
github = require './lib/github'
integration = require './lib/integration'
log = require('./lib/logging').log
app = module.exports = express.createServer()
app.configure 'development', ->
app.set "port", 3000
log.info "Running on port 3000."
app.configure 'production', ->
app.use express.errorHandler()
app.set "port", 3000
log.info "Running on port 3000."
# GET a list of jenkins jobs
#
app.get '/jenkins/jobs', (req, res) ->
jenkins = jenkinsapi.init(env.JENKINS_AUTHED_URL)
jenkins.all_jobs (err, data) ->
if err
return (console.log err)
console.log data
res.send 'Ok', 200
jenkins.get_config_xml 'percolate', (err, data) ->
console.log data
# Delete a particular jenkins job
#
app.del '/jenkins/jobs', (req, res) ->
ghComm = new github.GithubCommunicator(
env.GITHUB_REPO_OWNER, env.GITHUB_REPO, env.GITHUB_OAUTH_TOKEN)
ghJenkinsInt = new integration.GithubPrJenkinsIntegrator ghComm
ghJenkinsInt.deleteAllJobs()
res.send 'Ok', 200
# Jenkins lets us know when a build is happening
#
app.get '/jenkins/pre_build', (req, res) ->
sha = req.param 'sha'
job = req.param 'job'
build = parseInt req.param 'build'
user = req.param 'user'
repo = req.param 'repo'
state = req.param 'status'
# Look for an open pull request with this SHA and make comments.
commenter = new github.PullRequestCommenter(
sha, job, build, user, repo, state, env.GITHUB_OAUTH_TOKEN)
commenter.updateComments (e, r) -> console.log e if e?
res.send 'Ok', 200
# Jenkins lets us know when a build has failed or succeeded
#
app.get '/jenkins/post_build', (req, res) ->
sha = req.param 'sha'
job = req.param 'job'
build = parseInt req.param 'build'
user = req.param 'user'
repo = req.param 'repo'
state = req.param 'status'
# Look for an open pull request with this SHA and make comments.
commenter = new github.PullRequestCommenter(
sha, job, build, user, repo, state, env.GITHUB_OAUTH_TOKEN)
commenter.updateComments (e, r) -> console.log e if e?
res.send 'Ok', 200
# Poll periodicially for new PRs, respond accordingly
#
start_polling = ->
ghComm = new github.GithubCommunicator(
env.GITHUB_REPO_OWNER, env.GITHUB_REPO, env.GITHUB_OAUTH_TOKEN)
ghJenkinsInt = new integration.GithubPrJenkinsIntegrator ghComm
ghJenkinsInt.sync()
new cronJob('0 */2 * * * *'
, () ->
ghJenkinsInt.sync()
, null, true)
app.listen app.settings.port
start_polling()