-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_release.py.bak
128 lines (101 loc) · 4.1 KB
/
auto_release.py.bak
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
# # POST /repos/:owner/:repo/releases
# post_parameters = {
# "tag_name": "v1.0.0",
# # "target_commitish": "master",
# "name": "v1.0.0",
# "body": "Description of the release",
# "draft": False,
# "prerelease": False
# }
# #!/usr/bin/env python
# import os
# import re
# import argparse
# import requests
# import mimetypes
# def check_name(name):
# if not re.match(r"^\w[-\.\w]*$", name):
# raise RuntimeError("invalid name '{0}'".format(name))
# return name
# def already_exists(response, field):
# if response.status_code != 422:
# return False
# for error in response.json().get("errors", []):
# if error.get("field") != field or error.get("code") != "already_exists":
# return False
# return True
# def create_release(token, owner, repo, tag, exclusive=False, prerelease=False):
# response = requests.post(
# "https://api.github.com/repos/{0}/{1}/releases".format(owner, repo),
# json={
# "tag_name": tag,
# "name": tag,
# "prerelease": prerelease
# },
# headers={
# "Authorization": "token {0}".format(token)
# }
# )
# if already_exists(response, "tag_name") and not exclusive:
# response = requests.get(
# "https://api.github.com/repos/{0}/{1}/releases/tags/{2}".format(owner, repo, tag),
# headers={
# "Authorization": "token {0}".format(token)
# }
# )
# response.raise_for_status()
# return False, response.json()
# response.raise_for_status()
# return True, response.json()
# def upload_asset(token, upload_url, filename):
# content_type, _ = mimetypes.guess_type(filename)
# if content_type is None:
# content_type = "application/octet-stream"
# with open(filename, "rb") as fileobj:
# response = requests.post(
# upload_url,
# data=fileobj,
# params={
# "name": os.path.basename(filename)
# },
# headers={
# "Authorization": "token {0}".format(token),
# "Content-Type": content_type
# }
# )
# if already_exists(response, "name"):
# return False
# response.raise_for_status()
# return True
# def main():
# parser = argparse.ArgumentParser(description="Backup GitHub repositories.")
# parser.add_argument("-x", "--exclusive", action="store_true", help="fail if the release already exists")
# parser.add_argument("-p", "--prerelease", action="store_true", help="mark the release as prerelease")
# parser.add_argument("token", metavar="TOKEN", help="GitHub API token")
# parser.add_argument("owner", metavar="OWNER", help="project owner")
# parser.add_argument("repo", metavar="REPO", help="project repository name")
# parser.add_argument("tag", metavar="TAG", help="tag for the release")
# parser.add_argument("assets", metavar="ASSET", help="uploadable asset file", nargs="*")
# args = parser.parse_args()
# tag = check_name(args.tag)
# owner = check_name(args.owner)
# repo = check_name(args.repo)
# for asset in args.assets:
# with open(asset, "rb") as fileobj:
# if os.fstat(fileobj.fileno()).st_size == 0:
# raise RuntimeError("can not attach empty file '{0}'".format(asset))
# created, release = create_release(args.token, owner, repo, tag, args.exclusive, args.prerelease)
# if created:
# print "Created a new release {0}".format(release["name"])
# else:
# print "Already existing release {0}".format(release["name"])
# # upload_url is given in uri-template form. We could
# # use a package for this...
# upload_url, _, _ = release["upload_url"].partition("{")
# for asset in args.assets:
# if upload_asset(args.token, upload_url, asset):
# print "Uploaded asset '{0}'".format(asset)
# else:
# print "Skipped already existing asset '{0}'".format(asset)
# if __name__ == "__main__":
# main()