Skip to content

Commit c42b86f

Browse files
committed
Merge branch 'hotfix/config-init-and-cli-flags'
* hotfix/config-init-and-cli-flags: prepend slash to commits_url fix check_gh payload and secrets initialization don't try to load state from file on startup if doesn't exist print config after initialization
2 parents 4ef5bb1 + b31adf1 commit c42b86f

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

lib/action.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
198198
let repo = Github.repo_of_notification notification in
199199
match%lwt Github_api.get_config ~ctx ~repo with
200200
| Ok config ->
201-
Context.print_config ctx;
202201
ctx.config <- Some config;
202+
Context.print_config ctx;
203203
Lwt.return @@ Ok ()
204204
| Error e -> action_error e
205205
in

lib/api_remote.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open Common
55

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

1010
let contents_url ~(repo : Github_t.repository) ~path =
1111
String.substr_replace_first ~pattern:"{+path}" ~with_:path repo.contents_url

lib/context.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ let refresh_state ctx =
7474
match ctx.state_filepath with
7575
| None -> Ok ctx
7676
| Some path ->
77-
log#info "loading saved state from file %s" path;
78-
( match get_local_file path with
79-
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
80-
| Ok file ->
81-
let state = State_j.state_of_string file in
82-
Ok { ctx with state }
83-
)
77+
if Caml.Sys.file_exists path then begin
78+
log#info "loading saved state from file %s" path;
79+
match get_local_file path with
80+
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
81+
| Ok file ->
82+
let state = State_j.state_of_string file in
83+
Ok { ctx with state }
84+
end
85+
else Ok ctx
8486

8587
let print_config ctx =
8688
let cfg = get_config_exn ctx in

src/notabot.ml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let http_server_action addr port config secrets state =
2121
(** In check mode, instead of actually sending the message to slack, we
2222
simply print it in the console *)
2323
let check_gh_action file json config secrets state =
24-
match Github.event_of_filename file with
24+
match Github.event_of_filename (Caml.Filename.basename file) with
2525
| None ->
2626
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file
2727
| Some kind ->
@@ -30,14 +30,18 @@ let check_gh_action file json config secrets state =
3030
| Ok body ->
3131
let headers = [ "x-github-event", kind ] in
3232
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
33-
Lwt_main.run
34-
( if json then
35-
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
36-
Action.process_github_notification ctx headers body
37-
else
38-
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
39-
Action.process_github_notification ctx headers body
40-
)
33+
( match Context.refresh_secrets ctx with
34+
| Error e -> log#error "%s" e
35+
| Ok ctx ->
36+
Lwt_main.run
37+
( if json then
38+
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
39+
Action.process_github_notification ctx headers body
40+
else
41+
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
42+
Action.process_github_notification ctx headers body
43+
)
44+
)
4145

4246
let check_slack_action url file =
4347
let data = Stdio.In_channel.read_all file in
@@ -73,7 +77,7 @@ let secrets =
7377

7478
let state =
7579
let doc = "state file" in
76-
Arg.(value & opt (some file) None & info [ "state" ] ~docv:"STATE" ~doc)
80+
Arg.(value & opt (some string) None & info [ "state" ] ~docv:"STATE" ~doc)
7781

7882
let gh_payload =
7983
let doc = "JSON file containing a github webhook payload" in

0 commit comments

Comments
 (0)