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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
open Bos_setup
module D = struct
let pr_url = "${pr_url}"
let pr_node_id = "${pr_node_id}"
let download_url = "${download_url}"
let release_id = 1
let asset_name = "${asset_name}"
end
module Parse = struct
let path_from_regexp_opt uri regexp =
try
Some
("git@github.com:"
^ Re.(Group.get (exec (Emacs.compile_pat regexp) uri) 1))
with Not_found -> None
let ssh_uri_from_http uri =
match uri with
| _ when Bos_setup.String.is_prefix uri ~affix:"git@" ->
path_from_regexp_opt uri "git@github\\.com:\\(.+\\)"
| _ when Bos_setup.String.is_prefix uri ~affix:"git://" ->
path_from_regexp_opt uri "git://github\\.com/\\(.+\\)"
| _ when Bos_setup.String.is_prefix uri ~affix:"https://" ->
path_from_regexp_opt uri "https://github\\.com/\\(.+\\)"
| _ -> None
end
let run_with_auth ?(default_body = `Null) ~dry_run Curl.{ url; args; meth } =
let args = Curl_option.to_string_list args in
if dry_run then
Sos.show "exec:@[@ curl %a@]"
Format.(pp_print_list ~pp_sep:pp_print_space pp_print_string)
args
>>| fun () -> default_body
else
OS.Cmd.must_exist (Cmd.v "curl") >>= fun _ ->
let req = Curly.Request.make ~url ~meth () in
Logs.debug (fun l ->
l "[curl] executing request:@;<1 2>%a" Curly.Request.pp req);
Logs.debug (fun l ->
l "[curl] with args:@;<1 2>%a" (Fmt.list ~sep:Fmt.sp Fmt.string) args);
match Curly.run ~args req with
| Ok resp ->
Logs.debug (fun l ->
l "[curl] response received:@;<1 2>%a" Curly.Response.pp resp);
Json.from_string resp.body
| Error e -> R.error_msgf "curl execution failed: %a" Curly.Error.pp e
let curl_create_release ~token ~dry_run ~version ~tag ~draft msg user repo =
let curl_t =
Github_v3_api.Release.Request.create ~version ~tag ~msg ~user ~repo ~draft
in
let curl_t = Github_v3_api.with_auth ~token curl_t in
let default_body = `Assoc [ ("id", `Int D.release_id) ] in
run_with_auth ~dry_run ~default_body curl_t
>>= Github_v3_api.Release.Response.release_id
let curl_upload_archive ~token ~dry_run ~yes archive user repo release_id =
let curl_t =
Github_v3_api.Archive.Request.upload ~archive ~user ~repo ~release_id
in
let curl_t = Github_v3_api.with_auth ~token curl_t in
let default_body =
`Assoc
[
("browser_download_url", `String D.download_url);
("name", `String D.asset_name);
]
in
Prompt.try_again ~yes ~default_answer:Prompt.Yes
~question:(fun l ->
l "Uploading %a as release asset failed. Try again?" Text.Pp.path archive)
(fun () ->
run_with_auth ~dry_run ~default_body curl_t >>= fun response ->
Github_v3_api.Archive.Response.browser_download_url response
>>= fun url ->
Github_v3_api.Archive.Response.name response >>= fun name -> Ok (url, name))
let open_pr ~token ~dry_run ~title ~fork_owner ~branch ~opam_repo ~draft body
pkg =
let curl_t =
Github_v3_api.Pull_request.Request.open_ ~title ~fork_owner ~branch ~body
~opam_repo ~draft
in
let curl_t = Github_v3_api.with_auth ~token curl_t in
let default_body = `Assoc [ ("html_url", `String D.pr_url) ] in
run_with_auth ~dry_run ~default_body curl_t >>= fun json ->
(if draft then
Pkg.build_dir pkg >>= fun build_dir ->
Pkg.name pkg >>= fun name ->
Pkg.version pkg >>= fun version ->
Github_v3_api.Pull_request.Response.number json >>= fun pr_number ->
Config.Draft_pr.set ~dry_run ~build_dir ~name ~version
(string_of_int pr_number)
else Ok ())
>>= fun () -> Github_v3_api.Pull_request.Response.html_url json
let undraft_release ~token ~dry_run ~owner ~repo ~release_id ~name =
(match int_of_string_opt release_id with
| Some id -> Ok id
| None -> R.error_msgf "Invalid Github Release id: %s" release_id)
>>= fun release_id ->
let curl_t = Github_v3_api.Release.Request.undraft ~owner ~repo ~release_id in
let default_body =
`Assoc [ ("browser_download_url", `String D.download_url) ]
in
let curl_t = Github_v3_api.with_auth ~token curl_t in
run_with_auth ~dry_run ~default_body curl_t
>>= Github_v3_api.Release.Response.browser_download_url ~name
let undraft_pr ~token ~dry_run ~opam_repo:(user, repo) ~pr_id =
(match int_of_string_opt pr_id with
| Some id -> Ok id
| None -> R.error_msgf "Invalid Github PR number: %s" pr_id)
>>= fun pr_id ->
let curl_t =
Github_v4_api.Pull_request.Request.node_id ~user ~repo ~id:pr_id
in
let curl_t = Github_v4_api.with_auth ~token curl_t in
let default_body =
`Assoc
[
( "data",
`Assoc
[
( "repository",
`Assoc
[ ("pullRequest", `Assoc [ ("id", `String D.pr_node_id) ]) ]
);
] );
]
in
run_with_auth ~dry_run ~default_body curl_t
>>= Github_v4_api.Pull_request.Response.node_id
>>= fun node_id ->
let curl_t = Github_v4_api.Pull_request.Request.ready_for_review ~node_id in
let curl_t = Github_v4_api.with_auth ~token curl_t in
let default_body =
`Assoc
[
( "data",
`Assoc
[
( "markPullRequestReadyForReview",
`Assoc [ ("pullRequest", `Assoc [ ("url", `String D.pr_url) ]) ]
);
] );
]
in
run_with_auth ~dry_run ~default_body curl_t
>>= Github_v4_api.Pull_request.Response.url
let pkg_dev_repo p =
Pkg.dev_repo p >>= function
| Some r -> Ok r
| None ->
Pkg.opam p >>= fun opam ->
R.error_msgf "The field dev-repo is missing in %a." Fpath.pp opam
let check_tag ~dry_run vcs tag =
if Vcs.tag_exists ~dry_run vcs tag then Ok ()
else
R.error_msgf
"CHANGES.md lists '%a' as the latest release, but no corresponding tag \
has been found in the repository.@.Did you forget to call 'dune-release \
tag' ?"
Vcs.Tag.pp tag
let assert_tag_exists ~dry_run tag =
Vcs.get () >>= fun repo ->
if Vcs.tag_exists ~dry_run repo tag then Ok ()
else R.error_msgf "%a is not a valid tag" Vcs.Tag.pp tag
let determine_remote_tag_status ~local_rev ~remote_rev vcs =
match Vcs.commit_id ~commit_ish:remote_rev vcs with
| Ok resolved_rev when resolved_rev = local_rev && resolved_rev = remote_rev
->
`Up_to_date_unannotated
| Ok resolved_rev when resolved_rev = local_rev ->
`Up_to_date_annotated
| Ok resolved_rev -> `Points_to_different_commit resolved_rev
| Error _ -> `Points_to_missing_object
let remote_has_up_to_date_tag vcs ~local_rev ~remote_rev tag =
let points_to_different_commit pp_r =
App_log.unhappy (fun l ->
l
"The tag %a is present on the remote but points to a different \
commit (%a)."
Text.Pp.tag tag pp_r ())
in
match determine_remote_tag_status ~local_rev ~remote_rev vcs with
| `Up_to_date_annotated -> Ok true
| `Up_to_date_unannotated ->
App_log.unhappy (fun l ->
l
"The tag present on the remote is not annotated (it was not \
created by dune-release tag.)");
Ok true
| `Points_to_different_commit different_rev ->
points_to_different_commit (fun fmt () ->
Text.Pp.commit fmt different_rev);
Ok false
| `Points_to_missing_object ->
points_to_different_commit (fun fmt () ->
Format.fprintf fmt "that we don't have locally");
Ok false
let remote_has_tag_uptodate ~dry_run vcs ~dev_repo tag =
match Vcs.tag_points_to vcs tag with
| None -> Ok false
| Some local_rev -> (
Vcs.ls_remote ~dry_run vcs ~kind:`Tag ~filter:(Vcs.Tag.to_string tag)
dev_repo
>>= function
| [] -> Ok false
| (remote_rev, _) :: _ ->
remote_has_up_to_date_tag vcs ~local_rev ~remote_rev tag)
let push_tag ~dry_run ~yes ~dev_repo vcs tag =
remote_has_tag_uptodate ~dry_run vcs ~dev_repo tag >>= function
| true ->
App_log.status (fun l ->
l
"The tag %a is present and up-to-date on the remote: skipping the \
tag push"
Text.Pp.tag tag);
Ok ()
| false -> (
let uri =
match Parse.ssh_uri_from_http dev_repo with
| Some uri -> uri
| None ->
App_log.unhappy (fun l ->
l
"The uri %a is not recognized as a gihub uri, we are going \
to assume it is already a ssh uri."
Text.Pp.url dev_repo);
dev_repo
in
Prompt.confirm_or_abort ~yes
~question:(fun l ->
l "Push tag %a to %a?" Text.Pp.tag tag Text.Pp.url uri)
~default_answer:Yes
>>= fun () ->
App_log.status (fun l ->
l "Pushing tag %a to %a" Text.Pp.tag tag Text.Pp.url uri);
match
Vcs.run_git_quiet vcs ~dry_run
Cmd.(v "push" % "--force" % uri % Vcs.Tag.to_string tag)
with
| Ok () as ok -> ok
| Error (`Msg e) ->
R.error_msgf
"%s\n\
Pushing the tag failed, please push it manually and run the \
command again"
e)
let curl_get_release ~dry_run ~token ~tag ~user ~repo =
let curl_t = Github_v3_api.Release.Request.get ~tag ~user ~repo in
let curl_t = Github_v3_api.with_auth ~token curl_t in
run_with_auth ~dry_run curl_t >>= Github_v3_api.Release.Response.release_id
let create_release ~dry_run ~yes ~dev_repo ~token ~msg ~tag ~version ~user ~repo
~draft =
match curl_get_release ~dry_run ~token ~tag ~user ~repo with
| Error _ ->
Prompt.(
confirm_or_abort ~yes
~question:(fun l ->
l "Create %a %a on %a?" Text.Pp.maybe_draft (draft, "release")
Text.Pp.version version Text.Pp.url dev_repo)
~default_answer:Yes)
>>= fun () ->
App_log.status (fun l ->
l "Creating %a %a on %a via github's API" Text.Pp.maybe_draft
(draft, "release") Text.Pp.version version Text.Pp.url dev_repo);
curl_create_release ~token ~dry_run ~version ~tag msg user repo ~draft
>>= fun id ->
App_log.success (fun l ->
l "Successfully created %a with id %d" Text.Pp.maybe_draft
(draft, "release") id);
Ok id
| Ok id ->
App_log.status (fun l -> l "Release with id %d already exists" id);
Ok id
let publish_distrib ~token ?dev_repo ~dry_run ~msg ~archive ~yes ~draft p =
Pkg.infer_github_repo p >>= fun { owner; repo } ->
Pkg.tag p >>= fun tag ->
assert_tag_exists ~dry_run tag >>= fun () ->
Vcs.get () >>= fun vcs ->
check_tag ~dry_run vcs tag >>= fun () ->
let dev_repo =
match dev_repo with Some d -> Ok d | None -> pkg_dev_repo p
in
dev_repo >>= fun dev_repo ->
Pkg.build_dir p >>= fun build_dir ->
Pkg.name p >>= fun name ->
Pkg.version p >>= fun version ->
push_tag ~dry_run ~yes ~dev_repo vcs tag >>= fun () ->
create_release ~dry_run ~yes ~dev_repo ~token ~version ~msg ~tag ~user:owner
~repo ~draft
>>= fun id ->
(if draft then
Config.Draft_release.set ~dry_run ~build_dir ~name ~version
(string_of_int id)
else Config.Draft_release.unset ~dry_run ~build_dir ~name ~version)
>>= fun () ->
Prompt.(
confirm_or_abort ~yes
~question:(fun l -> l "Upload %a as release asset?" Text.Pp.path archive)
~default_answer:Yes)
>>= fun () ->
App_log.status (fun l ->
l "Uploading %a as a release asset for %a via github's API" Text.Pp.path
archive Text.Pp.version version);
curl_upload_archive ~token ~dry_run ~yes archive owner repo id
>>= fun (url, asset_name) ->
(if draft then
Config.Release_asset_name.set ~dry_run ~build_dir ~name ~version asset_name
else Config.Release_asset_name.unset ~dry_run ~build_dir ~name ~version)
>>= fun () -> Ok url
let rec pp_list pp ppf = function
| [] -> ()
| [ x ] -> pp ppf x
| [ x; y ] -> Fmt.pf ppf "%a and %a" pp x pp y
| h :: t -> Fmt.pf ppf "%a, %a" pp h (pp_list pp) t
let pr_title ~names ~version ~project_name ~pkgs_to_submit =
let number_of_pkgs = List.length names in
let pp_name ppf =
match (pkgs_to_submit, project_name) with
| [], Some project_name when number_of_pkgs > 1 ->
Format.fprintf ppf "%s (%d packages)" project_name number_of_pkgs
| [], _ -> pp_list Fmt.string ppf names
| pkgs_to_submit, _ -> pp_list Fmt.string ppf pkgs_to_submit
in
strf "[new release] %t (%a)" pp_name Version.pp version