diff --git a/lib/gitlab.atd b/lib/gitlab.atd index 4ad5abc..3aa40a2 100644 --- a/lib/gitlab.atd +++ b/lib/gitlab.atd @@ -62,7 +62,7 @@ type user_short = { name: string; username: string; ?state: string nullable; - avatar_url: string nullable; + ?avatar_url: string nullable; ?web_url: string nullable; ?email: string nullable; } @@ -1024,7 +1024,7 @@ type feature_flag_webhook = { project: project_webhook; user: user_short; user_url: string; - object_attributes: feature_flag_attributes; + object_attributes: feature_flag_attributes; } (* https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#events *) @@ -1119,9 +1119,10 @@ type noteable_type = [ | Issue ] +type notes = note list + type note = { id: int; - note_type : string nullable; body: string; attachment: string nullable; author: user_short; @@ -1135,6 +1136,12 @@ type note = { noteable_iid: int; } +type create_note = { + body: string; + created_at: date_time option; + merge_request_diff_sha: string option; +} + type branch = { id: int; project_id: int; @@ -1228,9 +1235,9 @@ type project_access_token = { type project_access_tokens = project_access_token list -(** Types for creating new resources via API. +(** Types for creating new resources via API. This reuses the enum serialisation code. - *) + *) type new_token = { name : string; expires_at : string; @@ -1283,7 +1290,7 @@ type project_hook = { push_events_branch_filter: string nullable; } -type project_hooks = project_hook list +type project_hooks = project_hook list type create_project_hook = { ?id: int option; @@ -1304,4 +1311,4 @@ type create_project_hook = { ?repository_update_events: bool option; ?wiki_page_events: bool option; ?token: string option; -} \ No newline at end of file +} diff --git a/lib/gitlab_core.ml b/lib/gitlab_core.ml index 12f6d45..a98bdb1 100644 --- a/lib/gitlab_core.ml +++ b/lib/gitlab_core.ml @@ -228,6 +228,16 @@ struct (Printf.sprintf "%s/projects/%i/merge_requests/%s/changes" api id merge_request_iid) + let project_merge_request_notes ~project_id ~merge_request_iid = + Uri.of_string + (Printf.sprintf "%s/projects/%i/merge_requests/%s/notes" api project_id + merge_request_iid) + + let project_merge_request_note_id ~project_id ~merge_request_iid ~note_id = + Uri.of_string + (Printf.sprintf "%s/projects/%i/merge_requests/%s/notes/%i" api project_id + merge_request_iid note_id) + let project_events ~id = Uri.of_string (Printf.sprintf "%s/projects/%i/events" api id) @@ -1538,7 +1548,25 @@ struct let body = Gitlab_j.string_of_create_project_hook create_project_hook in API.post ~token ~uri ~body ~expected_code:`Created (fun s -> Lwt.return (Gitlab_j.project_hook_of_string s)) + end + module Notes = struct + module Merge_request = struct + let list ?token ~project_id ~merge_request_iid ?sort () = + let uri = URI.project_merge_request_notes ~project_id ~merge_request_iid |> sort_param sort in + API.get_stream ?token ~uri (fun body -> + return (Gitlab_j.notes_of_string body)) + + let by_id ?token ~project_id ~merge_request_iid ~note_id () = + let uri = URI.project_merge_request_note_id ~project_id ~merge_request_iid ~note_id in + API.get ?token ~uri (fun body -> return (Gitlab_j.note_of_string body)) + + let create ~token ~project_id ~merge_request_iid ~create_note () = + let uri = URI.project_merge_request_notes ~project_id ~merge_request_iid in + let body = Gitlab_j.string_of_create_note create_note in + API.post ~token ~uri ~body ~expected_code:`Created (fun s -> + Lwt.return (Gitlab_j.note_of_string s)) + end end end diff --git a/lib/gitlab_s.mli b/lib/gitlab_s.mli index a8f15b8..f965510 100644 --- a/lib/gitlab_s.mli +++ b/lib/gitlab_s.mli @@ -145,7 +145,7 @@ module type Gitlab = sig scopes:Gitlab_t.scope list -> unit -> Uri.t - (** Create URL for Authorisation code flow. {{:https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-flow}} + (** Create URL for Authorisation code flow. {{:https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-flow}} *) val of_code : @@ -976,6 +976,7 @@ module type Gitlab = sig See {{:https://docs.gitlab.com/ee/api/issues.html#new-issue}New issue}. *) end + module Hook: sig val list : ?token:Token.t -> project_id:int -> unit -> Gitlab_t.project_hooks Response.t Monad.t @@ -1002,7 +1003,58 @@ module type Gitlab = sig (** Creates a new webhook. See {{:https://docs.gitlab.com/ee/api/projects.html#add-project-hook}Add project hook}. *) - + end + + (** The [Notes] module provides access to + {{:https://docs.gitlab.com/ee/api/notes.html#merge-requests}Notes + API}. + *) + module Notes : sig + + (** The [Merge_request] module provides access to + {{:https://docs.gitlab.com/ee/api/notes.html#merge-requests}Merge + requests notes API}. + *) + module Merge_request : sig + val list : + ?token:Token.t -> + project_id:int -> + merge_request_iid:string -> + ?sort:Gitlab_t.sort -> + unit -> + Gitlab_t.note Stream.t + (** [list ?token ~project_id ~merge_request_iid] + Request a list of a merge request notes. See + {{:https://docs.gitlab.com/ee/api/notes.html#list-all-merge-request-notes}List + all merge request notes}. + *) + + val by_id : + ?token:Token.t -> + project_id:int -> + merge_request_iid:string -> + note_id:int -> + unit -> + Gitlab_t.note Response.t Monad.t + (** [by_id ?token ~project_id ~merge_request_iid ~note_id] + Get a single note for a given merge request. See + {{:https://docs.gitlab.com/ee/api/notes.html#get-single-merge-request-note}Get + single merge request note}. + *) + + val create : + token:Token.t -> + project_id:int -> + merge_request_iid:string -> + create_note:Gitlab_t.create_note -> + unit -> + Gitlab_t.note Response.t Monad.t + (** [create ?token ~project_id ~merge_request_iid ~body] + Creates a new note. See + {{:https://docs.gitlab.com/ee/api/notes.html#create-new-merge-request-note}Create + new merge request note}. + *) + end end end diff --git a/test/cases/notes/event.json b/test/cases/notes/event.json new file mode 100644 index 0000000..edc4525 --- /dev/null +++ b/test/cases/notes/event.json @@ -0,0 +1,44 @@ +[ + { + "id": 302, + "body": "closed", + "attachment": null, + "author": { + "id": 1, + "username": "pipin", + "email": "admin@example.com", + "name": "Pip", + "state": "active", + "created_at": "2013-09-30T13:46:01Z" + }, + "created_at": "2013-10-02T09:22:45Z", + "updated_at": "2013-10-02T10:22:45Z", + "system": true, + "noteable_id": 377, + "noteable_type": "Issue", + "noteable_iid": 377, + "resolvable": false, + "confidential": false + }, + { + "id": 305, + "body": "Text of the comment\r\n", + "attachment": null, + "author": { + "id": 1, + "username": "pipin", + "email": "admin@example.com", + "name": "Pip", + "state": "active", + "created_at": "2013-09-30T13:46:01Z" + }, + "created_at": "2013-10-02T09:56:03Z", + "updated_at": "2013-10-02T09:56:03Z", + "system": true, + "noteable_id": 121, + "noteable_type": "Issue", + "noteable_iid": 121, + "resolvable": false, + "confidential": true + } +] diff --git a/test/cases/notes/expected.json b/test/cases/notes/expected.json new file mode 100644 index 0000000..edc4525 --- /dev/null +++ b/test/cases/notes/expected.json @@ -0,0 +1,44 @@ +[ + { + "id": 302, + "body": "closed", + "attachment": null, + "author": { + "id": 1, + "username": "pipin", + "email": "admin@example.com", + "name": "Pip", + "state": "active", + "created_at": "2013-09-30T13:46:01Z" + }, + "created_at": "2013-10-02T09:22:45Z", + "updated_at": "2013-10-02T10:22:45Z", + "system": true, + "noteable_id": 377, + "noteable_type": "Issue", + "noteable_iid": 377, + "resolvable": false, + "confidential": false + }, + { + "id": 305, + "body": "Text of the comment\r\n", + "attachment": null, + "author": { + "id": 1, + "username": "pipin", + "email": "admin@example.com", + "name": "Pip", + "state": "active", + "created_at": "2013-09-30T13:46:01Z" + }, + "created_at": "2013-10-02T09:56:03Z", + "updated_at": "2013-10-02T09:56:03Z", + "system": true, + "noteable_id": 121, + "noteable_type": "Issue", + "noteable_iid": 121, + "resolvable": false, + "confidential": true + } +] diff --git a/test/test.ml b/test/test.ml index d96b61e..1b35f62 100644 --- a/test/test.ml +++ b/test/test.ml @@ -133,6 +133,16 @@ module Gitlab_j_merge_requests : TestableJson = struct let to_json v = Yojson.Basic.from_string (Gitlab_j.string_of_merge_requests v) end +module Gitlab_j_notes : TestableJson = struct + type t = Gitlab_j.notes + + let name = "notes" + + let of_string = Gitlab_j.notes_of_string + + let to_json v = Yojson.Basic.from_string (Gitlab_j.string_of_notes v) +end + module Gitlab_j_commit_statuses : TestableJson = struct type t = Gitlab_j.commit_statuses @@ -204,6 +214,7 @@ module PS = Make (Gitlab_j_project_short) module PH = Make (Gitlab_j_project_hook) module WH = Make (Gitlab_j_webhooks) module MR = Make (Gitlab_j_merge_requests) +module N = Make (Gitlab_j_notes) module CS = Make (Gitlab_j_commit_statuses) module BF = Make (Gitlab_j_branches_full) module M = Make (Gitlab_j_milestones) @@ -222,6 +233,7 @@ let () = ("events", E.test ()); ("issues", I.test ()); ("merge_requests", MR.test ()); + ("notes", N.test ()); ("milestones", M.test ()); ("project_short", PS.test ()); ("projects", P.test ());