Skip to content

Commit 9a84ab3

Browse files
authored
Remove the optional of github_token, make github_pat optional (#361)
2 parents 7934b50 + 56d9e58 commit 9a84ab3

24 files changed

+539
-265
lines changed

README.md

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,6 @@ you are interested in hearing about it.
196196
197197
## How to use the **@coqbot** instance ##
198198
199-
### As a GitHub App
200-
201-
This is the recommended installation method, as this is both easier to
202-
set up and it gives access to new features (related to GitHub Checks).
203-
204-
Notes:
205-
206-
- Installation as a GitHub App is still in an experimental stage
207-
and you may frequently receive requests to expand permissions.
208-
209-
- All the repositories that use the bot and belong to the same
210-
owner must install the bot using the same method (GitHub App or
211-
regular user).
212-
213-
- If you were previously using the legacy installation method,
214-
make sure you disable any previously set up GitHub webhooks when
215-
switching to the GitHub App, otherwise the bot will receive every
216-
request twice.
217-
218199
The bot can be installed as a GitHub App to either your account or
219200
organization ([link to app](https://github.com/apps/coqbot-app)).
220201
Once you finish the installation, follow these steps:
@@ -271,32 +252,6 @@ Once you finish the installation, follow these steps:
271252
configuration file becomes `BOT_NAME.toml` where `BOT_NAME` is the name
272253
of the bot.
273254

274-
### As a regular user account (legacy)
275-
276-
The bot used to be given access to each of your GitHub repositories as a
277-
regular GitHub user account (**@coqbot**). This installation method is
278-
still supported for repositories that haven't migrated to the GitHub App
279-
yet. Here are the steps to follow in addition to those described in the
280-
`As GitHub App` section:
281-
282-
- In your GitHub repository:
283-
284-
- go to "Settings" / "Manage access" to add
285-
[**@coqbot**](https://github.com/coqbot) as a collaborator with
286-
the "Write" role (so that it can push status checks, and set
287-
labels).
288-
289-
Currently, every invitation requires a manual validation, so there
290-
may be some lag before **@coqbot** can push status checks
291-
to your repository.
292-
293-
- go to "Settings" / "Webhooks" and add one webhook with URL
294-
<https://coqbot.herokuapp.com/github> that will only be triggered
295-
at least by pull request events, and if you want to use the issue
296-
milestone feature, by issue events as well. Make sure you change
297-
the "content/type" value to "application/json".
298-
299-
300255

301256
## Architecture ##
302257

@@ -352,14 +307,16 @@ to [Heroku](https://www.heroku.com/). Simply follow the official
352307
The bot will need to read a few environment variables so make sure
353308
these are configured in your Heroku app:
354309

355-
- `GITHUB_ACCESS_TOKEN` (can also be defined in the configuration file as `github.api_token`)
356310
- `GITLAB_ACCESS_TOKEN` (can also be defined for each GitLab instance through the configuration file as `api_token` or through an environment variable whose name is defined in the configuration file as `api_token_env_var`)
357311
- `GITHUB_WEBHOOK_SECRET` (can also be defined in the configuration file as `github.webhook_secret`)
358312
- `GITLAB_WEBHOOK_SECRET` (can also be defined in the configuration file as `gitlab.webhook_secret`, will default to `GITHUB_WEBHOOK_SECRET` if not defined)
359313
- `DAILY_SCHEDULE_SECRET` (can also be defined in the configuration file as `github.daily_schedule_secret`, will default to `GITHUB_WEBHOOK_SECRET` if not defined)
360314
- `GITHUB_APP_ID` (can also be defined in the configuration file as `github.app_id`)
361315
- `GITHUB_PRIVATE_KEY` (a private key of your GitHub app)
362316
- `PORT` (can also be defined in the configuration file as `server.port`)
317+
- (optional) `GITHUB_ACCESS_TOKEN` / `github.api_token` in config: only needed
318+
for Rocq minimization flows that must act as the `coqbot` user rather than
319+
the GitHub App
363320

364321
Then, you must configure the bot with a configuration file. Here is an example
365322
to adapt to your needs [`example-config.toml`](example-config.toml).

bot-components/Bot_info.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ open Base
22

33
type t =
44
{ gitlab_instances: (string, string * string) Hashtbl.t
5-
; github_pat: string
6-
; github_install_token: string option
5+
; github_pat: string option
6+
; github_install_token: string
77
; github_name: string
88
; email: string
99
; domain: string
1010
; app_id: int }
1111

12-
let github_token bot_info =
13-
match bot_info.github_install_token with
14-
| Some t ->
15-
t
12+
let github_pat bot_info =
13+
match bot_info.github_pat with
14+
| Some pat ->
15+
pat
1616
| None ->
17-
bot_info.github_pat
17+
failwith
18+
"No GitHub PAT available. This operation requires a GitHub PAT. Please \
19+
ensure the PAT is set in the configuration."
20+
21+
let github_token bot_info = bot_info.github_install_token
1822

1923
let gitlab_name_and_token bot_info gitlab_domain =
2024
match Hashtbl.find bot_info.gitlab_instances gitlab_domain with

bot-components/Bot_info.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
type t =
22
{ gitlab_instances: (string, string * string) Base.Hashtbl.t
3-
; github_pat: string
4-
; github_install_token: string option
3+
; github_pat: string option
4+
; github_install_token: string
55
; github_name: string
66
; email: string
77
; domain: string
88
; app_id: int }
99

10+
val github_pat : t -> string
11+
1012
val github_token : t -> string
1113

1214
val gitlab_token : t -> string -> (string, string) Result.t

bot-components/GitHub_subscriptions.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ let github_event ~event json =
235235
| _ ->
236236
Ok (UnsupportedEvent (f "Unsupported GitHub event %s." event))
237237

238+
let legacy_webhook_log =
239+
"Error: received GitHub webhook without installation.id (legacy installation \
240+
method)"
241+
238242
let receive_github ~secret headers body =
239243
let open Result.Monad_infix in
240244
match Header.get headers "X-GitHub-Event" with
@@ -258,6 +262,11 @@ let receive_github ~secret headers body =
258262
Error "Webhook comes from a GitHub App, but it is not signed."
259263
with Yojson.Json_error _ | Type_error _ -> Ok None )
260264
>>= fun install_id ->
265+
( match install_id with
266+
| None ->
267+
Stdio.eprintf "%s\n" legacy_webhook_log
268+
| Some _ ->
269+
() ) ;
261270
github_event ~event json |> Result.map ~f:(fun r -> (install_id, r))
262271
with
263272
| Yojson.Json_error err ->

bot-components/GitHub_subscriptions.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type msg =
1616
| PushEvent of push_info
1717
| UnsupportedEvent of string
1818

19+
val legacy_webhook_log : string
20+
1921
val receive_github :
2022
secret:string
2123
-> Cohttp.Header.t

bot-components/Github_installations.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let action_with_new_installation_token ~bot_info ~key ~app_id ~install_id action
1919
~data:(install_token, expiration_date)
2020
in
2121
let bot_info : Bot_info.t =
22-
{bot_info with github_install_token= Some install_token}
22+
{bot_info with github_install_token= install_token}
2323
in
2424
action ~bot_info
2525
| Error err ->
@@ -42,7 +42,7 @@ let action_as_github_app_from_install_id ~bot_info ~key ~app_id ~install_id
4242
action )
4343
else
4444
let bot_info : Bot_info.t =
45-
{bot_info with github_install_token= Some install_token}
45+
{bot_info with github_install_token= install_token}
4646
in
4747
action ~bot_info
4848
| None ->

bot-components/GraphQL_query.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let send_graphql_query ~bot_info ?(extra_headers = []) ?(ignore_errors = false)
1919
| GitLab gitlab_domain ->
2020
gitlab_name_and_token bot_info gitlab_domain
2121
| GitHub ->
22-
Ok (bot_info.github_name, github_token bot_info) )
22+
Ok (bot_info.github_name, bot_info.github_install_token) )
2323
|> Lwt.return
2424
>>= fun (name, token) ->
2525
let headers =

