Skip to content

Commit

Permalink
Add the PR merge events to the contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Apr 3, 2024
1 parent 4756a4d commit 9818fd8
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 103 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#<PR_NUMBER>, @emillon, @gpetiot)

## 1.0.1

Expand Down
34 changes: 29 additions & 5 deletions lib/contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ let query user =
title
body
repository { nameWithOwner }
timelineItems(last:10, itemTypes:[MERGED_EVENT]) {
nodes {
... on MergedEvent {
createdAt
actor { login }
}
}
}
}
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/contributions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions lib/contributions_json_response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
202 changes: 107 additions & 95 deletions test/expect/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand Down Expand Up @@ -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).
Expand Down
Loading

0 comments on commit 9818fd8

Please sign in to comment.