This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit2travis.lua
More file actions
209 lines (164 loc) · 5.9 KB
/
Copy pathgit2travis.lua
File metadata and controls
209 lines (164 loc) · 5.9 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module("git2travis", package.seeall)
local pl = {}
pl.dir = require 'pl.dir'
pl.path = require 'pl.path'
pl.pretty = require 'pl.pretty'
pl.utils = require 'pl.utils'
local cfg = require 'git2travis.config'
local logging = require "logging"
require "logging.file"
local log = logging.file(cfg.logDir .. "/git2travis-%s.log", "%Y-%m-%d")
local json = require 'json'
local travisRepos = pl.dir.getdirectories(cfg.repoPath)
-- Connect with Travis CI
local function sleep(seconds)
os.execute("sleep " .. seconds)
end
local function urlencode(str)
str = string.gsub(str, "\r?\n", "\r\n")
str = string.gsub(str, "([^%w%-%.%_%~ ])", function (c)
return string.format("%%%02X", string.byte(c))
end)
str = string.gsub (str, " ", "+")
return str
end
local function travisRequest(method, endpoint, data)
local cmd = 'curl -X ' .. method .. ' ' ..
'-H "Content-Type: application/json" ' ..
'-H "Travis-API-Version: 3" ' ..
'-H "User-Agent: LunaCI" ' ..
'-H "Authorization: token ' .. cfg.travisToken .. '" '
if data then
cmd = cmd .. '-d \'' .. data .. '\' '
end
cmd = cmd .. 'https://api.travis-ci.org/' .. endpoint
local ok, _, stdout = pl.utils.executeex(cmd)
if not ok then
return ok, nil, cmd
end
local function decodeOutput()
return json.decode(stdout)
end
local ok, data = pcall(decodeOutput)
if ok then
return data["@type"] ~= "error", data, cmd
end
return false, "Error parsing json:\nInput: " .. stdout .. "\nError: " .. data, cmd
end
local function travisActivateRepo(repo)
return travisRequest("POST", "repo/" .. urlencode(repo) .. "/activate?include=branch.last_build")
end
local function travisRequestBuild(repo)
return travisRequest("POST", "repo/" .. urlencode(repo) .. "/requests")
end
local function travisSetEnv(repo)
return travisRequest(
"POST",
"repo/" .. urlencode(repo) .. "/env_vars",
'{ "env_var.name": "GITHUB_ACCESS_TOKEN", "env_var.value": "' .. cfg.githubToken .. '", "env_var.public": false }'
)
end
local function travisSync()
local ok, data, cmd = travisRequest("GET", "user")
if not ok then
return ok, data, cmd
end
local user_id = data['id']
return travisRequest("POST", "user/" .. user_id .. "/sync")
end
local function travisWireRepository(repo)
-- Assume that name of folder with repository is identical to module name
local modName = pl.path.relpath(repo, cfg.repoPath)
log:debug("Enabling Travis for '" .. modName .. "'...")
local ok, data = travisActivateRepo(cfg.githubDir .. "/" .. modName)
log:debug("Activate:\nData: " .. pl.pretty.write(data))
if not ok then
log:error("Error trying to activate repository on Travis-CI.\nData: " .. pl.pretty.write(data))
return false
end
local default_branch = data["default_branch"]
if not default_branch then
log:error("Default branch for '" .. modName .. "' is nil even if it shouldn't be.")
return false
end
local last_build = default_branch["last_build"]
if last_build then
log:debug("It seems '" .. modName .. "' is already wired up correctly, continuing to the next package...")
return true
end
local ok, data = travisSetEnv(cfg.githubDir .. "/" .. modName)
log:debug("Set env:\nData: " .. pl.pretty.write(data))
if not ok then
log:error("Error setting environment variables on Travis-CI.\nData: " .. pl.pretty.write(data))
if data["@type"] == "error" and data["error_type"] == "duplicate_resource" then
log:warn("The required variables seem to be set already, trying to continue as normal...")
else
return false
end
end
local ok, data = travisRequestBuild(cfg.githubDir .. "/" .. modName)
log:debug("Request:\nData: " .. pl.pretty.write(data))
if not ok then
log:error("Error requesting Travis-CI build.\nData: " .. pl.pretty.write(data))
return false
end
return true
end
log:debug("Syncing Travis account...")
local retriesRemaining = cfg.travisMaxTries
while retriesRemaining >= 0 do
local ok, data = travisSync()
if not ok then
log:error("Error syncing Travis account.\nData: " .. pl.pretty.write(data))
log:debug("Waiting for " .. cfg.travisSyncWait .. " seconds...")
sleep(cfg.travisSyncWait)
log:debug("Trying again. Number of tries left: " .. retriesRemaining)
retriesRemaining = retriesRemaining - 1
if retriesRemaining < 0 then
log:error("Failed to sync Travis, exiting...")
os.exit(1)
end
else
break
end
end
log:debug("Waiting for " .. cfg.travisSyncWait .. " seconds...")
sleep(cfg.travisSyncWait)
local stillInactive = travisRepos
log:debug("Repos still inactive on Travis: " .. #stillInactive)
local previousCount = #stillInactive
retriesRemaining = cfg.travisMaxTries
while #stillInactive > 0 do
travisRepos = stillInactive
stillInactive = {}
for _, repo in pairs(travisRepos) do
if not travisWireRepository(repo) then
table.insert(stillInactive, repo)
end
end
if #stillInactive > 0 then
log:debug("There are still some inactive Travis repos (" .. #stillInactive .. ").")
if #stillInactive == previousCount then
-- nothing happened, we're at the risk of running into an infinite loop
retriesRemaining = retriesRemaining - 1
if retriesRemaining < 0 then
log:error("Not all repositories were synced with Travis (" .. #stillInactive .. "): " .. pl.pretty.write(stillInactive))
os.exit(1)
end
if retriesRemaining > 0 then
log:debug((retriesRemaining + 1) .. " tries remaining...")
else
log:debug("Last try...")
end
else
previousCount = #stillInactive
retriesRemaining = cfg.travisMaxTries
end
log:debug("Syncing Travis and waiting for " .. cfg.travisSyncWait .. " seconds before trying again.")
local ok, data = travisSync()
if not ok then
log:error("Error syncing Travis account.\nData: " .. pl.pretty.write(data))
end
sleep(cfg.travisSyncWait)
end
end