@@ -2,7 +2,6 @@ open Devkit
2
2
open Base
3
3
open Slack
4
4
open Config_t
5
- open Config
6
5
open Common
7
6
open Github_j
8
7
@@ -13,24 +12,14 @@ let action_error msg = raise (Action_error msg)
13
12
let log = Log. from " action"
14
13
15
14
module Action (Github_api : Api.Github ) (Slack_api : Api.Slack ) = struct
16
- (* this should move to context.ml once rule.ml is refactored to not depend on it *)
17
- let print_config ctx =
18
- let cfg = Context. get_config_exn ctx in
19
- let secrets = Context. get_secrets_exn ctx in
20
- log#info " using prefix routing:" ;
21
- Rule.Prefix. print_prefix_routing cfg.prefix_rules.rules;
22
- log#info " using label routing:" ;
23
- Rule.Label. print_label_routing cfg.label_rules.rules;
24
- log#info " signature checking %s" (if Option. is_some secrets.gh_hook_token then " enabled" else " disabled" )
25
-
26
- let partition_push cfg n =
15
+ let partition_push (cfg : Config_t.config ) n =
27
16
let default = Option. to_list cfg.prefix_rules.default_channel in
28
17
let rules = cfg.prefix_rules.rules in
29
18
n.commits
30
19
|> List. filter ~f: (fun c -> c.distinct)
31
20
|> List. filter ~f: (fun c ->
32
21
let branch = Github. commits_branch_of_ref n.ref in
33
- let skip = Github. is_main_merge_message ~msg: c.message ~branch cfg in
22
+ let skip = Github. is_main_merge_message ~msg: c.message ?main_branch:cfg.main_branch_name ~branch in
34
23
if skip then log#info " main branch merge, ignoring %s: %s" c.id (first_line c.message);
35
24
not skip)
36
25
|> List. concat_map ~f: (fun commit ->
@@ -45,7 +34,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
45
34
|> Map. map ~f: (fun commits -> { n with commits })
46
35
|> Map. to_alist
47
36
48
- let partition_label (cfg : Config.t ) (labels : label list ) =
37
+ let partition_label (cfg : Config_t.config ) (labels : label list ) =
49
38
let default = cfg.label_rules.default_channel in
50
39
let rules = cfg.label_rules.rules in
51
40
labels |> List. concat_map ~f: (Rule.Label. match_rules ~rules ) |> List. dedup_and_sort ~compare: String. compare
@@ -87,7 +76,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
87
76
| Submitted , _ , _ -> partition_label cfg n.pull_request.labels
88
77
| _ -> []
89
78
90
- let partition_commit (cfg : Config.t ) files =
79
+ let partition_commit (cfg : Config_t.config ) files =
91
80
let default = Option. to_list cfg.prefix_rules.default_channel in
92
81
let rules = cfg.prefix_rules.rules in
93
82
let matched_channel_names =
@@ -101,18 +90,18 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
101
90
let cfg = Context. get_config_exn ctx in
102
91
let pipeline = n.context in
103
92
let current_status = n.state in
104
- let updated_at = n.updated_at in
105
- let get_commit_info ( ) =
93
+ let rules = cfg.status_rules.rules in
94
+ let action_on_match ( branches : branch list ) =
106
95
let default = Option. to_list cfg.prefix_rules.default_channel in
107
- let () = Context. refresh_pipeline_status ~pipeline ~branches: n.branches ~status: current_status ~updated_at ctx in
108
- match List. is_empty n. branches with
96
+ let () = Context. refresh_pipeline_status ~pipeline ~branches ~status: current_status ctx in
97
+ match List. is_empty branches with
109
98
| true -> Lwt. return []
110
99
| false ->
111
100
match cfg.main_branch_name with
112
101
| None -> Lwt. return default
113
102
| Some main_branch_name ->
114
103
(* non-main branch build notifications go to default channel to reduce spam in topic channels *)
115
- match List. exists n. branches ~f: (fun { name } -> String. equal name main_branch_name) with
104
+ match List. exists branches ~f: (fun { name } -> String. equal name main_branch_name) with
116
105
| false -> Lwt. return default
117
106
| true ->
118
107
let sha = n.commit.sha in
@@ -122,27 +111,23 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
122
111
| Ok commit -> Lwt. return @@ partition_commit cfg commit.files
123
112
)
124
113
in
125
- let res =
126
- match
127
- List. exists cfg.status_rules.status ~f: (fun x ->
128
- match x with
129
- | State s -> Poly. equal s n.state
130
- | HideConsecutiveSuccess -> Poly. equal Success n.state
131
- | _ -> false )
132
- with
133
- | false -> Lwt. return []
134
- | true ->
135
- match List. exists ~f: id [ Rule.Status. hide_cancelled n cfg; Rule.Status. hide_success n ctx ] with
136
- | true -> Lwt. return []
137
- | false ->
138
- match cfg.status_rules.title with
139
- | None -> get_commit_info ()
140
- | Some status_filter ->
141
- match List. exists status_filter ~f: (String. equal n.context) with
142
- | false -> Lwt. return []
143
- | true -> get_commit_info ()
144
- in
145
- res
114
+ if Context. is_pipeline_allowed ctx ~pipeline then begin
115
+ match Rule.Status. match_rules ~rules n with
116
+ | Some Ignore | None -> Lwt. return []
117
+ | Some Allow -> action_on_match n.branches
118
+ | Some Allow_once ->
119
+ match Map. find ctx.state.pipeline_statuses pipeline with
120
+ | Some branch_statuses ->
121
+ let has_same_status_state_as_prev (branch : branch ) =
122
+ match Map. find branch_statuses branch.name with
123
+ | None -> false
124
+ | Some state -> Poly. equal state current_status
125
+ in
126
+ let branches = List. filter n.branches ~f: (Fn. non @@ has_same_status_state_as_prev) in
127
+ action_on_match branches
128
+ | None -> action_on_match n.branches
129
+ end
130
+ else Lwt. return []
146
131
147
132
let partition_commit_comment (ctx : Context.t ) n =
148
133
let cfg = Context. get_config_exn ctx in
@@ -213,9 +198,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
213
198
let repo = Github. repo_of_notification notification in
214
199
match % lwt Github_api. get_config ~ctx ~repo with
215
200
| Ok config ->
216
- print_config ctx;
217
- (* can remove this wrapper once status_rules doesn't depend on Config.t *)
218
- ctx.config < - Some (Config. make config);
201
+ Context. print_config ctx;
202
+ ctx.config < - Some config;
219
203
Lwt. return @@ Ok ()
220
204
| Error e -> action_error e
221
205
in
@@ -233,7 +217,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
233
217
let process_github_notification (ctx : Context.t ) headers body =
234
218
try % lwt
235
219
let secrets = Context. get_secrets_exn ctx in
236
- match Github. parse_exn ~secret : secrets.gh_hook_token headers body with
220
+ match Github. parse_exn ?hook_token :secrets.gh_hook_token headers body with
237
221
| exception exn -> Exn_lwt. fail ~exn " failed to parse payload"
238
222
| payload ->
239
223
( match % lwt refresh_config_of_context ctx payload with
@@ -243,7 +227,11 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
243
227
let % lwt () = send_notifications ctx notifications in
244
228
( match ctx.state_filepath with
245
229
| None -> Lwt. return_unit
246
- | Some path -> Lwt. return @@ State. save path ctx.state
230
+ | Some path ->
231
+ ( match % lwt State. save ctx.state path with
232
+ | Ok () -> Lwt. return_unit
233
+ | Error e -> action_error e
234
+ )
247
235
)
248
236
)
249
237
with
0 commit comments