-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_lastrelease.py
154 lines (137 loc) · 5.17 KB
/
git_lastrelease.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Daniel BUTEAU <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
import json, re
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import open_url
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': 'preview',
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: git_lastrelease
short_description: get the latest release name from github or gitlab project
description: get the latest release name from github or gitlab project
version_added: 2.8.5
options:
repo:
description:
- url to the git repo
required: true
choices: ['github.com', 'gitlab.com', 'gitlab.your_instance.suffix']
namespace:
description:
- the group, namespace or owner where the git project is
required: true
project:
description:
- the name of the project, usually in the url it's the part before the .git
required: true
draft:
description:
- only used with github.com, set to true if you want to get the latest draft release
required: false
choices: [ 'true', 'false']
default: 'false'
prerelease:
description:
- put to yes if you want to get future release, false if only existing one
required: false
choices: [ 'true', 'false']
default: 'false'
token:
description:
- if your project is not publicly available give your personnal token
required: false
author: "Daniel BUTEAU (@daniel_buteau)"
'''
def list_releases(module, repo, namespace, project, token):
if repo == "github.com":
RELEASE_URL = "https://api.%s/repos/%s/%s/releases" % ('github.com', namespace, project)
if token in vars() and token != '':
headers = {
"Accept": "application/json",
"Authorization": ("token %s" % token)
}
else:
headers = {"Accept": "application/json"}
elif re.search('gitlab', repo):
RELEASE_URL = "https://%s/api/v4/projects/%s%%2F%s/releases" % (repo, namespace, project)
if token in vars() and token != '':
headers = {
"Accept": "application/json",
"Authorization": ("Bearer %s" % token)
}
else:
headers = {"Accept": "application/json"}
try:
module.debug('debug %s' % RELEASE_URL)
req = open_url(RELEASE_URL, headers=headers, timeout=6)
return json.loads(req.read())
except Exception as err:
return module.fail_json(msg=str(err))
def select_release(releases, repo, draft, prerelease):
if repo == "github.com" and draft == 'true':
for key in releases:
if key['draft'] == 'false':
releases.pop(key)
releases.sort(key=lambda r: r['created_at'])
releases = releases[0]['name']
elif prerelease == 'true':
for key in releases:
if repo == 'github.com':
if key['prerelease'] == 'false':
releases.pop(key)
else:
if key['upcoming_release'] == 'false':
releases.pop(key)
releases.sort(key=lambda r: r['created_at'])
if repo == 'github.com':
release = {
'name': releases[0]['name'], 'links':
[
{'zip': releases[0]['zipball_url']},
{'tar': releases[0]['tarball_url']}
]}
else:
release = {'name':releases[0]['name'], 'links': []}
for rel_format, url in releases[0]['assets']['sources']:
release['links'].append({rel_format: url})
return release
def main():
module = AnsibleModule(
argument_spec=dict(
repo=dict(required=True),
namespace=dict(required=True),
project=dict(required=True),
draft=dict(required=False, default='false'),
prerelease=dict(required=False, default='false'),
token=dict(required=False)
),
supports_check_mode=True
)
repo = module.params['repo']
namespace = module.params['namespace']
project = module.params['project']
draft = module.params['draft']
prerelease = module.params['prerelease']
token = module.params['token']
if module.check_mode:
latest_release = {
'name': 'v0.0.0', 'links':
[
{'zip' : 'https://%s/%s/%s/%s-v0.0.0.zip' % (repo, namespace, project, project)},
{'tar' : 'https://%s/%s/%s/%s-v0.0.0.tar' % (repo, namespace, project, project)},
{'tar.gz': 'https://%s/%s/%s/%s-v0.0.0.tgz' % (repo, namespace, project, project)}
]
}
else:
try:
releases = list_releases(module, repo, namespace, project, token)
latest_release = select_release(releases, repo, draft, prerelease)
except Exception as err:
return module.fail_json(msg=str(err))
return module.exit_json(results=latest_release)
if __name__ == '__main__':
main()