From 52950a719f7bed5765121fd88b28908147ef7966 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Thu, 24 Dec 2020 14:36:27 +0800 Subject: [PATCH 1/4] print config after initialization --- lib/action.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/action.ml b/lib/action.ml index d63ac13a..e01991aa 100644 --- a/lib/action.ml +++ b/lib/action.ml @@ -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 From 6d428aedc2d996eb20bb668dbd0c293724af3803 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Thu, 24 Dec 2020 14:44:09 +0800 Subject: [PATCH 2/4] don't try to load state from file on startup if doesn't exist --- lib/context.ml | 16 +++++++++------- src/notabot.ml | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/context.ml b/lib/context.ml index bbbd70a8..7516e6d8 100644 --- a/lib/context.ml +++ b/lib/context.ml @@ -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 diff --git a/src/notabot.ml b/src/notabot.ml index e66a146c..7e272107 100644 --- a/src/notabot.ml +++ b/src/notabot.ml @@ -73,7 +73,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 From ba1679aabcb80ad068daf673f2657edf46e0dda1 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Thu, 24 Dec 2020 14:45:41 +0800 Subject: [PATCH 3/4] fix check_gh payload and secrets initialization --- src/notabot.ml | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/notabot.ml b/src/notabot.ml index 7e272107..b4a5c2b2 100644 --- a/src/notabot.ml +++ b/src/notabot.ml @@ -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 -> @@ -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 From b31adf180248d1b3395d9ac5e066871eb00c3eb4 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Mon, 28 Dec 2020 15:03:48 +0800 Subject: [PATCH 4/4] prepend slash to commits_url --- lib/api_remote.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api_remote.ml b/lib/api_remote.ml index 8ec07bf7..5e7b4b94 100644 --- a/lib/api_remote.ml +++ b/lib/api_remote.ml @@ -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