From e61b379423d353564e2dcf90034bd1f7e0b44bda Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Wed, 8 Sep 2021 16:02:02 +0200 Subject: [PATCH 1/6] Support github URL for pull requests --- lib/source.ml | 29 +++++++++++++++++++++++++++-- test/unit/test_source.ml | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/source.ml b/lib/source.ml index 89bd09d..1dc73e3 100644 --- a/lib/source.ml +++ b/lib/source.ml @@ -4,6 +4,28 @@ type t = Github_branch of Branch.t | Github_PR of Pull_request.t let github_pr pr = Github_PR pr +let parse_as_pr_url s = + let re = + Re.compile + (Re.seq + [ + Re.bos; + Re.str "https://github.com/"; + Re.group Pull_request.user_re; + Re.str "/"; + Re.group Pull_request.repo_re; + Re.str "/pull/"; + Re.group (Re.rep1 Re.digit); + Re.eos; + ]) + in + let open Let_syntax.Option in + let+ g = Re.exec_opt re s in + let user = Re.Group.get g 1 in + let repo = Re.Group.get g 2 in + let number = Re.Group.get g 3 |> int_of_string in + Github_PR { user; repo; number } + let parse_as_branch s = let branch_name = Re.rep1 Re.any in let re_branch = @@ -28,10 +50,13 @@ let parse_as_branch s = let parse_as_pr s = Option.map github_pr (Pull_request.parse s) let parse s = - match parse_as_branch s with + match parse_as_pr_url s with | Some r -> Ok r | None -> ( - match parse_as_pr s with Some r -> Ok r | None -> Error `Unknown) + match parse_as_branch s with + | Some r -> Ok r + | None -> ( + match parse_as_pr s with Some r -> Ok r | None -> Error `Unknown)) let pp ppf = function | Github_branch branch -> diff --git a/test/unit/test_source.ml b/test/unit/test_source.ml index 170c7e3..119fa55 100644 --- a/test/unit/test_source.ml +++ b/test/unit/test_source.ml @@ -33,6 +33,8 @@ let parse_tests = test "repos can have dashes" "user/repo-with-dashes#1234" (Ok (Github_PR { user = "user"; repo = "repo-with-dashes"; number = 1234 })); + test "url syntax for issues" "https://github.com/user/repo/pull/1234" + (Ok (Github_PR { user = "user"; repo = "repo"; number = 1234 })); ] let switch_target_tests = From f5c6f143f5170f80b83b761b45e79b347e3a0db9 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Wed, 8 Sep 2021 16:34:36 +0200 Subject: [PATCH 2/6] Support url syntax for branches --- lib/source.ml | 35 +++++++++++++++++++++++++++++++---- test/unit/test_source.ml | 4 ++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/source.ml b/lib/source.ml index 1dc73e3..c8419d2 100644 --- a/lib/source.ml +++ b/lib/source.ml @@ -26,8 +26,31 @@ let parse_as_pr_url s = let number = Re.Group.get g 3 |> int_of_string in Github_PR { user; repo; number } +let branch_name = Re.rep1 Re.any + +let parse_as_branch_url s = + let re = + Re.compile + (Re.seq + [ + Re.bos; + Re.str "https://github.com/"; + Re.group Pull_request.user_re; + Re.str "/"; + Re.group Pull_request.repo_re; + Re.str "/tree/"; + Re.group branch_name; + Re.eos; + ]) + in + let open Let_syntax.Option in + let+ g = Re.exec_opt re s in + let user = Re.Group.get g 1 in + let repo = Re.Group.get g 2 in + let branch = Re.Group.get g 3 in + Github_branch { user; repo; branch } + let parse_as_branch s = - let branch_name = Re.rep1 Re.any in let re_branch = Re.compile (Re.seq @@ -50,13 +73,17 @@ let parse_as_branch s = let parse_as_pr s = Option.map github_pr (Pull_request.parse s) let parse s = - match parse_as_pr_url s with + match parse_as_branch_url s with | Some r -> Ok r | None -> ( - match parse_as_branch s with + match parse_as_pr_url s with | Some r -> Ok r | None -> ( - match parse_as_pr s with Some r -> Ok r | None -> Error `Unknown)) + match parse_as_branch s with + | Some r -> Ok r + | None -> ( + match parse_as_pr s with Some r -> Ok r | None -> Error `Unknown)) + ) let pp ppf = function | Github_branch branch -> diff --git a/test/unit/test_source.ml b/test/unit/test_source.ml index 119fa55..da274e3 100644 --- a/test/unit/test_source.ml +++ b/test/unit/test_source.ml @@ -35,6 +35,10 @@ let parse_tests = (Github_PR { user = "user"; repo = "repo-with-dashes"; number = 1234 })); test "url syntax for issues" "https://github.com/user/repo/pull/1234" (Ok (Github_PR { user = "user"; repo = "repo"; number = 1234 })); + test "url syntax for branches" + "https://github.com/user/repo/tree/branch_name" + (Ok + (Github_branch { user = "user"; repo = "repo"; branch = "branch_name" })); ] let switch_target_tests = From 01554228fbcdc258bd6e041909aaf9d4d494e3a9 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Wed, 8 Sep 2021 16:38:09 +0200 Subject: [PATCH 3/6] Percent-decode URLs --- dune-project | 3 ++- lib/dune | 2 +- lib/source.ml | 2 +- opam-compiler.opam | 1 + test/unit/test_source.ml | 9 +++++++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/dune-project b/dune-project index 9f368c7..0ff93f0 100644 --- a/dune-project +++ b/dune-project @@ -42,6 +42,7 @@ (alcotest (and (>= 1.2.0) + uri :with-test))) (conflicts - (result (< 1.5)))) + (result (< 1.5)))) \ No newline at end of file diff --git a/lib/dune b/lib/dune index 7ffc472..5c7b399 100644 --- a/lib/dune +++ b/lib/dune @@ -1,3 +1,3 @@ (library (name opam_compiler) - (libraries bos cmdliner curly github-data ocaml-version re)) + (libraries bos cmdliner curly github-data ocaml-version re uri)) diff --git a/lib/source.ml b/lib/source.ml index c8419d2..f013f18 100644 --- a/lib/source.ml +++ b/lib/source.ml @@ -44,7 +44,7 @@ let parse_as_branch_url s = ]) in let open Let_syntax.Option in - let+ g = Re.exec_opt re s in + let+ g = Re.exec_opt re (Uri.pct_decode s) in let user = Re.Group.get g 1 in let repo = Re.Group.get g 2 in let branch = Re.Group.get g 3 in diff --git a/opam-compiler.opam b/opam-compiler.opam index b9ec694..6f38573 100644 --- a/opam-compiler.opam +++ b/opam-compiler.opam @@ -21,6 +21,7 @@ depends: [ "re" "rresult" {>= "0.6.0"} "alcotest" {>= "1.2.0" & with-test} + "uri" "odoc" {with-doc} ] conflicts: [ diff --git a/test/unit/test_source.ml b/test/unit/test_source.ml index da274e3..61a2633 100644 --- a/test/unit/test_source.ml +++ b/test/unit/test_source.ml @@ -39,6 +39,15 @@ let parse_tests = "https://github.com/user/repo/tree/branch_name" (Ok (Github_branch { user = "user"; repo = "repo"; branch = "branch_name" })); + test "urls need to be decoded" + "https://github.com/user/repo/tree/4.12.0+domains%2Bsafepoint%2Bchannel_hooks" + (Ok + (Github_branch + { + user = "user"; + repo = "repo"; + branch = "4.12.0+domains+safepoint+channel_hooks"; + })); ] let switch_target_tests = From aba8a324513e3afd913094004dfcd79a20b42c71 Mon Sep 17 00:00:00 2001 From: Etienne Millon Date: Wed, 8 Sep 2021 16:41:56 +0200 Subject: [PATCH 4/6] Use a binding operator to try parsers --- lib/source.ml | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/source.ml b/lib/source.ml index f013f18..e8e09de 100644 --- a/lib/source.ml +++ b/lib/source.ml @@ -72,18 +72,14 @@ let parse_as_branch s = let parse_as_pr s = Option.map github_pr (Pull_request.parse s) +let ( let/ ) x f = match x with Some r -> Ok r | None -> f () + let parse s = - match parse_as_branch_url s with - | Some r -> Ok r - | None -> ( - match parse_as_pr_url s with - | Some r -> Ok r - | None -> ( - match parse_as_branch s with - | Some r -> Ok r - | None -> ( - match parse_as_pr s with Some r -> Ok r | None -> Error `Unknown)) - ) + let/ () = parse_as_branch_url s in + let/ () = parse_as_pr_url s in + let/ () = parse_as_branch s in + let/ () = parse_as_pr s in + Error `Unknown let pp ppf = function | Github_branch branch -> From b8d0a70bae2db81fad3a031a4129f7b7dced439b Mon Sep 17 00:00:00 2001 From: Tim McGilchrist Date: Thu, 26 Sep 2024 18:22:31 +1000 Subject: [PATCH 5/6] Rebase changes --- CHANGES.md | 3 +++ dune-project | 2 +- opam-compiler.opam | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 41a31e1..bb0a374 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## Unreleased +- Support Github URLs as sources (#32, @emillon) + ## 0.2.0 (2024-01-19) - Use curl to download (#10, @emillon) diff --git a/dune-project b/dune-project index 0ff93f0..2fc0c97 100644 --- a/dune-project +++ b/dune-project @@ -39,10 +39,10 @@ re (rresult (>= 0.6.0)) + uri (alcotest (and (>= 1.2.0) - uri :with-test))) (conflicts (result (< 1.5)))) \ No newline at end of file diff --git a/opam-compiler.opam b/opam-compiler.opam index 6f38573..bdcd520 100644 --- a/opam-compiler.opam +++ b/opam-compiler.opam @@ -20,8 +20,8 @@ depends: [ "ocaml-version" {>= "3.0.0"} "re" "rresult" {>= "0.6.0"} - "alcotest" {>= "1.2.0" & with-test} "uri" + "alcotest" {>= "1.2.0" & with-test} "odoc" {with-doc} ] conflicts: [ From 1a518c0740006b3899d444588f01154157dd5033 Mon Sep 17 00:00:00 2001 From: Tim McGilchrist Date: Thu, 26 Sep 2024 19:19:54 +1000 Subject: [PATCH 6/6] Add example for using github urls --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 5010b7f..0a2f56f 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ example, the following is recognized: # Use this branch opam compiler create 'myself/ocaml:mybranch' + # Use this pull request + opam compiler create https://github.com/ocaml/ocaml/pull/10831 + It will try determine a switch name and description from the source name, but it is also possible to pass an explicit switch name: