Skip to content

Commit f627700

Browse files
committed
state: store runtime repo state in a hash table indexed by repo url
1 parent 6f62e45 commit f627700

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

lib/action.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
100100
let rules = cfg.status_rules.rules in
101101
let action_on_match (branches : branch list) =
102102
let default = Option.to_list cfg.prefix_rules.default_channel in
103-
let () = Context.refresh_pipeline_status ctx repo.url ~pipeline ~branches ~status:current_status in
103+
State.set_repo_pipeline_status ctx.state repo.url ~pipeline ~branches ~status:current_status;
104104
match List.is_empty branches with
105105
| true -> Lwt.return []
106106
| false ->
@@ -118,11 +118,12 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
118118
)
119119
in
120120
if Context.is_pipeline_allowed ctx repo.url ~pipeline then begin
121+
let repo_state = State.find_or_add_repo ctx.state repo.url in
121122
match Rule.Status.match_rules ~rules n with
122123
| Some Ignore | None -> Lwt.return []
123124
| Some Allow -> action_on_match n.branches
124125
| Some Allow_once ->
125-
match Map.find ctx.state.pipeline_statuses pipeline with
126+
match Map.find repo_state.pipeline_statuses pipeline with
126127
| Some branch_statuses ->
127128
let has_same_status_state_as_prev (branch : branch) =
128129
match Map.find branch_statuses branch.name with

lib/context.ml

Lines changed: 1 addition & 4 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 = Stringtbl.empty ();
25-
state = State.empty;
25+
state = State.empty ();
2626
}
2727

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

64-
let refresh_pipeline_status ctx repo_url ~pipeline ~(branches : Github_t.branch list) ~status =
65-
if is_pipeline_allowed ctx repo_url ~pipeline then State.refresh_pipeline_status ctx.state ~pipeline ~branches ~status
66-
6764
let log = Log.from "context"
6865

6966
let refresh_secrets ctx =

lib/state.atd

Lines changed: 7 additions & 1 deletion
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,8 +10,13 @@ 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;
20+
repos : repo_state table_as_object;
1521
?bot_user_id <ocaml mutable>: string nullable;
1622
}

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; bot_user_id = None }
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 = Stringtbl.empty (); bot_user_id = None }
8+
9+
let find_or_add_repo (state : State_t.state) repo_url =
10+
Stringtbl.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)