From 9818fd814acc05fba1675f679a10fbdc6ad1cc0a Mon Sep 17 00:00:00 2001 From: Guillaume Petiot Date: Wed, 3 Apr 2024 10:51:55 +0100 Subject: [PATCH 1/2] Add the PR merge events to the contributions --- CHANGES.md | 1 + lib/contributions.ml | 34 ++++- lib/contributions.mli | 3 +- lib/contributions_json_response.ml | 5 + test/expect/main.ml | 202 +++++++++++++++-------------- test/lib/test_contributions.ml | 48 ++++++- 6 files changed, 190 insertions(+), 103 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 41a41d3..eb6a53e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ ### Added - Display curl requests and responses in debug mode (`-vv` or `--verbosity debug`) (#36, @gpetiot) +- Add the PR merge events to the contributions (#, @emillon, @gpetiot) ## 1.0.1 diff --git a/lib/contributions.ml b/lib/contributions.ml index ece4fb2..21b60e9 100644 --- a/lib/contributions.ml +++ b/lib/contributions.ml @@ -27,6 +27,14 @@ let query user = title body repository { nameWithOwner } + timelineItems(last:10, itemTypes:[MERGED_EVENT]) { + nodes { + ... on MergedEvent { + createdAt + actor { login } + } + } + } } } } @@ -75,7 +83,8 @@ module Repo_map = Map.Make (String) type item = { repo : string; - kind : [ `Issue | `Issue_comment | `PR | `Review of string | `New_repo ]; + kind : + [ `Issue | `Issue_comment | `PR | `Review of string | `Merge | `New_repo ]; date : Datetime.t; url : string; title : string; @@ -102,14 +111,28 @@ let read_issue_comments = let repo = c.repository.nameWithOwner in { kind = `Issue_comment; date; url; title; body; repo }) -let read_prs = - List.map (fun (c : Json.PullRequest.contribution) -> +let read_prs ~username = + List.fold_left + (fun acc (c : Json.PullRequest.contribution) -> let date = c.occurredAt in let url = c.pullRequest.url in let title = c.pullRequest.title in let body = c.pullRequest.body in let repo = c.pullRequest.repository.nameWithOwner in - { kind = `PR; date; url; title; body; repo }) + let timeline_items = c.pullRequest.timelineItems.nodes in + let acc = { kind = `PR; date; url; title; body; repo } :: acc in + let acc = + List.fold_left + (fun acc (it : Json.PullRequest.timelineItem) -> + let date = it.createdAt in + let login = it.actor.login in + if String.equal login username then + { kind = `Merge; date; url; title; body = ""; repo } :: acc + else acc) + acc timeline_items + in + acc) + [] let read_reviews = List.map (fun (c : Json.PullRequest.Review.contribution) -> @@ -151,7 +174,7 @@ let of_json ~period:(from, to_) ~user json = let items = let issues = read_issues contribs.issueContributions.nodes in let issue_comments = read_issue_comments root.issueComments.nodes in - let prs = read_prs contribs.pullRequestContributions.nodes in + let prs = read_prs ~username contribs.pullRequestContributions.nodes in let reviews = read_reviews contribs.pullRequestReviewContributions.nodes in @@ -190,6 +213,7 @@ let pp_title f t = Fmt.pf f "Commented on %S [#%s](%s)" t.title (id t.url) t.url | `PR -> Fmt.pf f "%s [#%s](%s)" t.title (id t.url) t.url | `Review s -> Fmt.pf f "%s %s [#%s](%s)" s t.title (id t.url) t.url + | `Merge -> Fmt.pf f "Merged %S [#%s](%s)" t.title (id t.url) t.url | `New_repo -> Fmt.pf f "Created repository [%s](%s)" t.repo t.url let pp_body f = function diff --git a/lib/contributions.mli b/lib/contributions.mli index 660c0e8..0d4eb33 100644 --- a/lib/contributions.mli +++ b/lib/contributions.mli @@ -4,7 +4,8 @@ end type item = { repo : string; - kind : [ `Issue | `Issue_comment | `PR | `Review of string | `New_repo ]; + kind : + [ `Issue | `Issue_comment | `PR | `Review of string | `Merge | `New_repo ]; date : Datetime.t; url : string; title : string; diff --git a/lib/contributions_json_response.ml b/lib/contributions_json_response.ml index 11742bc..bdeac1b 100644 --- a/lib/contributions_json_response.ml +++ b/lib/contributions_json_response.ml @@ -36,11 +36,16 @@ module Issue = struct end module PullRequest = struct + type actor = { login : string } [@@deriving yojson] + type timelineItem = { createdAt : string; actor : actor } [@@deriving yojson] + type timelineItems = { nodes : timelineItem list } [@@deriving yojson] + type t = { url : string; title : string; body : string; repository : Repository.name; + timelineItems : timelineItems; } [@@deriving yojson] diff --git a/test/expect/main.ml b/test/expect/main.ml index 562cbae..79b14dc 100644 --- a/test/expect/main.ml +++ b/test/expect/main.ml @@ -5,101 +5,111 @@ let contributions_example ~user = { username = user; activity = - Repo_map.empty - |> Repo_map.add "gpetiot/config.ml" - [ - { - repo = "gpetiot/config.ml"; - kind = `New_repo; - date = "2024-03-02T09:40:41Z"; - url = "https://github.com/gpetiot/config.ml"; - title = "Created new repository"; - body = ""; - }; - ] - |> Repo_map.add "gpetiot/js_of_ocaml" - [ - { - repo = "gpetiot/js_of_ocaml"; - kind = `New_repo; - date = "2024-03-01T10:43:33Z"; - url = "https://github.com/gpetiot/js_of_ocaml"; - title = "Created new repository"; - body = ""; - }; - ] - |> Repo_map.add "ocaml-ppx/ocamlformat" - [ - { - repo = "ocaml-ppx/ocamlformat"; - kind = `PR; - date = "2024-03-05T11:21:22Z"; - url = "https://github.com/ocaml-ppx/ocamlformat/pull/2533"; - title = "Represent the expr sequence as a list"; - body = "xxx"; - }; - ] - |> Repo_map.add "realworldocaml/mdx" - [ - { - repo = "realworldocaml/mdx"; - kind = `Review "APPROVED"; - date = "2024-03-05T11:43:04Z"; - url = - "https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244"; - title = "Add upgrade instructions in the changelog for #446"; - body = "xxx"; - }; - { - repo = "realworldocaml/mdx"; - kind = `PR; - date = "2024-03-04T17:20:11Z"; - url = "https://github.com/realworldocaml/mdx/pull/450"; - title = "Add an 'exec' label to execute include OCaml blocks"; - body = "xxx"; - }; - ] - |> Repo_map.add "tarides/get-activity" - [ - { - repo = "tarides/get-activity"; - kind = `Issue; - date = "2024-03-04T11:55:37Z"; - url = "https://github.com/tarides/get-activity/issues/8"; - title = - "Add the PR/issues comments to the result of okra generate"; - body = "xxx"; - }; - ] - |> Repo_map.add "tarides/okra" - [ - { - repo = "tarides/okra"; - kind = `Review "APPROVED"; - date = "2024-02-28T11:09:41Z"; - url = - "https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361"; - title = "Make README.md more precise"; - body = "xxx"; - }; - { - repo = "tarides/okra"; - kind = `Issue_comment; - date = "2024-03-13T11:09:56Z"; - url = - "https://github.com/tarides/okra/issues/114#issuecomment-1994130584"; - title = "Gitlab: exception when parsing Gitlab's JSON"; - body = "xxx"; - }; - { - repo = "tarides/okra"; - kind = `Issue; - date = "2024-02-27T12:05:04Z"; - url = "https://github.com/tarides/okra/issues/165"; - title = "Make the `get-activity` package known to ocaml-ci"; - body = "xxx"; - }; - ]; + Repo_map.of_seq @@ List.to_seq + @@ [ + ( "gpetiot/config.ml", + [ + { + repo = "gpetiot/config.ml"; + kind = `New_repo; + date = "2024-03-02T09:40:41Z"; + url = "https://github.com/gpetiot/config.ml"; + title = "Created new repository"; + body = ""; + }; + ] ); + ( "gpetiot/js_of_ocaml", + [ + { + repo = "gpetiot/js_of_ocaml"; + kind = `New_repo; + date = "2024-03-01T10:43:33Z"; + url = "https://github.com/gpetiot/js_of_ocaml"; + title = "Created new repository"; + body = ""; + }; + ] ); + ( "ocaml-ppx/ocamlformat", + [ + { + repo = "ocaml-ppx/ocamlformat"; + kind = `PR; + date = "2024-03-05T11:21:22Z"; + url = "https://github.com/ocaml-ppx/ocamlformat/pull/2533"; + title = "Represent the expr sequence as a list"; + body = "xxx"; + }; + { + repo = "ocaml-ppx/ocamlformat"; + kind = `Merge; + date = "2024-03-13T11:09:56Z"; + url = "https://github.com/ocaml-ppx/ocamlformat/pull/2533"; + title = "Represent the expr sequence as a list"; + body = ""; + }; + ] ); + ( "realworldocaml/mdx", + [ + { + repo = "realworldocaml/mdx"; + kind = `Review "APPROVED"; + date = "2024-03-05T11:43:04Z"; + url = + "https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244"; + title = "Add upgrade instructions in the changelog for #446"; + body = "xxx"; + }; + { + repo = "realworldocaml/mdx"; + kind = `PR; + date = "2024-03-04T17:20:11Z"; + url = "https://github.com/realworldocaml/mdx/pull/450"; + title = "Add an 'exec' label to execute include OCaml blocks"; + body = "xxx"; + }; + ] ); + ( "tarides/get-activity", + [ + { + repo = "tarides/get-activity"; + kind = `Issue; + date = "2024-03-04T11:55:37Z"; + url = "https://github.com/tarides/get-activity/issues/8"; + title = + "Add the PR/issues comments to the result of okra generate"; + body = "xxx"; + }; + ] ); + ( "tarides/okra", + [ + { + repo = "tarides/okra"; + kind = `Review "APPROVED"; + date = "2024-02-28T11:09:41Z"; + url = + "https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361"; + title = "Make README.md more precise"; + body = "xxx"; + }; + { + repo = "tarides/okra"; + kind = `Issue_comment; + date = "2024-03-13T11:09:56Z"; + url = + "https://github.com/tarides/okra/issues/114#issuecomment-1994130584"; + title = "Gitlab: exception when parsing Gitlab's JSON"; + body = "xxx"; + }; + { + repo = "tarides/okra"; + kind = `Issue; + date = "2024-02-27T12:05:04Z"; + url = "https://github.com/tarides/okra/issues/165"; + title = "Make the `get-activity` package known to ocaml-ci"; + body = "xxx"; + }; + ] ); + ]; } let%expect_test "Contributions.pp" = @@ -127,6 +137,8 @@ let%expect_test "Contributions.pp" = Represent the expr sequence as a list [#2533](https://github.com/ocaml-ppx/ocamlformat/pull/2533). xxx + Merged "Represent the expr sequence as a list" [#2533](https://github.com/ocaml-ppx/ocamlformat/pull/2533). + ### realworldocaml/mdx APPROVED Add upgrade instructions in the changelog for #446 [#449](https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244). diff --git a/test/lib/test_contributions.ml b/test/lib/test_contributions.ml index 9b9540e..5a03986 100644 --- a/test/lib/test_contributions.ml +++ b/test/lib/test_contributions.ml @@ -8,13 +8,20 @@ module Testable = struct module Item = struct module Kind = struct - type t = [ `Issue | `Issue_comment | `PR | `Review of string | `New_repo ] + type t = + [ `Issue + | `Issue_comment + | `PR + | `Review of string + | `Merge + | `New_repo ] let pp fs = function | `Issue -> Format.fprintf fs "`Issue" | `Issue_comment -> Format.fprintf fs "`Issue_comment" | `PR -> Format.fprintf fs "`PR" | `Review x -> Format.fprintf fs "`Review %S" x + | `Merge -> Format.fprintf fs "`Merge" | `New_repo -> Format.fprintf fs "`New_repo" let eq (x : t) (y : t) = @@ -22,6 +29,7 @@ module Testable = struct | `Issue, `Issue | `Issue_comment, `Issue_comment | `PR, `PR + | `Merge, `Merge | `New_repo, `New_repo -> true | `Review x, `Review y -> String.equal x y @@ -113,6 +121,14 @@ let request ~user = title body repository { nameWithOwner } + timelineItems(last:10, itemTypes:[MERGED_EVENT]) { + nodes { + ... on MergedEvent { + createdAt + actor { login } + } + } + } } } } @@ -236,6 +252,16 @@ let activity_example ~user = "body": "xxx", "repository": { "nameWithOwner": "ocaml-ppx/ocamlformat" + }, + "timelineItems": { + "nodes": [ + { + "createdAt": "2024-03-13T11:09:56Z", + "actor": { + "login": %S + } + } + ] } } }, @@ -247,6 +273,16 @@ let activity_example ~user = "body": "xxx", "repository": { "nameWithOwner": "realworldocaml/mdx" + }, + "timelineItems": { + "nodes": [ + { + "createdAt": "2024-04-02T20:22:37Z", + "actor": { + "login": "xxx" + } + } + ] } } } @@ -322,7 +358,7 @@ let activity_example ~user = } } |} - (User.response_field user) (user |> or_viewer) + (User.response_field user) (user |> or_viewer) (user |> or_viewer) let activity_example_json ~user = Yojson.Safe.from_string (activity_example ~user) @@ -425,6 +461,14 @@ let contributions_example2 ~user = title = "Represent the expr sequence as a list"; body = "xxx"; }; + { + repo = "ocaml-ppx/ocamlformat"; + kind = `Merge; + date = "2024-03-13T11:09:56Z"; + url = "https://github.com/ocaml-ppx/ocamlformat/pull/2533"; + title = "Represent the expr sequence as a list"; + body = ""; + }; ] ); ( "realworldocaml/mdx", [ From 8ecce5100dd41bcae8e319bf06803f61ae2cc8c9 Mon Sep 17 00:00:00 2001 From: "Guillaume \"Liam\" Petiot" Date: Wed, 3 Apr 2024 11:24:37 +0100 Subject: [PATCH 2/2] Update CHANGES.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index eb6a53e..f684e62 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,7 +11,7 @@ ### Added - Display curl requests and responses in debug mode (`-vv` or `--verbosity debug`) (#36, @gpetiot) -- Add the PR merge events to the contributions (#, @emillon, @gpetiot) +- Add the PR merge events to the contributions (#37, @emillon, @gpetiot) ## 1.0.1