Skip to content

Commit 3ef4159

Browse files
committed
make config a hash table mapping urls to config object
1 parent 0f95bfb commit 3ef4159

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

lib/action.ml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
8787
if List.is_empty matched_channel_names then default else matched_channel_names
8888

8989
let partition_status (ctx : Context.t) (n : status_notification) =
90-
let cfg = Context.get_config_exn ctx in
90+
let repo = n.repository in
91+
let cfg = Context.find_repo_config_exn ctx repo.url in
9192
let pipeline = n.context in
9293
let current_status = n.state in
9394
let rules = cfg.status_rules.rules in
@@ -111,7 +112,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
111112
| Ok commit -> Lwt.return @@ partition_commit cfg commit.files
112113
)
113114
in
114-
if Context.is_pipeline_allowed ctx ~pipeline then begin
115+
if Context.is_pipeline_allowed ctx repo.url ~pipeline then begin
115116
match Rule.Status.match_rules ~rules n with
116117
| Some Ignore | None -> Lwt.return []
117118
| Some Allow -> action_on_match n.branches
@@ -130,7 +131,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
130131
else Lwt.return []
131132

132133
let partition_commit_comment (ctx : Context.t) n =
133-
let cfg = Context.get_config_exn ctx in
134+
let cfg = Context.find_repo_config_exn ctx n.repository.url in
134135
match n.comment.commit_id with
135136
| None -> action_error "unable to find commit id for this commit comment event"
136137
| Some sha ->
@@ -149,7 +150,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
149150
)
150151

151152
let generate_notifications (ctx : Context.t) req =
152-
let cfg = Context.get_config_exn ctx in
153+
let repo = Github.repo_of_notification req in
154+
let cfg = Context.find_repo_config_exn ctx repo.url in
153155
match req with
154156
| Github.Push n ->
155157
partition_push cfg n |> List.map ~f:(fun (channel, n) -> generate_push_notification n channel) |> Lwt.return
@@ -178,20 +180,20 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
178180
in
179181
Lwt_list.iter_s notify notifications
180182

181-
(** `refresh_config_of_context ctx n` updates the current context if the configuration
182-
hasn't been loaded yet, or if the incoming request `n` is a push
183+
(** `refresh_repo_config ctx n` fetches the latest repo config if it's
184+
uninitialized, or if the incoming request `n` is a push
183185
notification containing commits that touched the config file. *)
184-
let refresh_config_of_context (ctx : Context.t) notification =
186+
let refresh_repo_config (ctx : Context.t) notification =
187+
let repo = Github.repo_of_notification notification in
185188
let fetch_config () =
186-
let repo = Github.repo_of_notification notification in
187189
match%lwt Github_api.get_config ~ctx ~repo with
188190
| Ok config ->
189-
ctx.config <- Some config;
190-
Context.print_config ctx;
191+
Context.set_repo_config ctx repo.url config;
192+
Context.print_config ctx repo.url;
191193
Lwt.return @@ Ok ()
192194
| Error e -> action_error e
193195
in
194-
match ctx.config with
196+
match Context.find_repo_config ctx repo.url with
195197
| None -> fetch_config ()
196198
| Some _ ->
197199
match notification with
@@ -208,7 +210,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
208210
match Github.parse_exn ~secret:secrets.gh_hook_token headers body with
209211
| exception exn -> Exn_lwt.fail ~exn "failed to parse payload"
210212
| payload ->
211-
( match%lwt refresh_config_of_context ctx payload with
213+
( match%lwt refresh_repo_config ctx payload with
212214
| Error e -> action_error e
213215
| Ok () ->
214216
let%lwt notifications = generate_notifications ctx payload in

lib/context.ml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ open Devkit
44

55
exception Context_error of string
66

7-
let context_error msg = raise (Context_error msg)
7+
let context_error fmt = Printf.ksprintf (fun msg -> raise (Context_error msg)) fmt
88

99
type t = {
1010
config_filename : string;
1111
secrets_filepath : string;
1212
state_filepath : string option;
1313
mutable secrets : Config_t.secrets option;
14-
mutable config : Config_t.config option;
14+
config : Config_t.config Table.t;
1515
state : State_t.state;
1616
}
1717

@@ -21,7 +21,7 @@ let default : t =
2121
secrets_filepath = "secrets.json";
2222
state_filepath = None;
2323
secrets = None;
24-
config = None;
24+
config = Table.empty ();
2525
state = State.empty;
2626
}
2727

@@ -35,30 +35,36 @@ let get_secrets_exn ctx =
3535
| None -> context_error "secrets is uninitialized"
3636
| Some secrets -> secrets
3737

38-
let get_config_exn ctx =
39-
match ctx.config with
40-
| None -> context_error "config is uninitialized"
38+
let find_repo_config ctx repo_url = Hashtbl.find ctx.config repo_url
39+
40+
let find_repo_config_exn ctx repo_url =
41+
match find_repo_config ctx repo_url with
42+
| None -> context_error "config uninitialized for repo %s" repo_url
4143
| Some config -> config
4244

45+
let set_repo_config ctx repo_url config = Hashtbl.set ctx.config ~key:repo_url ~data:config
46+
4347
let hook_of_channel ctx channel_name =
4448
let secrets = get_secrets_exn ctx in
4549
match List.find secrets.slack_hooks ~f:(fun webhook -> String.equal webhook.channel channel_name) with
4650
| Some hook -> Some hook.url
4751
| None -> None
4852

49-
(** `is_pipeline_allowed ctx p` returns `true` if ctx.config.status_rules
50-
doesn't define a whitelist of allowed pipelines, or if the list
53+
(** `is_pipeline_allowed s r p` returns `true` if
54+
`status_rules` doesn't define a whitelist of allowed
55+
pipelines in the config of repo `r`, or if the list
5156
contains pipeline `p`; returns `false` otherwise. *)
52-
let is_pipeline_allowed ctx ~pipeline =
53-
match ctx.config with
57+
let is_pipeline_allowed ctx repo_url ~pipeline =
58+
match find_repo_config ctx repo_url with
5459
| None -> false
5560
| Some config ->
5661
match config.status_rules.allowed_pipelines with
5762
| Some allowed_pipelines when not @@ List.exists allowed_pipelines ~f:(String.equal pipeline) -> false
5863
| _ -> true
5964

6065
let refresh_pipeline_status ctx ~pipeline ~(branches : Github_t.branch list) ~status =
61-
if is_pipeline_allowed ctx ~pipeline then State.refresh_pipeline_status ctx.state ~pipeline ~branches ~status else ()
66+
if is_pipeline_allowed ctx "" ~pipeline then State.refresh_pipeline_status ctx.state ~pipeline ~branches ~status
67+
else ()
6268

6369
let log = Log.from "context"
6470

@@ -90,8 +96,8 @@ let refresh_state ctx =
9096
end
9197
else Ok ctx
9298

93-
let print_config ctx =
94-
let cfg = get_config_exn ctx in
99+
let print_config ctx repo_url =
100+
let cfg = find_repo_config_exn ctx repo_url in
95101
let secrets = get_secrets_exn ctx in
96102
log#info "using prefix routing:";
97103
Rule.Prefix.print_prefix_routing cfg.prefix_rules.rules;

0 commit comments

Comments
 (0)