Skip to content

Commit 233543c

Browse files
committed
cfg: unify repos and allowed_repos into singlee field
1 parent 6813222 commit 233543c

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

documentation/secret_docs.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ A secrets file stores sensitive information. Unlike the repository configuration
1717
|-|-|-|-|
1818
| `gh_token` | specify to grant the bot access to private repositories; omit for public repositories | Yes | - |
1919
| `gh_hook_token` | specify to ensure the bot only receives GitHub notifications from pre-approved repositories | Yes | - |
20-
| `repos` | an object mapping repository URLs to repository-specific GitHub secrets | Yes | - |
21-
| `allowed_repos` | a whitelist of repository URLs to process payloads for | Yes | all incoming payloads are processed |
20+
| `repos` | specify to use Monorobot in multiple repositories (with support for overriding secrets) | Yes | - |
2221
| `slack_access_token` | slack bot access token to enable message posting to the workspace | Yes | try to use webhooks defined in `slack_hooks` instead |
2322
| `slack_hooks` | list of channel names and their corresponding webhook endpoint | Yes | try to use token defined in `slack_access_token` instead |
2423
| `slack_signing_secret` | specify to verify incoming slack requests | Yes | - |
@@ -39,28 +38,26 @@ Refer [here](https://docs.github.com/en/free-pro-team@latest/developers/webhooks
3938

4039
## `repos`
4140

42-
If you're using Monorobot for multiple repositories that need different secrets (e.g., one on github.com and another on GitHub Enterprise), you can provide them as an object. Secrets defined here will take precedence over those defined at the top level of the secrets file.
41+
Specifies which repositories to accept events from, along with any repository-specific overrides to secrets. If omitted, assumes all notifications come from a single repository and accepts all events.
42+
43+
Secrets defined here take precedence over those defined at the top level of the secrets file.
4344

4445
Repository URLs should be fully qualified (include the protocol), with no trailing backslash.
4546

4647
```json
47-
{
48-
"https://github.com/ahrefs/runner" : {
48+
[
49+
{
50+
"url": "https://github.com/ahrefs/runner",
4951
"gh_token": "XXX"
5052
},
51-
"https://git.ahrefs.com/ahrefs/coyote" : {
53+
{
54+
"url": "https://git.ahrefs.com/ahrefs/coyote",
5255
"gh_token": "XXX",
5356
"gh_hook_token": "XXX"
5457
}
55-
}
58+
]
5659
```
5760

58-
## `allowed_repos`
59-
60-
Use this option to restrict incoming notifications from GitHub to approved repository URLs.
61-
62-
Repository URLs should be fully qualified (include the protocol), with no trailing backslash.
63-
6461
## `slack_access_token`
6562

6663
Required for:

lib/action.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
239239
in
240240
let repo_is_allowed secrets payload =
241241
let repo = Github.repo_of_notification payload in
242-
let allowed_repos = secrets.allowed_repos in
243-
List.is_empty allowed_repos || List.exists allowed_repos ~f:(String.equal repo.url)
242+
List.is_empty secrets.repos || List.exists secrets.repos ~f:(fun r -> String.equal r.url repo.url)
244243
in
245244
try%lwt
246245
let secrets = Context.get_secrets_exn ctx in

lib/config.atd

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ type status_rule <ocaml from="Rule"> = abstract
22
type prefix_rule <ocaml from="Rule"> = abstract
33
type label_rule <ocaml from="Rule"> = abstract
44
type project_owners_rule <ocaml from="Rule"> = abstract
5-
type 'v map_as_object <ocaml from="Common"> = abstract
65

76
(* This type of rule is used for CI build notifications. *)
87
type status_rules = {
@@ -45,24 +44,24 @@ type webhook = {
4544
channel : string; (* name of the Slack channel to post the message *)
4645
}
4746

48-
type gh_repo_secrets = {
47+
type gh_secrets = {
4948
(* GitHub personal access token, if repo access requires it *)
5049
?gh_token : string nullable;
5150
(* GitHub webhook token to secure the webhook *)
5251
?gh_hook_token : string nullable;
5352
}
5453

54+
type repo_config = {
55+
url : string;
56+
inherit gh_secrets;
57+
}
58+
5559
(* This is the structure of the secrets file which stores sensitive information, and
5660
shouldn't be checked into version control. *)
5761
type secrets = {
62+
inherit gh_secrets;
5863
(* repo-specific secrets; overrides global values if defined for a given repo *)
59-
~repo_secrets <ocaml default="Common.StringMap.empty"> : gh_repo_secrets map_as_object;
60-
(* whitelist of repository URLs to handle notifications for *)
61-
~allowed_repos <ocaml default="[]"> : string list;
62-
(* GitHub personal access token, if repo access requires it *)
63-
?gh_token : string nullable;
64-
(* GitHub webhook token to secure the webhook *)
65-
?gh_hook_token : string nullable;
64+
~repos <ocaml default="[]"> : repo_config list;
6665
(* list of Slack webhook & channel name pairs *)
6766
~slack_hooks <ocaml default="[]"> : webhook list;
6867
(* Slack bot token (`xoxb-XXXX`), giving the bot capabilities to interact with the workspace *)

lib/context.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type t = {
1616
}
1717

1818
let default_config_filename = "monorobot.json"
19-
2019
let default_secrets_filepath = "secrets.json"
2120

2221
let make ?config_filename ?secrets_filepath ?state_filepath () =
@@ -44,14 +43,14 @@ let find_repo_config_exn ctx repo_url =
4443
let set_repo_config ctx repo_url config = Stringtbl.set ctx.config ~key:repo_url ~data:config
4544

4645
let gh_token_of_secrets (secrets : Config_t.secrets) repo_url =
47-
match Map.find secrets.repo_secrets repo_url with
46+
match List.find secrets.repos ~f:(fun r -> String.equal r.Config_t.url repo_url) with
4847
| None -> secrets.gh_token
49-
| Some repo_secrets -> repo_secrets.gh_token
48+
| Some repos -> repos.gh_token
5049

5150
let gh_hook_token_of_secrets (secrets : Config_t.secrets) repo_url =
52-
match Map.find secrets.repo_secrets repo_url with
51+
match List.find secrets.repos ~f:(fun r -> String.equal r.Config_t.url repo_url) with
5352
| None -> secrets.gh_hook_token
54-
| Some repo_secrets -> repo_secrets.gh_hook_token
53+
| Some repos -> repos.gh_hook_token
5554

5655
let hook_of_channel ctx channel_name =
5756
let secrets = get_secrets_exn ctx in

0 commit comments

Comments
 (0)