@@ -2,65 +2,46 @@ open Devkit
2
2
open Base
3
3
open Slack
4
4
open Config_t
5
- open Rule_t
6
5
open Config
7
6
open Common
8
7
open Github_j
9
8
9
+ exception Action_error of string
10
+
11
+ let action_error msg = raise (Action_error msg)
12
+
10
13
let log = Log. from " action"
11
14
15
+ module Github_api = Api_remote. Github
16
+
12
17
module Action = struct
13
18
let partition_push cfg n =
14
- let group_commit chan l =
15
- List. filter_map l ~f: (fun (chan' , commit ) ->
16
- match String. equal chan chan' with
17
- | false -> None
18
- | true -> Some commit)
19
- in
20
- let default commit =
21
- Option. value_map cfg.prefix_rules.default_channel ~default: [] ~f: (fun webhook -> [ webhook, commit ])
22
- in
19
+ let default = Option. to_list cfg.prefix_rules.default_channel in
23
20
let rules = cfg.prefix_rules.rules in
24
- let channels =
25
- n.commits
26
- |> List. filter ~f: (fun c -> c.distinct)
27
- |> List. filter ~f: (fun c ->
28
- let branch = Github. commits_branch_of_ref n.ref in
29
- let skip = Github. is_main_merge_message ~msg: c.message ~branch cfg in
30
- if skip then log#info " main branch merge, ignoring %s: %s" c.id (first_line c.message);
31
- not skip)
32
- |> List. map ~f: (fun commit ->
33
- match Rule.Prefix. filter_push rules commit with
34
- | [] -> default commit
35
- | l -> l)
36
- |> List. concat
37
- in
38
- let prefix_chans =
39
- let chans =
40
- Option. to_list cfg.prefix_rules.default_channel
41
- @ List. map rules ~f: (fun (rule : prefix_rule ) -> rule.channel_name)
42
- in
43
- List. dedup_and_sort chans ~compare: String. compare
44
- in
45
- List. filter_map prefix_chans ~f: (fun chan ->
46
- match group_commit chan channels with
47
- | [] -> None
48
- | l -> Some (chan, { n with commits = l }))
49
-
50
- let partition_label cfg (labels : Github_j.label list ) =
51
- let default = Option. to_list cfg.label_rules.default_channel in
52
- match labels with
53
- | [] -> default
54
- | labels ->
55
- let rules = cfg.label_rules.rules in
56
- let channels =
57
- labels
58
- |> List. map ~f: (fun (label : Github_j.label ) ->
59
- match Rule.Label. filter_label rules label with
60
- | [] -> default
61
- | l -> l)
62
- in
63
- List. dedup_and_sort ~compare: String. compare (List. concat channels)
21
+ n.commits
22
+ |> List. filter ~f: (fun c -> c.distinct)
23
+ |> List. filter ~f: (fun c ->
24
+ let branch = Github. commits_branch_of_ref n.ref in
25
+ let skip = Github. is_main_merge_message ~msg: c.message ~branch cfg in
26
+ if skip then log#info " main branch merge, ignoring %s: %s" c.id (first_line c.message);
27
+ not skip)
28
+ |> List. concat_map ~f: (fun commit ->
29
+ let matched_channel_names =
30
+ Github. modified_files_of_commit commit
31
+ |> List. filter_map ~f: (Rule.Prefix. match_rules ~rules )
32
+ |> List. dedup_and_sort ~compare: String. compare
33
+ in
34
+ let channel_names = if List. is_empty matched_channel_names then default else matched_channel_names in
35
+ List. map channel_names ~f: (fun n -> n, commit))
36
+ |> Map. of_alist_multi (module String )
37
+ |> Map. map ~f: (fun commits -> { n with commits })
38
+ |> Map. to_alist
39
+
40
+ let partition_label (cfg : Config.t ) (labels : label list ) =
41
+ let default = cfg.label_rules.default_channel in
42
+ let rules = cfg.label_rules.rules in
43
+ labels |> List. concat_map ~f: (Rule.Label. match_rules ~rules ) |> List. dedup_and_sort ~compare: String. compare
44
+ |> fun channel_names -> if List. is_empty channel_names then Option. to_list default else channel_names
64
45
65
46
let partition_pr cfg (n : pr_notification ) =
66
47
match n.action with
@@ -98,11 +79,15 @@ module Action = struct
98
79
| Submitted , _ , _ -> partition_label cfg n.pull_request.labels
99
80
| _ -> []
100
81
101
- let partition_commit cfg files =
102
- let names = List. map ~f: (fun f -> f.filename) files in
103
- match Rule.Prefix. unique_chans_of_files cfg.prefix_rules.rules names with
104
- | _ :: _ as xs -> xs
105
- | [] -> Option. to_list cfg.prefix_rules.default_channel
82
+ let partition_commit (cfg : Config.t ) files =
83
+ let default = Option. to_list cfg.prefix_rules.default_channel in
84
+ let rules = cfg.prefix_rules.rules in
85
+ let matched_channel_names =
86
+ List. map ~f: (fun f -> f.filename) files
87
+ |> List. filter_map ~f: (Rule.Prefix. match_rules ~rules )
88
+ |> List. dedup_and_sort ~compare: String. compare
89
+ in
90
+ if List. is_empty matched_channel_names then default else matched_channel_names
106
91
107
92
let partition_status (ctx : Context.t ) (n : status_notification ) =
108
93
let cfg = ctx.cfg in
@@ -153,18 +138,24 @@ module Action = struct
153
138
Context. update_state ctx (Github. Status n);
154
139
res
155
140
156
- let partition_commit_comment cfg n =
157
- let default = Option. to_list cfg.prefix_rules.default_channel in
158
- match n.comment.path with
159
- | None ->
160
- ( match % lwt Github. generate_commit_from_commit_comment cfg n with
161
- | None -> Lwt. return default
162
- | Some commit -> Lwt. return (partition_commit cfg commit.files)
141
+ let partition_commit_comment (ctx : Context.t ) n =
142
+ let cfg = ctx.cfg in
143
+ match n.comment.commit_id with
144
+ | None -> action_error " unable to find commit id for this commit comment event"
145
+ | Some sha ->
146
+ ( match % lwt Github_api. get_api_commit ~ctx ~repo: n.repository ~sha with
147
+ | Error e -> action_error e
148
+ | Ok commit ->
149
+ let default = Option. to_list cfg.prefix_rules.default_channel in
150
+ let rules = cfg.prefix_rules.rules in
151
+ ( match n.comment.path with
152
+ | None -> Lwt. return @@ (partition_commit cfg commit.files, commit)
153
+ | Some filename ->
154
+ match Rule.Prefix. match_rules filename ~rules with
155
+ | None -> Lwt. return (default, commit)
156
+ | Some chan -> Lwt. return ([ chan ], commit)
157
+ )
163
158
)
164
- | Some p ->
165
- match Rule.Prefix. chan_of_file cfg.prefix_rules.rules p with
166
- | None -> Lwt. return default
167
- | Some chan -> Lwt. return [ chan ]
168
159
169
160
let generate_notifications (ctx : Context.t ) req =
170
161
let cfg = ctx.cfg in
@@ -186,8 +177,8 @@ module Action = struct
186
177
|> List. map ~f: (fun webhook -> webhook, generate_issue_comment_notification n)
187
178
|> Lwt. return
188
179
| Commit_comment n ->
189
- let % lwt webhooks = partition_commit_comment cfg n in
190
- let % lwt notif = generate_commit_comment_notification cfg n in
180
+ let % lwt webhooks, api_commit = partition_commit_comment ctx n in
181
+ let % lwt notif = generate_commit_comment_notification api_commit n in
191
182
let notifs = List. map ~f: (fun webhook -> webhook, notif) webhooks in
192
183
Lwt. return notifs
193
184
| Status n ->
0 commit comments