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

config: allow config filename per repo #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -286,7 +286,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
| Github.Push commit_pushed_notification ->
let commits = commit_pushed_notification.commits in
let modified_files = List.concat_map Github.modified_files_of_commit commits in
let config_was_modified = List.exists (String.equal ctx.config_filename) modified_files in
let config_was_modified = List.exists (String.equal (Context.get_config_filename ctx repo.url)) modified_files in
if config_was_modified then fetch_config ~ctx ~repo else Lwt.return @@ Ok ()
| _ -> Lwt.return @@ Ok ()

Expand Down
4 changes: 2 additions & 2 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ let get_repo_member_cache ~(repo : Github_t.repository) ~kind ~ref_ ~of_string =
with_cache_file url of_string

module Github : Api.Github = struct
let get_config ~(ctx : Context.t) ~repo:_ =
let url = Filename.concat cwd ctx.config_filename in
let get_config ~(ctx : Context.t) ~(repo : Github_t.repository) =
let url = Filename.concat cwd (Context.get_config_filename ctx repo.url) in
with_cache_file url Config_j.config_of_string

let get_api_commit ~ctx:_ ~repo ~sha =
Expand Down
2 changes: 1 addition & 1 deletion lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Github : Api.Github = struct

let get_config ~(ctx : Context.t) ~repo =
let secrets = Context.get_secrets_exn ctx in
let url = contents_url ~repo ~path:ctx.config_filename in
let url = contents_url ~repo ~path:(Context.get_config_filename ctx repo.url) in
let token = Context.gh_token_of_secrets secrets repo.url in
let headers = build_headers ?token () in
match%lwt http_request ~headers `GET url with
Expand Down
2 changes: 2 additions & 0 deletions lib/config.atd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type repo_config = {
?gh_token : string nullable;
(* GitHub webhook secret token to secure the webhook *)
?gh_hook_secret : string nullable;
(* override the default config filename for one repository *)
?config_filename : string nullable;
}

(* This is the structure of the secrets file which stores sensitive information, and
Expand Down
8 changes: 8 additions & 0 deletions lib/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ let get_secrets_exn ctx =
| None -> context_error "secrets is uninitialized"
| Some secrets -> secrets

let get_config_filename ctx repo_url =
match get_secrets_exn ctx with
| exception Context_error _ -> ctx.config_filename
Comment on lines +38 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be better without this catch, but the tests are failing if I omit it.

| secrets ->
match List.find_opt (fun r -> String.equal r.Config_t.url repo_url) secrets.repos with
| None | Some { config_filename = None; _ } -> ctx.config_filename
| Some { config_filename = Some f; _ } -> f

let find_repo_config ctx repo_url = Stringtbl.find_opt ctx.config repo_url

let find_repo_config_exn ctx repo_url =
Expand Down
4 changes: 3 additions & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ let process_gh_payload ~(secrets : Config_t.secrets) ~config (kind, path, state_
let make_test_context event =
let repo = Github.repo_of_notification @@ Github.parse_exn headers event in
(* overwrite repo url in secrets with that of notification for this test case *)
let secrets = { secrets with repos = [ { url = repo.url; gh_token = None; gh_hook_secret = None } ] } in
let secrets =
{ secrets with repos = [ { url = repo.url; gh_token = None; gh_hook_secret = None; config_filename = None } ] }
in
let ctx = Context.make () in
ctx.secrets <- Some secrets;
let (_ : State_t.repo_state) = State.find_or_add_repo ctx.state repo.url in
Expand Down