This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
58 lines (50 loc) · 1.59 KB
/
builder.py
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
import git, os, shutil, sys
import requests
from flask import Flask
app = Flask(__name__)
GIT_DIR = os.getenv('GIT_DIR')
if not GIT_DIR:
GIT_DIR = '/tmp/gitdir'
BUILDKITE_URL = os.getenv('BUILDKITE_URL')
if not BUILDKITE_URL:
print('export BUILDKITE_URL env-var')
exit(1)
GIT_URL = os.getenv('GIT_URL')
if not GIT_URL:
print('export GIT_URL env-var')
exit(1)
if not os.path.exists(GIT_DIR ):
os.mkdir(GIT_DIR )
print 'Cloning repo'
repo = git.Repo.init(GIT_DIR , bare=True)
origin = repo.create_remote('origin',GIT_URL)
else:
repo = git.Repo(GIT_DIR )
if not isinstance(repo, git.Repo):
print 'Not a valid repo'
exit(1)
origin = repo.remotes.origin
fetched = origin.fetch()
@app.route('/', methods=['GET', 'POST'])
def build():
fetched = origin.fetch()
output = '<ul>'
for fetch in fetched:
output += '<li>' + fetch.remote_ref_path + ' / ' + str(fetch.ref) + ' (' + str(fetch.flags) + ')'
if not fetch.flags & fetch.HEAD_UPTODATE:
output += ' -> trigger!'
commit = repo.commit(fetch.ref)
data = {
'commit': 'HEAD',
'branch': str(fetch.ref)[len('origin') + 1:],
'message': commit.summary,
'author': {'name': commit.author.name, 'email': commit.author.email}
}
requests.post(BUILDKITE_URL, json=data)
output += '</li>'
return output + '</ul>'
if __name__ == '__main__':
debug = False
if os.getenv('DEBUG'):
debug = True
app.run(debug=debug, host='0.0.0.0', use_reloader=False)