-
Notifications
You must be signed in to change notification settings - Fork 8
/
scheduleserver.coffee
78 lines (71 loc) · 2.43 KB
/
scheduleserver.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
'use strict'
schedule = require('node-schedule')
GitHubApi = require('github')
request = require("request")
yaml = require('js-yaml')
_ = require('underscore')
github = new GitHubApi
version: '3.0.0'
debug: if process.env.NODE_ENV is 'development' then true else false
protocol: 'https'
config = require('./lib/config')
logger = require('./lib/logger')
redis_url = require("url").parse config.redis_url
redis_hostname = redis_url.hostname
redis_port = redis_url.port or 6379
redis = require("redis").createClient(redis_port, redis_hostname)
# update public opentest.task repos
updateTaskRepos = (page, per_page) ->
github.search.repos
q: 'opentest.task in:name fork:true'
sort: 'stars'
order: 'desc'
per_page: per_page
page: page
client_id: config.github.clientID
client_secret: config.github.clientSecret
, (error, repos) ->
if error
logger.error "Error when retrieving github repositories due to #{error}"
else
hash = {}
items = []
items.push item for item in repos.items when item.name.match(/^opentest.task\s*-/)
count = items.length
for item in items
do (item) ->
getEnv item.owner.login, item.name, item.default_branch, (err, environ) ->
item.environ = if err? then {} else environ
count--
if count is 0
hash[item.id] = JSON.stringify(item) for item in items
redis.hmset 'opentest:task:repositories', hash
if page < Math.ceil(repos.total_count/per_page)
updateTaskRepos page + 1, per_page
getEnv = (user, repo, branch, callback) ->
url = "https://raw.github.com/#{user}/#{repo}/#{branch}/.init.yml"
request.get url, (err, res, body) ->
if err? or res.statusCode isnt 200
callback "Error when retrieving file .init.yml"
else
try
doc = yaml.safeLoad body
env = doc.env or {}
for name, value of env
if value instanceof Array
env[name] =
options: value
fix: false
exclusive: false
else
env[name] =
options: if value.options instanceof Array then value.options else []
fix: value.fix or false
exclusive: value.exclusive or false
callback null, env
catch e
return callback e
# schedule it every minute
schedule.scheduleJob '* * * * *', ->
logger.info 'Update opentest.task repos.'
updateTaskRepos 1, 100