Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial load of config/state/secrets #99

Merged
merged 4 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
let repo = Github.repo_of_notification notification in
match%lwt Github_api.get_config ~ctx ~repo with
| Ok config ->
Context.print_config ctx;
ctx.config <- Some config;
Context.print_config ctx;
Lwt.return @@ Ok ()
| Error e -> action_error e
in
Expand Down
2 changes: 1 addition & 1 deletion lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Common

module Github : Api.Github = struct
let commits_url ~(repo : Github_t.repository) ~sha =
String.substr_replace_first ~pattern:"{/sha}" ~with_:sha repo.commits_url
String.substr_replace_first ~pattern:"{/sha}" ~with_:("/" ^ sha) repo.commits_url

let contents_url ~(repo : Github_t.repository) ~path =
String.substr_replace_first ~pattern:"{+path}" ~with_:path repo.contents_url
Expand Down
16 changes: 9 additions & 7 deletions lib/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ let refresh_state ctx =
match ctx.state_filepath with
| None -> Ok ctx
| Some path ->
log#info "loading saved state from file %s" path;
( match get_local_file path with
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
| Ok file ->
let state = State_j.state_of_string file in
Ok { ctx with state }
)
if Caml.Sys.file_exists path then begin
log#info "loading saved state from file %s" path;
match get_local_file path with
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
| Ok file ->
let state = State_j.state_of_string file in
Ok { ctx with state }
end
else Ok ctx

let print_config ctx =
let cfg = get_config_exn ctx in
Expand Down
24 changes: 14 additions & 10 deletions src/notabot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let http_server_action addr port config secrets state =
(** In check mode, instead of actually sending the message to slack, we
simply print it in the console *)
let check_gh_action file json config secrets state =
match Github.event_of_filename file with
match Github.event_of_filename (Caml.Filename.basename file) with
| None ->
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file
| Some kind ->
Expand All @@ -30,14 +30,18 @@ let check_gh_action file json config secrets state =
| Ok body ->
let headers = [ "x-github-event", kind ] in
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
Lwt_main.run
( if json then
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
Action.process_github_notification ctx headers body
else
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
Action.process_github_notification ctx headers body
)
( match Context.refresh_secrets ctx with
| Error e -> log#error "%s" e
| Ok ctx ->
Lwt_main.run
( if json then
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
Action.process_github_notification ctx headers body
else
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
Action.process_github_notification ctx headers body
)
)

let check_slack_action url file =
let data = Stdio.In_channel.read_all file in
Expand Down Expand Up @@ -73,7 +77,7 @@ let secrets =

let state =
let doc = "state file" in
Arg.(value & opt (some file) None & info [ "state" ] ~docv:"STATE" ~doc)
Arg.(value & opt (some string) None & info [ "state" ] ~docv:"STATE" ~doc)

let gh_payload =
let doc = "JSON file containing a github webhook payload" in
Expand Down