Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub urls #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased
- Support Github URLs as sources (#32, @emillon)

## 0.2.0 (2024-01-19)

- Use curl to download (#10, @emillon)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
re
(rresult
(>= 0.6.0))
uri
(alcotest
(and
(>= 1.2.0)
:with-test)))
(conflicts
(result (< 1.5))))
(result (< 1.5))))
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -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))
58 changes: 53 additions & 5 deletions lib/source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,53 @@ 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 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 (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
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
Expand All @@ -27,11 +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 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 ->
Expand Down
1 change: 1 addition & 0 deletions opam-compiler.opam
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ depends: [
"ocaml-version" {>= "3.0.0"}
"re"
"rresult" {>= "0.6.0"}
"uri"
"alcotest" {>= "1.2.0" & with-test}
"odoc" {with-doc}
]
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ 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 }));
test "url syntax for branches"
"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 =
Expand Down