Skip to content

Commit 987f50d

Browse files
committed
don't overwrite access token in secrets file; use state instead
If access token is provided in secrets file at startup time, should copy into runtime state.
1 parent b9bd9d4 commit 987f50d

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

lib/action.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
284284
let process_slack_oauth (ctx : Context.t) args =
285285
try%lwt
286286
let secrets = Context.get_secrets_exn ctx in
287-
match secrets.slack_access_token with
287+
match ctx.state.slack_access_token with
288288
| Some _ -> Lwt.return "ok"
289289
| None ->
290290
match Slack.validate_state ?oauth_state:secrets.slack_oauth_state ~args with
@@ -293,9 +293,18 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
293293
match List.Assoc.find args "code" ~equal:String.equal with
294294
| None -> action_error "argument `code` not found in slack authorization request"
295295
| Some code ->
296-
( match%lwt Slack_api.update_access_token_of_context ~ctx ~code with
296+
( match%lwt Slack_api.access_token_of_code ~ctx ~code with
297297
| Error e -> action_error e
298-
| Ok () -> Lwt.return "ok"
298+
| Ok access_token ->
299+
State.set_slack_access_token ctx.state access_token;
300+
( match ctx.state_filepath with
301+
| None -> Lwt.return "ok"
302+
| Some path ->
303+
( match%lwt State.save ctx.state path with
304+
| Ok () -> Lwt.return "ok"
305+
| Error e -> action_error e
306+
)
307+
)
299308
)
300309
with
301310
| Yojson.Json_error msg ->

lib/api.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ module type Slack = sig
1717

1818
val send_chat_unfurl : ctx:Context.t -> chat_unfurl_req -> (unit, string) Result.t Lwt.t
1919

20-
val update_access_token_of_context : ctx:Context.t -> code:string -> (unit, string) Result.t Lwt.t
20+
val access_token_of_code : ctx:Context.t -> code:string -> (string, string) Result.t Lwt.t
2121
end

lib/api_local.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Slack_base : Api.Slack = struct
2929

3030
let send_chat_unfurl ~ctx:_ _ = Lwt.return @@ Error "undefined for local setup"
3131

32-
let update_access_token_of_context ~ctx:_ ~code:_ = Lwt.return @@ Error "undefined for local setup"
32+
let access_token_of_code ~ctx:_ ~code:_ = Lwt.return @@ Error "undefined for local setup"
3333
end
3434

3535
module Slack : Api.Slack = struct
@@ -41,9 +41,7 @@ module Slack : Api.Slack = struct
4141
Stdio.printf "%s\n" json;
4242
Lwt.return @@ Ok ()
4343

44-
let update_access_token_of_context ~ctx:_ ~code:_ =
45-
Stdio.printf "will generate token\n";
46-
Lwt.return @@ Ok ()
44+
let access_token_of_code ~ctx:_ ~code = Lwt.return @@ Ok (Printf.sprintf "token of code %s" code)
4745
end
4846

4947
module Slack_simple : Api.Slack = struct

lib/api_remote.ml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,4 @@ module Slack : Api.Slack = struct
142142
| None, None -> Lwt.return @@ Error "an unknown error occurred while getting access token"
143143
)
144144
)
145-
146-
let update_access_token_of_context ~ctx ~code =
147-
let secrets = Context.get_secrets_exn ctx in
148-
match%lwt access_token_of_code ~ctx ~code with
149-
| Error e -> Lwt.return @@ Error e
150-
| Ok access_token ->
151-
let secrets = { secrets with slack_access_token = Some access_token } in
152-
ctx.secrets <- Some secrets;
153-
let data = Config_j.string_of_secrets secrets |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
154-
Lwt.return @@ write_to_local_file ~data ctx.secrets_filepath
155145
end

lib/context.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ let refresh_state ctx =
8686
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
8787
| Ok file ->
8888
let state = State_j.state_of_string file in
89+
let secrets = get_secrets_exn ctx in
90+
begin
91+
match secrets.slack_access_token with
92+
| None -> ()
93+
| Some token -> State.set_slack_access_token ctx.state token
94+
end;
8995
Ok { ctx with state }
9096
end
9197
else Ok ctx

lib/state.atd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ type pipeline_statuses = branch_statuses map_as_object
1414

1515
(* The serializable runtime state of the bot *)
1616
type state = {
17-
pipeline_statuses <ocaml mutable>: pipeline_statuses
17+
pipeline_statuses <ocaml mutable>: pipeline_statuses;
18+
?slack_access_token <ocaml mutable>: string nullable;
1819
}

lib/state.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Base
22
open Common
33
open Devkit
44

5-
let empty : State_t.state = { pipeline_statuses = StringMap.empty }
5+
let empty : State_t.state = { pipeline_statuses = StringMap.empty; slack_access_token = None }
66

77
let refresh_pipeline_status (state : State_t.state) ~pipeline ~(branches : Github_t.branch list) ~status =
88
let update_pipeline_status branch_statuses =
@@ -12,6 +12,8 @@ let refresh_pipeline_status (state : State_t.state) ~pipeline ~(branches : Githu
1212
in
1313
state.pipeline_statuses <- Map.update state.pipeline_statuses pipeline ~f:update_pipeline_status
1414

15+
let set_slack_access_token (state : State_t.state) access_token = state.slack_access_token <- Some access_token
16+
1517
let log = Log.from "state"
1618

1719
let save state path =

0 commit comments

Comments
 (0)