15
15
16
16
17
17
class Publish (BaseModel ):
18
- version : str
18
+ ref : str | None
19
+ version : str | None
19
20
metadata : dict [str , Any ] | None
20
21
metadata_errors : str | None
21
22
readme : str | None
@@ -46,18 +47,55 @@ def publish(
46
47
# content={"message": "Private repositories are not supported, \
47
48
# see https://github.com/flakestry/flakestry.dev/issues"})
48
49
50
+ github_headers = {
51
+ "Authorization" : f"Bearer { github_token } " ,
52
+ "X-GitHub-Api-Version" : "2022-11-28" ,
53
+ "Accept" : "application/vnd.github+json" ,
54
+ }
55
+
56
+ owner_name , repository_name = token .repository .split ("/" )
57
+ if publish .ref :
58
+ ref = publish .ref
59
+ elif publish .version :
60
+ ref = f"refs/tags/{ publish .version } "
61
+ else :
62
+ return JSONResponse (
63
+ status_code = status .HTTP_400_BAD_REQUEST ,
64
+ content = {"message" : 'Neither "ref" nor "version" were provided' },
65
+ )
66
+
67
+ # Get info on the commit to be published
68
+ commit_response = requests .get (
69
+ f"https://api.github.com/repos/{ owner_name } /{ repository_name } /commits/{ ref } " ,
70
+ headers = github_headers ,
71
+ )
72
+ commit_response .raise_for_status ()
73
+ commit_json = commit_response .json ()
74
+ commit_sha = commit_json ["sha" ]
75
+ commit_date = commit_json ["commit" ]["committer" ]["date" ]
76
+
77
+ # Validate & parse version
78
+ datetime = re .sub (r"[^0-9]" , "" , commit_date )
79
+ if publish .version :
80
+ given_version = publish .version .format (
81
+ datetime = datetime ,
82
+ date = datetime [:8 ],
83
+ time = datetime [8 :],
84
+ )
85
+ elif publish .ref and publish .ref .startswith ("refs/tags/" ):
86
+ given_version = publish .ref .removeprefix ("refs/tags/" )
87
+ else :
88
+ given_version = f"v0.1.{ datetime } "
89
+
49
90
version_regex = r"^v?([0-9]+\.[0-9]+\.?[0-9]*$)"
50
- version = re .search (version_regex , publish . version )
91
+ version = re .search (version_regex , given_version )
51
92
if not version :
52
93
return JSONResponse (
53
94
status_code = status .HTTP_400_BAD_REQUEST ,
54
- content = {
55
- "message" : f"{ publish .version } doesn't match regex { version_regex } "
56
- },
95
+ content = {"message" : f"{ given_version } doesn't match regex { version_regex } " },
57
96
)
58
97
else :
59
98
version = version .groups ()[0 ]
60
- owner_name , repository_name = token .repository .split ("/" )
61
99
62
100
# Create owner if it doesn't exist
63
101
owner = session .exec (
@@ -81,45 +119,24 @@ def publish(
81
119
session .commit ()
82
120
session .refresh (repo )
83
121
84
- # 400 if version already exists
122
+ # 409 if version already exists
85
123
if session .exec (
86
124
select (Release )
87
125
.where (Release .version == version )
88
126
.where (Release .repo_id == repo .id )
89
127
).first ():
90
128
return JSONResponse (
91
- status_code = status .HTTP_400_BAD_REQUEST ,
92
- content = {"message" : f"Version { publish .version } already exists" },
93
- )
94
-
95
- github_headers = {
96
- "Authorization" : f"Bearer { github_token } " ,
97
- "X-GitHub-Api-Version" : "2022-11-28" ,
98
- "Accept" : "application/vnd.github+json" ,
99
- }
100
- ref_response = requests .get (
101
- f"https://api.github.com/repos/{ owner_name } /{ repository_name } /git/ref/tags/{ publish .version } " ,
102
- headers = github_headers ,
103
- )
104
- ref_response .raise_for_status ()
105
- commit = ref_response .json ()["object" ]["sha" ]
106
-
107
- if ref_response .json ()["object" ]["type" ] == "tag" :
108
- # now we get the commit sha
109
- tag_response = requests .get (
110
- f"https://api.github.com/repos/{ owner_name } /{ repository_name } /git/tags/{ commit } " ,
111
- headers = github_headers ,
129
+ status_code = status .HTTP_409_CONFLICT ,
130
+ content = {"message" : f"Version { version } already exists" },
112
131
)
113
- tag_response .raise_for_status ()
114
- commit = tag_response .json ()["object" ]["sha" ]
115
132
116
133
# index README
117
134
try :
118
135
description = publish .metadata ["description" ]
119
136
except Exception :
120
137
description = None
121
138
122
- path = f"{ owner_name } /{ repository_name } /{ commit } /{ publish .readme } "
139
+ path = f"{ owner_name } /{ repository_name } /{ commit_sha } /{ publish .readme } "
123
140
if publish .readme :
124
141
readme_response = requests .get (
125
142
f"https://raw.githubusercontent.com/{ path } " , headers = github_headers
@@ -129,12 +146,13 @@ def publish(
129
146
else :
130
147
readme = None
131
148
149
+ # Do release
132
150
release = Release (
133
151
repo_id = repo .id ,
134
152
version = version ,
135
153
readme_filename = publish .readme ,
136
154
readme = readme ,
137
- commit = commit ,
155
+ commit = commit_sha ,
138
156
description = description ,
139
157
meta_data = publish .metadata ,
140
158
meta_data_errors = publish .metadata_errors ,
@@ -145,6 +163,7 @@ def publish(
145
163
session .commit ()
146
164
session .refresh (release )
147
165
166
+ # Index release
148
167
document = {
149
168
"description" : description ,
150
169
"readme" : readme ,
0 commit comments