Skip to content

Commit

Permalink
Support listing and creating Notes in merge requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA authored and tmcgilchrist committed Jun 1, 2022
1 parent a5f52e2 commit e64e257
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 9 deletions.
21 changes: 14 additions & 7 deletions lib/gitlab.atd
Original file line number Diff line number Diff line change
Expand Up @@ -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;
} <ocaml field_prefix="user_short_">
Expand Down Expand Up @@ -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 *)
Expand Down Expand Up @@ -1119,9 +1119,10 @@ type noteable_type = [
| Issue <json name="Issue">
]

type notes = note list

type note = {
id: int;
note_type <json name="type"> : string nullable;
body: string;
attachment: string nullable;
author: user_short;
Expand All @@ -1135,6 +1136,12 @@ type note = {
noteable_iid: int;
} <ocaml field_prefix="note_">

type create_note = {
body: string;
created_at: date_time option;
merge_request_diff_sha: string option;
} <ocaml field_prefix="create_note_">

type branch = {
id: int;
project_id: int;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -1304,4 +1311,4 @@ type create_project_hook = {
?repository_update_events: bool option;
?wiki_page_events: bool option;
?token: string option;
}
}
28 changes: 28 additions & 0 deletions lib/gitlab_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
56 changes: 54 additions & 2 deletions lib/gitlab_s.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
44 changes: 44 additions & 0 deletions test/cases/notes/event.json
Original file line number Diff line number Diff line change
@@ -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
}
]
44 changes: 44 additions & 0 deletions test/cases/notes/expected.json
Original file line number Diff line number Diff line change
@@ -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
}
]
12 changes: 12 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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 ());
Expand Down

0 comments on commit e64e257

Please sign in to comment.