File tree Expand file tree Collapse file tree 7 files changed +26
-20
lines changed Expand file tree Collapse file tree 7 files changed +26
-20
lines changed Original file line number Diff line number Diff line change @@ -284,7 +284,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
284
284
let process_slack_oauth (ctx : Context.t ) args =
285
285
try % lwt
286
286
let secrets = Context. get_secrets_exn ctx in
287
- match secrets .slack_access_token with
287
+ match ctx.state .slack_access_token with
288
288
| Some _ -> Lwt. return " ok"
289
289
| None ->
290
290
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
293
293
match List.Assoc. find args " code" ~equal: String. equal with
294
294
| None -> action_error " argument `code` not found in slack authorization request"
295
295
| 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
297
297
| 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
+ )
299
308
)
300
309
with
301
310
| Yojson. Json_error msg ->
Original file line number Diff line number Diff line change @@ -17,5 +17,5 @@ module type Slack = sig
17
17
18
18
val send_chat_unfurl : ctx :Context .t -> chat_unfurl_req -> (unit , string ) Result .t Lwt .t
19
19
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
21
21
end
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ module Slack_base : Api.Slack = struct
29
29
30
30
let send_chat_unfurl ~ctx :_ _ = Lwt. return @@ Error " undefined for local setup"
31
31
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"
33
33
end
34
34
35
35
module Slack : Api .Slack = struct
@@ -41,9 +41,7 @@ module Slack : Api.Slack = struct
41
41
Stdio. printf " %s\n " json;
42
42
Lwt. return @@ Ok ()
43
43
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)
47
45
end
48
46
49
47
module Slack_simple : Api .Slack = struct
Original file line number Diff line number Diff line change @@ -142,14 +142,4 @@ module Slack : Api.Slack = struct
142
142
| None , None -> Lwt. return @@ Error " an unknown error occurred while getting access token"
143
143
)
144
144
)
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
155
145
end
Original file line number Diff line number Diff line change @@ -86,6 +86,12 @@ let refresh_state ctx =
86
86
| Error e -> fmt_error " error while getting local file: %s\n failed to get state from file %s" e path
87
87
| Ok file ->
88
88
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 ;
89
95
Ok { ctx with state }
90
96
end
91
97
else Ok ctx
Original file line number Diff line number Diff line change @@ -14,5 +14,6 @@ type pipeline_statuses = branch_statuses map_as_object
14
14
15
15
(* The serializable runtime state of the bot *)
16
16
type state = {
17
- pipeline_statuses <ocaml mutable>: pipeline_statuses
17
+ pipeline_statuses <ocaml mutable>: pipeline_statuses;
18
+ ?slack_access_token <ocaml mutable>: string nullable;
18
19
}
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ open Base
2
2
open Common
3
3
open Devkit
4
4
5
- let empty : State_t.state = { pipeline_statuses = StringMap. empty }
5
+ let empty : State_t.state = { pipeline_statuses = StringMap. empty; slack_access_token = None }
6
6
7
7
let refresh_pipeline_status (state : State_t.state ) ~pipeline ~(branches : Github_t.branch list ) ~status =
8
8
let update_pipeline_status branch_statuses =
@@ -12,6 +12,8 @@ let refresh_pipeline_status (state : State_t.state) ~pipeline ~(branches : Githu
12
12
in
13
13
state.pipeline_statuses < - Map. update state.pipeline_statuses pipeline ~f: update_pipeline_status
14
14
15
+ let set_slack_access_token (state : State_t.state ) access_token = state.slack_access_token < - Some access_token
16
+
15
17
let log = Log. from " state"
16
18
17
19
let save state path =
You can’t perform that action at this time.
0 commit comments