@@ -3,6 +3,10 @@ open Devkit
3
3
open Printf
4
4
open Github_j
5
5
6
+ exception Remote_config_error of string
7
+
8
+ let remote_config_error fmt = ksprintf (fun s -> raise (Remote_config_error s)) fmt
9
+
6
10
let log = Log. from " github"
7
11
8
12
type t =
@@ -14,14 +18,9 @@ type t =
14
18
| Issue_comment of issue_comment_notification
15
19
| Commit_comment of commit_comment_notification
16
20
| Status of status_notification
21
+ (* all other events *)
17
22
| Event of string
18
23
19
- (* all other events *)
20
-
21
- exception Remote_config_error of string
22
-
23
- let remote_config_error fmt = ksprintf (fun s -> raise (Remote_config_error s)) fmt
24
-
25
24
let to_repo = function
26
25
| Push n -> Some n.repository
27
26
| Pull_request n -> Some n.repository
@@ -33,6 +32,16 @@ let to_repo = function
33
32
| Status n -> Some n.repository
34
33
| Event _ -> None
35
34
35
+ let commits_branch_of_ref ref =
36
+ match String. split ~on: '/' ref with
37
+ | "refs" :: "heads" :: l -> String. concat ~sep: " /" l
38
+ | _ -> ref
39
+
40
+ let event_of_filename filename =
41
+ match String. split_on_chars filename ~on: [ '.' ] with
42
+ | [ kind; _name; ext ] when String. equal ext " json" -> Some kind
43
+ | _ -> None
44
+
36
45
let api_url_of_repo (repo : repository ) =
37
46
Option. map ~f: (fun host ->
38
47
match host with
@@ -74,23 +83,12 @@ let get_remote_config_json_url filename ?token req =
74
83
end
75
84
76
85
let config_of_content_api_response response =
77
- let decode_string_pad s =
78
- let rec strip_padding i =
79
- if i < 0 then " "
80
- else (
81
- match s.[i] with
82
- | '=' | '\n' | '\r' | '\t' | ' ' -> strip_padding (i - 1 )
83
- | _ -> String. sub s ~pos: 0 ~len: (i + 1 )
84
- )
85
- in
86
- Base64. decode_string @@ strip_padding (String. length s - 1 )
87
- in
88
86
try % lwt
89
87
match response.encoding with
90
88
| "base64" ->
91
89
Lwt. return
92
90
@@ Config_j. config_of_string
93
- @@ decode_string_pad
91
+ @@ Common. decode_string_pad
94
92
@@ String. concat
95
93
@@ String. split_lines
96
94
@@ response.content
@@ -99,11 +97,23 @@ let config_of_content_api_response response =
99
97
| Base64. Invalid_char -> remote_config_error " unable to decode configuration file from base64"
100
98
| Yojson. Json_error msg -> remote_config_error " unable to parse configuration file as valid JSON (%s)" msg
101
99
102
- let load_config_json url =
103
- let headers = [ " Accept: application/vnd.github.v3+json" ] in
104
- match % lwt Web. http_request_lwt ~headers `GET url with
105
- | `Error e -> remote_config_error " error while querying github api %s: %s" url e
106
- | `Ok s -> config_of_content_api_response @@ Github_j. content_api_response_of_string s
100
+ let is_main_merge_message ~msg :message ~branch (cfg : Config.t ) =
101
+ match cfg.main_branch_name with
102
+ | Some main_branch when String. equal branch main_branch ->
103
+ (*
104
+ handle "Merge <main branch> into <feature branch>" commits when they are merged into main branch
105
+ we should have already seen these commits on the feature branch but for some reason they are distinct:true
106
+ *)
107
+ let prefix = sprintf " Merge branch '%s' into " main_branch in
108
+ let prefix2 = sprintf " Merge remote-tracking branch 'origin/%s' into " main_branch in
109
+ let title = Common. first_line message in
110
+ String. is_prefix title ~prefix || String. is_prefix title ~prefix: prefix2
111
+ | Some main_branch ->
112
+ let expect = sprintf " Merge branch '%s' into %s" main_branch branch in
113
+ let expect2 = sprintf " Merge remote-tracking branch 'origin/%s' into %s" main_branch branch in
114
+ let title = Common. first_line message in
115
+ String. equal title expect || String. equal title expect2
116
+ | _ -> false
107
117
108
118
let is_valid_signature ~secret headers_sig body =
109
119
let request_hash =
@@ -113,33 +123,11 @@ let is_valid_signature ~secret headers_sig body =
113
123
let (`Hex request_hash) = Hex. of_string request_hash in
114
124
String. equal headers_sig (sprintf " sha1=%s" request_hash)
115
125
116
- (* Parse a payload. The type of the payload is detected from the headers. *)
117
- let parse_exn ~secret headers body =
118
- begin
119
- match secret with
120
- | None -> ()
121
- | Some secret ->
122
- match List.Assoc. find headers " x-hub-signature" ~equal: String. equal with
123
- | None -> Exn. fail " unable to find header x-hub-signature"
124
- | Some req_sig -> if not @@ is_valid_signature ~secret req_sig body then failwith " request signature invalid"
125
- end ;
126
- match List.Assoc. find_exn headers " x-github-event" ~equal: String. equal with
127
- | exception exn -> Exn. fail ~exn " unable to read x-github-event"
128
- | "push" -> Push (commit_pushed_notification_of_string body)
129
- | "pull_request" -> Pull_request (pr_notification_of_string body)
130
- | "pull_request_review" -> PR_review (pr_review_notification_of_string body)
131
- | "pull_request_review_comment" -> PR_review_comment (pr_review_comment_notification_of_string body)
132
- | "issues" -> Issue (issue_notification_of_string body)
133
- | "issue_comment" -> Issue_comment (issue_comment_notification_of_string body)
134
- | "status" -> Status (status_notification_of_string body)
135
- | "commit_comment" -> Commit_comment (commit_comment_notification_of_string body)
136
- | ("member" | "create" | "delete" | "release" ) as event -> Event event
137
- | event -> failwith @@ sprintf " unsupported event : %s" event
138
-
139
- let get_commits_branch ref =
140
- match String. split ~on: '/' ref with
141
- | "refs" :: "heads" :: l -> String. concat ~sep: " /" l
142
- | _ -> ref
126
+ let load_config_json url =
127
+ let headers = [ " Accept: application/vnd.github.v3+json" ] in
128
+ match % lwt Web. http_request_lwt ~headers `GET url with
129
+ | `Error e -> remote_config_error " error while querying github api %s: %s" url e
130
+ | `Ok s -> config_of_content_api_response @@ Github_j. content_api_response_of_string s
143
131
144
132
let query_api ?token ~url parse =
145
133
let headers = Option. map token ~f: (fun t -> [ sprintf " Authorization: token %s" t ]) in
@@ -187,3 +175,26 @@ let generate_commit_from_commit_comment cfg n =
187
175
let commit_url = String. sub ~pos: 0 ~len: url_length url ^ " /" ^ sha in
188
176
(* add sha hash to get the full api link *)
189
177
generate_query_commit cfg ~url: commit_url ~sha
178
+
179
+ (* Parse a payload. The type of the payload is detected from the headers. *)
180
+ let parse_exn ~secret headers body =
181
+ begin
182
+ match secret with
183
+ | None -> ()
184
+ | Some secret ->
185
+ match List.Assoc. find headers " x-hub-signature" ~equal: String. equal with
186
+ | None -> Exn. fail " unable to find header x-hub-signature"
187
+ | Some req_sig -> if not @@ is_valid_signature ~secret req_sig body then failwith " request signature invalid"
188
+ end ;
189
+ match List.Assoc. find_exn headers " x-github-event" ~equal: String. equal with
190
+ | exception exn -> Exn. fail ~exn " unable to read x-github-event"
191
+ | "push" -> Push (commit_pushed_notification_of_string body)
192
+ | "pull_request" -> Pull_request (pr_notification_of_string body)
193
+ | "pull_request_review" -> PR_review (pr_review_notification_of_string body)
194
+ | "pull_request_review_comment" -> PR_review_comment (pr_review_comment_notification_of_string body)
195
+ | "issues" -> Issue (issue_notification_of_string body)
196
+ | "issue_comment" -> Issue_comment (issue_comment_notification_of_string body)
197
+ | "status" -> Status (status_notification_of_string body)
198
+ | "commit_comment" -> Commit_comment (commit_comment_notification_of_string body)
199
+ | ("member" | "create" | "delete" | "release" ) as event -> Event event
200
+ | event -> failwith @@ sprintf " unsupported event : %s" event
0 commit comments