bot-components/HTTP_utils.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let headers_of_list = headers
2121

2222
(* GitHub authorization header builder *)
2323
let github_header bot_info =
24-
[("Authorization", "bearer " ^ github_token bot_info)]
24+
[("Authorization", "bearer " ^ bot_info.github_install_token)]
2525

2626
(* GitHub API preview headers *)
2727
let project_api_preview_header =

src/actions_job.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ let job_action ~bot_info
4747
Lwt.return_unit )
4848
in
4949
Ci_job_status.job_failure ~bot_info job_info ~pr_num
50-
(gh_owner, gh_repo) ~github_repo_full_name ~gitlab_domain
51-
~gitlab_repo_full_name ~context ~failure_reason ~external_id
52-
~summary_builder ~allow_failure_handler ()
50+
(gh_owner, gh_repo) ~gitlab_domain ~gitlab_repo_full_name ~context
51+
~failure_reason ~external_id ~summary_builder
52+
~allow_failure_handler ()
5353
| "success" as state ->
5454
Ci_job_status.job_success_or_pending ~bot_info (gh_owner, gh_repo)
55-
job_info ~github_repo_full_name ~gitlab_domain
56-
~gitlab_repo_full_name ~context ~state ~external_id
55+
job_info ~gitlab_domain ~gitlab_repo_full_name ~context ~state
56+
~external_id
5757
<&> Ci_documentation.send_doc_url ~bot_info job_info
5858
~github_repo_full_name
5959
| ("created" | "running") as state ->
6060
Ci_job_status.job_success_or_pending ~bot_info (gh_owner, gh_repo)
61-
job_info ~github_repo_full_name ~gitlab_domain
62-
~gitlab_repo_full_name ~context ~state ~external_id
61+
job_info ~gitlab_domain ~gitlab_repo_full_name ~context ~state
62+
~external_id
6363
| "cancelled" | "canceled" | "pending" ->
6464
(* Ideally we should check if a status was already reported for
6565
this job. But it is important to avoid making dozens of

src/actions_pr_sync.ml

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
open Base
22
open Bot_components
3-
open Bot_components.Bot_info
43
open Bot_components.GitHub_types
54
open Bot_components.GitHub_GitLab_sync
65
open Cohttp_lwt_unix
@@ -145,38 +144,25 @@ let update_pr ?full_ci ?(skip_author_check = false) ~bot_info
145144
(* Add rebase label if it exists *)
146145
GitHub_automation.add_labels_if_absent ~bot_info pr_info.issue [rebase_label] ;
147146
(* Add fail status check *)
148-
match bot_info.github_install_token with
149-
| None ->
150-
GitHub_mutations.send_status_check
151-
~repo_full_name:
152-
(f "%s/%s" pr_info.issue.issue.owner pr_info.issue.issue.repo)
153-
~commit:pr_info.head.sha ~state:"error" ~url:""
154-
~context:"GitLab CI pipeline (pull request)"
155-
~description:
156-
"Pipeline did not run on GitLab CI because PR has conflicts with \
157-
base branch."
158-
~bot_info
147+
let open Lwt.Infix in
148+
let open Lwt.Syntax in
149+
GitHub_queries.get_repository_id ~bot_info ~owner:pr_info.issue.issue.owner
150+
~repo:pr_info.issue.issue.repo
151+
>>= function
152+
| Ok repo_id ->
153+
(let+ _ =
154+
GitHub_mutations.create_check_run ~bot_info
155+
~name:"GitLab CI pipeline (pull request)" ~status:COMPLETED
156+
~repo_id ~head_sha:pr_info.head.sha ~conclusion:FAILURE
157+
~title:
158+
"Pipeline did not run on GitLab CI because PR has conflicts \
159+
with base branch."
160+
~details_url:"" ~summary:"" ()
161+
in
162+
() )
159163
|> Lwt_result.ok
160-
| Some _ -> (
161-
let open Lwt.Infix in
162-
let open Lwt.Syntax in
163-
GitHub_queries.get_repository_id ~bot_info
164-
~owner:pr_info.issue.issue.owner ~repo:pr_info.issue.issue.repo
165-
>>= function
166-
| Ok repo_id ->
167-
(let+ _ =
168-
GitHub_mutations.create_check_run ~bot_info
169-
~name:"GitLab CI pipeline (pull request)" ~status:COMPLETED
170-
~repo_id ~head_sha:pr_info.head.sha ~conclusion:FAILURE
171-
~title:
172-
"Pipeline did not run on GitLab CI because PR has conflicts \
173-
with base branch."
174-
~details_url:"" ~summary:"" ()
175-
in
176-
() )
177-
|> Lwt_result.ok
178-
| Error e ->
179-
Lwt.return (Error e) ) )
164+
| Error e ->
165+
Lwt.return (Error e) )
180166

181167
let run_ci_action ~bot_info ~comment_info ?full_ci ~gitlab_mapping
182168
~github_mapping () =

0 commit comments

Comments
 (0)