diff --git a/lib/api_local.ml b/lib/api_local.ml index 4d78c883..9d6a522e 100644 --- a/lib/api_local.ml +++ b/lib/api_local.ml @@ -7,8 +7,10 @@ let cwd = Caml.Sys.getcwd () let cache_dir = Caml.Filename.concat cwd "github-api-cache" module Github : Api.Github = struct + let mock_config_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_config" + let get_config ~(ctx : Context.t) ~repo:_ = - let url = Caml.Filename.concat cwd ctx.config_filename in + let url = Caml.Filename.concat mock_config_dir ctx.config_filename in match get_local_file url with | Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url | Ok file -> Lwt.return @@ Ok (Config_j.config_of_string file) diff --git a/test/notabot.json b/mock_config/notabot.json similarity index 100% rename from test/notabot.json rename to mock_config/notabot.json diff --git a/test/secrets.json b/mock_secrets/secrets.json similarity index 100% rename from test/secrets.json rename to mock_secrets/secrets.json diff --git a/mock_states/status.state_hide_success_test.json b/mock_states/status.state_hide_success_test.json index 6d188219..6af4a551 100644 --- a/mock_states/status.state_hide_success_test.json +++ b/mock_states/status.state_hide_success_test.json @@ -1,11 +1,13 @@ { - "pipeline_statuses": { - "default": { - "master": "failure" - }, - "buildkite/notabot-test": { - "master": "success" + "https://git.ahrefs.com/ahrefs/notabot_test": { + "pipeline_statuses": { + "default": { + "master": "failure" + }, + "buildkite/notabot-test": { + "master": "success" + } } } -} \ No newline at end of file +} diff --git a/mock_states/status.state_hide_success_test_disallowed_pipeline.json b/mock_states/status.state_hide_success_test_disallowed_pipeline.json index b97c98e8..13626e2a 100644 --- a/mock_states/status.state_hide_success_test_disallowed_pipeline.json +++ b/mock_states/status.state_hide_success_test_disallowed_pipeline.json @@ -1,7 +1,9 @@ { - "pipeline_statuses": { - "buildkite/notabot-test": { - "master": "failure" + "https://git.ahrefs.com/ahrefs/notabot_test": { + "pipeline_statuses": { + "buildkite/notabot-test": { + "master": "failure" + } } } -} \ No newline at end of file +} diff --git a/test/dune b/test/dune index 272bc3e0..b8bc0cd0 100644 --- a/test/dune +++ b/test/dune @@ -9,9 +9,9 @@ (deps (source_tree ../mock_states) (source_tree ../mock_payloads) - (source_tree github-api-cache) - notabot.json - secrets.json) + (source_tree ../mock_config) + (source_tree ../mock_secrets) + (source_tree github-api-cache)) (action (with-stdout-to slack_payloads.out diff --git a/test/test.ml b/test/test.ml index 491b5e01..aee7096c 100644 --- a/test/test.ml +++ b/test/test.ml @@ -1,12 +1,17 @@ open Base open Lib +open Common let log = Devkit.Log.from "test" +let () = Devkit.Log.set_loglevels "error" + let mock_payload_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_payloads" let mock_state_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_states" +let mock_secrets_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_secrets" + module Action_local = Action.Action (Api_local.Github) (Api_local.Slack) let get_mock_payloads () = @@ -19,51 +24,29 @@ let get_mock_payloads () = let state_path = Caml.Filename.concat mock_state_dir fn in if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None) -let process ~(secrets : Config_t.secrets) ~config (kind, path, state_path) = - let headers = [ "x-github-event", kind ] in - let make_test_context event = - let repo = Github.repo_of_notification @@ Github.parse_exn headers event in - let ctx = Context.make () in - ctx.secrets <- Some secrets; - ignore (State.find_or_add_repo ctx.state repo.url); - match state_path with - | None -> - State.set_repo_config ctx.state ~config ~repo_url:repo.url; - Lwt.return ctx - | Some state_path -> - match Common.get_local_file state_path with - | Error e -> - log#error "failed to read %s: %s" state_path e; - Lwt.return ctx - | Ok file -> - let repo_state = State_j.repo_state_of_string file in - Hashtbl.set ctx.state ~key:repo.url ~data:repo_state; - State.set_repo_config ctx.state ~repo_url:repo.url ~config; - Lwt.return ctx +let process (kind, path, state_filepath) = + let make_test_context () = + let config_filename = Context.default.config_filename in + let secrets_filepath = Caml.Filename.concat mock_secrets_dir Context.default.secrets_filepath in + let ctx = Context.make ~config_filename ~secrets_filepath ?state_filepath ~verbose:false () in + match Context.refresh_state ctx with + | Error e -> fmt_error "failed to read state: %s" e + | Ok ctx -> + match Context.refresh_secrets ctx with + | Error e -> fmt_error "failed to read secrets: %s" e + | Ok ctx -> Ok ctx in Stdio.printf "===== file %s =====\n" path; let headers = [ "x-github-event", kind ] in - match Common.get_local_file path with + match get_local_file path with | Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e | Ok event -> - let%lwt ctx = make_test_context event in + match make_test_context () with + | Error e -> Lwt.return @@ log#error "%s" e + | Ok ctx -> let%lwt _ctx = Action_local.process_github_notification ctx headers event in Lwt.return_unit let () = let payloads = get_mock_payloads () in - let repo : Github_t.repository = { name = ""; full_name = ""; url = ""; commits_url = ""; contents_url = "" } in - let ctx = Context.make ~state_filepath:"state.json" () in - Lwt_main.run - ( match%lwt Api_local.Github.get_config ~ctx ~repo with - | Error e -> - log#error "%s" e; - Lwt.return_unit - | Ok config -> - match Context.refresh_secrets ctx with - | Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads - | Error e -> - log#error "failed to read secrets:"; - log#error "%s" e; - Lwt.return_unit - ) + Lwt_main.run (Lwt_list.iter_s process payloads)