Skip to content

Commit 08ea75c

Browse files
committed
store runtime repo state in a hash table indexed by repo url
1 parent 509567b commit 08ea75c

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

lib/action.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
9494
let rules = cfg.status_rules.rules in
9595
let action_on_match (branches : branch list) =
9696
let default = Option.to_list cfg.prefix_rules.default_channel in
97-
let () = Context.refresh_pipeline_status ~pipeline ~branches ~status:current_status ctx in
97+
State.set_repo_pipeline_status ctx.state repo.url ~pipeline ~branches ~status:current_status;
9898
match List.is_empty branches with
9999
| true -> Lwt.return []
100100
| false ->
@@ -113,11 +113,12 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
113113
)
114114
in
115115
if Context.is_pipeline_allowed ctx repo.url ~pipeline then begin
116+
let repo_state = State.find_or_add_repo ctx.state repo.url in
116117
match Rule.Status.match_rules ~rules n with
117118
| Some Ignore | None -> Lwt.return []
118119
| Some Allow -> action_on_match n.branches
119120
| Some Allow_once ->
120-
match Map.find ctx.state.pipeline_statuses pipeline with
121+
match Map.find repo_state.pipeline_statuses pipeline with
121122
| Some branch_statuses ->
122123
let has_same_status_state_as_prev (branch : branch) =
123124
match Map.find branch_statuses branch.name with

lib/context.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let default : t =
2222
state_filepath = None;
2323
secrets = None;
2424
config = Table.empty ();
25-
state = State.empty;
25+
state = State.empty ();
2626
}
2727

2828
let make ?config_filename ?secrets_filepath ?state_filepath () =
@@ -62,10 +62,6 @@ let is_pipeline_allowed ctx repo_url ~pipeline =
6262
| Some allowed_pipelines when not @@ List.exists allowed_pipelines ~f:(String.equal pipeline) -> false
6363
| _ -> true
6464

65-
let refresh_pipeline_status ctx ~pipeline ~(branches : Github_t.branch list) ~status =
66-
if is_pipeline_allowed ctx "" ~pipeline then State.refresh_pipeline_status ctx.state ~pipeline ~branches ~status
67-
else ()
68-
6965
let log = Log.from "context"
7066

7167
let refresh_secrets ctx =

lib/state.atd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type status_state <ocaml from="Github"> = abstract
22
type 'v map_as_object <ocaml from="Common"> = abstract
3+
type 'v table_as_object <ocaml from="Common"> = abstract
34

45
(* A map from branch names to build statuses *)
56
type branch_statuses = status_state map_as_object
@@ -9,7 +10,12 @@ type branch_statuses = status_state map_as_object
910
branch *)
1011
type pipeline_statuses = branch_statuses map_as_object
1112

13+
(* The runtime state of a given GitHub repository *)
14+
type repo_state = {
15+
pipeline_statuses <ocaml mutable>: pipeline_statuses
16+
}
17+
1218
(* The serializable runtime state of the bot *)
1319
type state = {
14-
pipeline_statuses <ocaml mutable>: pipeline_statuses
15-
}
20+
~repos <ocaml default="Common.Table.empty ()"> : repo_state table_as_object
21+
}

lib/state.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ open Base
22
open Common
33
open Devkit
44

5-
let empty : State_t.state = { pipeline_statuses = StringMap.empty }
5+
let empty_repo_state () : State_t.repo_state = { pipeline_statuses = StringMap.empty }
66

7-
let refresh_pipeline_status (state : State_t.state) ~pipeline ~(branches : Github_t.branch list) ~status =
8-
let update_pipeline_status branch_statuses =
7+
let empty () : State_t.state = { repos = Table.empty () }
8+
9+
let find_or_add_repo (state : State_t.state) repo_url =
10+
Hashtbl.find_or_add state.repos repo_url ~default:empty_repo_state
11+
12+
let set_repo_pipeline_status (state : State_t.state) repo_url ~pipeline ~(branches : Github_t.branch list) ~status =
13+
let set_branch_status branch_statuses =
914
let new_statuses = List.map branches ~f:(fun b -> b.name, status) in
1015
let init = Option.value branch_statuses ~default:(Map.empty (module String)) in
1116
List.fold_left new_statuses ~init ~f:(fun m (key, data) -> Map.set m ~key ~data)
1217
in
13-
state.pipeline_statuses <- Map.update state.pipeline_statuses pipeline ~f:update_pipeline_status
18+
let repo_state = find_or_add_repo state repo_url in
19+
repo_state.pipeline_statuses <- Map.update repo_state.pipeline_statuses pipeline ~f:set_branch_status
1420

1521
let log = Log.from "state"
1622

0 commit comments

Comments
 (0)