From b40f707ba06c0ab5ccf3b3d7c33e35ac19a1a9fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Casta=C3=B1o=20Arteaga?= Date: Wed, 28 Oct 2026 17:59:14 +0100 Subject: [PATCH 1/2] Add optional label for PRs needing DCO attention MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergio Castaño Arteaga --- README.md | 35 ++ dco2/Cargo.toml | 1 + dco2/src/dco/event/mod.rs | 220 ++++++++++- dco2/src/dco/event/tests.rs | 657 ++++++++++++++++++++++++++++++++- dco2/src/github/client.rs | 494 ++++++++++++++++++++++++- dco2/src/github/event.rs | 142 +++++++ docs/config/dco.yml | 4 + docs/scripts/label-open-prs.sh | 206 +++++++++++ 8 files changed, 1739 insertions(+), 20 deletions(-) create mode 100755 docs/scripts/label-open-prs.sh diff --git a/README.md b/README.md index cd999ed..ffcdbe1 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,41 @@ allowOverrideAction: false > boundary because repository maintainers with write access can still change > the repository configuration. +### Labeling pull requests + +DCO2 can keep a label in sync with the DCO check result. When enabled, the label is added to open pull requests whose DCO check fails, and removed when the check passes (including when it passes thanks to remediation commits or the `Set DCO to pass` action). + +By default, this feature is **disabled**. To enable it, set the name of the label to use in the [configuration file](docs/config/dco.yml) (`.github/dco.yml`): + +```yaml +label: needs-dco +``` + +The label name can have up to 50 characters. Blank names, names longer than 50 characters, `.` and `..` are ignored, and labeling stays disabled. If the label does not exist in the repository, DCO2 creates it. Existing labels (including their colors and descriptions) are never modified. + +Labeling requires the `Pull requests: write` permission (`Issues: write` can be used instead, as it also allows managing labels): + +- When using the [public DCO2 application](https://github.com/apps/dco-2), the organization or account owner must approve the updated permissions requested by the application. +- When running your own instance of DCO2, add this permission to your GitHub App. No changes to the DCO2 server configuration are required. + +Approving this permission is optional. DCO checks keep working without it, but DCO2 will not label pull requests. It may take up to about an hour for labeling to start after approving the permission or updating the configuration file. + +Labeling is done on a best-effort basis: + +- Closed pull requests are never labeled or unlabeled. +- Disabling the option does not remove labels already applied. +- When several pull requests from forks share the same head commit, the `Set DCO to pass` action only removes the label from the pull request whose check run was used. The label is removed from the other pull requests on the next push, or it can be removed manually. +- Pull requests that were already open when labeling was enabled are labeled the next time new commits are pushed to them. To label them right away, you can use the [label-open-prs.sh](docs/scripts/label-open-prs.sh) sample script, which requires bash 4 or later and the [GitHub CLI](https://cli.github.com): + + ```text + docs/scripts/label-open-prs.sh --dry-run OWNER/REPO needs-dco + ``` + + The script labels the open pull requests whose latest DCO2 check failed. Remove `--dry-run` to apply the changes, and use `--app-slug` to set your GitHub App's slug when running your own instance of DCO2. Using the `Set DCO to pass` action on a pull request from a fork labeled by the script may not remove the label if its DCO check run does not reference the pull request; in that case, the label is removed on the next push, or it can be removed manually. + +> [!NOTE] +> When several commits are pushed in very quick succession, a stale label may occasionally remain on a pull request until the next push. + ## Deployment We'll soon provide some documentation about how to deploy your own instance of DCO2. diff --git a/dco2/Cargo.toml b/dco2/Cargo.toml index fc2e779..d6605c1 100644 --- a/dco2/Cargo.toml +++ b/dco2/Cargo.toml @@ -30,3 +30,4 @@ tracing = { workspace = true } indoc = { workspace = true } mockall = { workspace = true } pretty_assertions = { workspace = true } +tokio = { workspace = true, features = ["test-util"] } diff --git a/dco2/src/dco/event/mod.rs b/dco2/src/dco/event/mod.rs index 916dad9..6a6be5c 100644 --- a/dco2/src/dco/event/mod.rs +++ b/dco2/src/dco/event/mod.rs @@ -1,17 +1,23 @@ //! This module contains the logic to process GitHub webhook events. -use std::collections::HashSet; +use std::{ + collections::{BTreeSet, HashSet}, + time::Duration, +}; use anyhow::{Context, Result}; use askama::Template; use chrono::Utc; +use tokio::time::timeout; +use tracing::{debug, info, warn}; use crate::{ dco::check::{CheckInput, check}, github::{ CheckRun, CheckRunAction, CheckRunConclusion, CheckRunEvent, CheckRunEventAction, CheckRunStatus, - Commit, DynGHClient, Event, MergeGroupEvent, MergeGroupEventAction, NewCheckRunInput, - PullRequestEvent, PullRequestEventAction, + Commit, Config, Ctx, DynGHClient, Event, LabelsPermissionError, MergeGroupEvent, + MergeGroupEventAction, NewCheckRunInput, PullRequestEvent, PullRequestEventAction, PullRequestLabel, + is_rate_limit_error, }, }; @@ -42,6 +48,20 @@ const OVERRIDE_ACTION_DESCRIPTION: &str = "Manually set DCO check result to pass /// Summary of the override action. const OVERRIDE_ACTION_SUMMARY: &str = "Check result was manually set to passed"; +/// Maximum time the synchronization of the label of a pull request can take, +/// so that it does not delay the check run (e.g. when requests are retried). +const SYNC_LABEL_TIMEOUT: Duration = Duration::from_secs(3); + +/// Input used to synchronize the label of a pull request. When the event +/// includes the labels of the pull request, they are used to skip the +/// synchronization if no change is needed. +struct SyncLabelInput<'a> { + head_sha: &'a str, + labels: Option<&'a [PullRequestLabel]>, + pr_number: i64, + should_be_labeled: bool, +} + /// Process the GitHub webhook event provided, taking the appropriate action. pub async fn process_event(gh_client: DynGHClient, event: &Event) -> Result<()> { match event { @@ -79,6 +99,7 @@ async fn process_check_run_event(gh_client: DynGHClient, event: &CheckRunEvent) actions: vec![], completed_at: Utc::now(), conclusion: CheckRunConclusion::Success, + external_id: event.check_run.external_id.clone(), head_sha: event.check_run.head_sha.clone(), name: CHECK_NAME.to_string(), started_at, @@ -87,6 +108,33 @@ async fn process_check_run_event(gh_client: DynGHClient, event: &CheckRunEvent) title: OVERRIDE_ACTION_SUMMARY.to_string(), }); gh_client.create_check_run(&ctx, &check_run).await.context("error creating check run")?; + + // Remove the label from the pull requests associated with the check + // run: the one it was created for (external id) and those reported + // by GitHub that target this repository + let pr_numbers: BTreeSet = event + .check_run + .external_id + .iter() + .filter_map(|external_id| external_id.parse().ok()) + .chain( + event + .check_run + .pull_requests + .iter() + .filter(|pr| pr.base.repo.id == event.repository.id) + .map(|pr| pr.number), + ) + .collect(); + for pr_number in pr_numbers { + let input = SyncLabelInput { + head_sha: &event.check_run.head_sha, + labels: None, + pr_number, + should_be_labeled: false, + }; + sync_label(&gh_client, &ctx, &config, &input).await; + } } Ok(()) @@ -107,6 +155,7 @@ async fn process_merge_group_event(gh_client: DynGHClient, event: &MergeGroupEve actions: vec![], completed_at: Utc::now(), conclusion: CheckRunConclusion::Success, + external_id: None, head_sha: event.merge_group.head_commit.id.clone(), name: CHECK_NAME.to_string(), started_at, @@ -163,9 +212,10 @@ async fn process_pull_request_event(gh_client: DynGHClient, event: &PullRequestE members, }; let output = check(&input); + let check_failed = output.num_commits_with_errors > 0; - // Create check run - let (conclusion, title, actions) = if output.num_commits_with_errors == 0 { + // Prepare check run + let (conclusion, title, actions) = if !check_failed { (CheckRunConclusion::Success, CHECK_PASSED_TITLE, vec![]) } else { let actions = if output.config.override_action_is_allowed() { @@ -184,6 +234,7 @@ async fn process_pull_request_event(gh_client: DynGHClient, event: &PullRequestE actions, completed_at: Utc::now(), conclusion, + external_id: Some(event.number.to_string()), head_sha: event.pull_request.head.sha.clone(), name: CHECK_NAME.to_string(), started_at, @@ -191,6 +242,19 @@ async fn process_pull_request_event(gh_client: DynGHClient, event: &PullRequestE summary: output.render().context("error rendering output template")?, title: title.to_string(), }); + + // Add or remove the label depending on the check result. This is done + // before creating the check run so that the override action can't be + // used until the label has been synchronized + let input = SyncLabelInput { + head_sha: &event.pull_request.head.sha, + labels: Some(&event.pull_request.labels), + pr_number: event.number, + should_be_labeled: check_failed, + }; + sync_label(&gh_client, &ctx, &output.config, &input).await; + + // Create check run gh_client.create_check_run(&ctx, &check_run).await.context("error creating check run")?; Ok(()) @@ -233,3 +297,149 @@ async fn collect_members( Ok(members) } + +/// Find the label provided in the list of labels of a pull request, returning +/// the name of the applied label (label names are case-insensitive). +fn find_label<'a>(labels: impl IntoIterator, label: &str) -> Option<&'a str> { + let label_lowercase = label.to_lowercase(); + labels.into_iter().find(|name| name.to_lowercase() == label_lowercase) +} + +/// Check if the error provided was caused by the app lacking the permissions +/// required to manage labels. +fn is_labels_permission_error(err: &anyhow::Error) -> bool { + err.chain().any(|cause| cause.is::()) +} + +/// Synchronize the configured label of a pull request with the result of the +/// DCO check. This is a best-effort operation that must not prevent the check +/// run from being created, so errors are logged instead of returned. +async fn sync_label(gh_client: &DynGHClient, ctx: &Ctx, config: &Config, input: &SyncLabelInput<'_>) { + // Give up when the synchronization takes too long + let Ok(result) = timeout(SYNC_LABEL_TIMEOUT, try_sync_label(gh_client, ctx, config, input)).await else { + warn!( + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "label sync timed out" + ); + return; + }; + + // Log the error found, if any + if let Err(err) = result { + // Missing permissions are expected until the installation owner + // approves them (and until access tokens issued before are renewed) + if is_labels_permission_error(&err) { + info!( + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "permissions to manage labels not granted, skipping label sync" + ); + return; + } + + // Rate limits are logged separately to make them easier to spot + if is_rate_limit_error(&err) { + warn!( + ?err, + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "rate limited by GitHub, skipping label sync" + ); + return; + } + + warn!( + ?err, + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "error syncing label" + ); + } +} + +/// Synchronize the configured label of a pull request with the result of the +/// DCO check, returning any error found. +async fn try_sync_label( + gh_client: &DynGHClient, + ctx: &Ctx, + config: &Config, + input: &SyncLabelInput<'_>, +) -> Result<()> { + // Skip when labeling is disabled or the configured label is not valid + let Some(label) = config.label() else { + if config.label.is_some() { + warn!( + owner = %ctx.owner, + repo = %ctx.repo, + "invalid label configured, skipping label sync" + ); + } + return Ok(()); + }; + + // Skip when the labels included in the event show that no change is + // needed (the current labels are checked again before changing them) + if let Some(labels) = input.labels + && find_label(labels.iter().map(|l| l.name.as_str()), label).is_some() == input.should_be_labeled + { + return Ok(()); + } + + // Skip when the permissions required to manage labels are not granted + if !gh_client.can_manage_labels(ctx).await.context("error checking labels permissions")? { + info!( + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "permissions to manage labels not granted, skipping label sync" + ); + return Ok(()); + } + + // Get the current state of the pull request, skipping it when closed or + // when its head has moved (a newer event will handle the new commits) + let pr = gh_client + .get_pull_request(ctx, input.pr_number) + .await + .context("error getting pull request")?; + if !pr.is_open || pr.head_sha != input.head_sha { + debug!( + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + "pull request closed or head moved, skipping label sync" + ); + return Ok(()); + } + + // Add or remove the label when needed (the name of the applied label is + // used when removing it, as it may differ in case from the configured one) + let applied_label = find_label(pr.labels.iter().map(String::as_str), label); + match (input.should_be_labeled, applied_label) { + (true, None) => { + gh_client.add_label(ctx, input.pr_number, label).await.context("error adding label")?; + info!(owner = %ctx.owner, repo = %ctx.repo, pr_number = input.pr_number, label, "label added"); + } + (false, Some(applied_label)) => { + gh_client + .remove_label(ctx, input.pr_number, applied_label) + .await + .context("error removing label")?; + info!( + owner = %ctx.owner, + repo = %ctx.repo, + pr_number = input.pr_number, + label = applied_label, + "label removed" + ); + } + _ => {} + } + + Ok(()) +} diff --git a/dco2/src/dco/event/tests.rs b/dco2/src/dco/event/tests.rs index fe8bd00..41849db 100644 --- a/dco2/src/dco/event/tests.rs +++ b/dco2/src/dco/event/tests.rs @@ -2,35 +2,42 @@ use std::{future, sync::Arc}; use anyhow::{Ok, anyhow}; use indoc::indoc; -use mockall::predicate::eq; +use mockall::{Sequence, predicate::eq}; use crate::{ dco::{ event::{ CHECK_FAILED_TITLE, CHECK_NAME, CHECK_PASSED_TITLE, MERGE_GROUP_CHECKS_REQUESTED_SUMMARY, OVERRIDE_ACTION_DESCRIPTION, OVERRIDE_ACTION_IDENTIFIER, OVERRIDE_ACTION_LABEL, - OVERRIDE_ACTION_SUMMARY, + OVERRIDE_ACTION_SUMMARY, is_labels_permission_error, }, process_event, }, github::{ CheckRunAction, CheckRunConclusion, CheckRunEvent, CheckRunEventAction, CheckRunEventCheckRun, - CheckRunStatus, Commit, Config, ConfigRequire, Event, Installation, MergeGroupEvent, - MergeGroupEventAction, MergeGroupEventMergeGroup, MergeGroupHeadCommit, MockGHClient, Organization, - PullRequest, PullRequestBase, PullRequestEvent, PullRequestEventAction, PullRequestHead, Repository, - RepositoryOwner, RequestedAction, User, + CheckRunEventPullRequest, CheckRunEventPullRequestBase, CheckRunEventPullRequestRepository, + CheckRunStatus, Commit, Config, ConfigRequire, Ctx, Event, Installation, LabelsPermissionError, + MergeGroupEvent, MergeGroupEventAction, MergeGroupEventMergeGroup, MergeGroupHeadCommit, + MockGHClient, Organization, PullRequest, PullRequestBase, PullRequestEvent, PullRequestEventAction, + PullRequestHead, PullRequestLabel, PullRequestState, Repository, RepositoryOwner, RequestedAction, + User, }, }; +const LABEL: &str = "needs-dco"; + #[tokio::test] async fn check_run_event_other_action() { let event = CheckRunEvent { action: CheckRunEventAction::Other, check_run: CheckRunEventCheckRun { + external_id: None, head_sha: "head_sha".to_string(), + pull_requests: vec![], }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -49,10 +56,13 @@ async fn check_run_event_requested_action_unknown_identifier() { let event = CheckRunEvent { action: CheckRunEventAction::RequestedAction, check_run: CheckRunEventCheckRun { + external_id: None, head_sha: "head_sha".to_string(), + pull_requests: vec![], }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -74,10 +84,13 @@ async fn check_run_event_requested_action_override_error_creating_check_run() { let event = CheckRunEvent { action: CheckRunEventAction::RequestedAction, check_run: CheckRunEventCheckRun { + external_id: None, head_sha: "head_sha".to_string(), + pull_requests: vec![], }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -119,10 +132,13 @@ async fn check_run_event_requested_action_override_ignored_when_disabled() { let event = CheckRunEvent { action: CheckRunEventAction::RequestedAction, check_run: CheckRunEventCheckRun { + external_id: None, head_sha: "head_sha".to_string(), + pull_requests: vec![], }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -144,15 +160,166 @@ async fn check_run_event_requested_action_override_ignored_when_disabled() { process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); } +#[tokio::test] +async fn check_run_event_requested_action_override_label_disabled() { + let event = override_event(Some("42"), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, Config::default()); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_error_removing_label() { + let event = override_event(Some("42"), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &[LABEL]), + ); + gh_client + .expect_remove_label() + .with(eq(event.ctx()), eq(42), eq(LABEL)) + .times(1) + .returning(|_, _, _| Box::pin(future::ready(Err(anyhow!("test error"))))); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_ignores_other_repositories_pull_requests() { + let event = override_event(Some(""), &[(43, 99)]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_permission_not_granted() { + let event = override_event(Some("42"), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + expect_can_manage_labels(&mut gh_client, &event.ctx(), false); + gh_client.expect_get_pull_request().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_removed_from_associated_pull_requests() { + let event = override_event(Some("42"), &[(42, 20), (43, 20)]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + gh_client + .expect_can_manage_labels() + .with(eq(event.ctx())) + .times(2) + .returning(|_| Box::pin(future::ready(Ok(true)))); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &[LABEL]), + ); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 43, + pr_state("head_sha", true, &[LABEL]), + ); + expect_remove_label(&mut gh_client, &event.ctx(), 42, LABEL); + expect_remove_label(&mut gh_client, &event.ctx(), 43, LABEL); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_removed_using_external_id() { + let event = override_event(Some("42"), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &[LABEL]), + ); + expect_remove_label(&mut gh_client, &event.ctx(), 42, LABEL); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_removed_using_pull_requests() { + let event = override_event(Some(""), &[(43, 20)]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 43, + pr_state("head_sha", true, &[LABEL]), + ); + expect_remove_label(&mut gh_client, &event.ctx(), 43, LABEL); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_skipped_when_head_moved() { + let event = override_event(Some("42"), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("new_head_sha", true, &[LABEL]), + ); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + +#[tokio::test] +async fn check_run_event_requested_action_override_label_skipped_without_pull_requests() { + let event = override_event(Some(""), &[]); + let mut gh_client = MockGHClient::new(); + expect_override_check(&mut gh_client, &event, config_with_label()); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); +} + #[tokio::test] async fn check_run_event_requested_action_override_success() { let event = CheckRunEvent { action: CheckRunEventAction::RequestedAction, check_run: CheckRunEventCheckRun { + external_id: None, head_sha: "head_sha".to_string(), + pull_requests: vec![], }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -189,6 +356,18 @@ async fn check_run_event_requested_action_override_success() { process_event(Arc::new(gh_client), &Event::CheckRun(event)).await.unwrap(); } +#[test] +fn is_labels_permission_error_other_error() { + let err = anyhow!("test error").context("error adding label"); + assert!(!is_labels_permission_error(&err)); +} + +#[test] +fn is_labels_permission_error_permission_error_with_context() { + let err = anyhow::Error::new(LabelsPermissionError).context("error adding label"); + assert!(is_labels_permission_error(&err)); +} + #[tokio::test] async fn merge_group_other_action() { let event = MergeGroupEvent { @@ -200,6 +379,7 @@ async fn merge_group_other_action() { }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -224,6 +404,7 @@ async fn merge_group_checks_requested_error_creating_check_run() { }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -245,6 +426,7 @@ async fn merge_group_checks_requested_error_creating_check_run() { && check_run.status() == &CheckRunStatus::Completed && check_run.summary() == MERGE_GROUP_CHECKS_REQUESTED_SUMMARY && check_run.title() == MERGE_GROUP_CHECKS_REQUESTED_SUMMARY + && check_run.external_id().is_none() }) .times(1) .returning(|_, _| Box::pin(future::ready(Err(anyhow!("test error"))))); @@ -263,6 +445,7 @@ async fn merge_group_checks_requested_success() { }, installation: Installation { id: 1 }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -284,6 +467,7 @@ async fn merge_group_checks_requested_success() { && check_run.status() == &CheckRunStatus::Completed && check_run.summary() == MERGE_GROUP_CHECKS_REQUESTED_SUMMARY && check_run.title() == MERGE_GROUP_CHECKS_REQUESTED_SUMMARY + && check_run.external_id().is_none() }) .times(1) .returning(|_, _| Box::pin(future::ready(Ok(())))); @@ -291,11 +475,264 @@ async fn merge_group_checks_requested_success() { process_event(Arc::new(gh_client), &Event::MergeGroup(event)).await.unwrap(); } +#[tokio::test] +async fn pull_request_event_label_added_before_check_run_when_check_fails() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + let commit = commit_with_signoff(false); + gh_client + .expect_compare_commits() + .with(eq(event.ctx()), eq("base_sha"), eq("head_sha")) + .times(1) + .returning(move |_, _, _| Box::pin(future::ready(Ok(vec![commit.clone()])))); + gh_client + .expect_get_config() + .with(eq(event.ctx())) + .times(1) + .returning(|_| Box::pin(future::ready(Ok(Some(config_with_label()))))); + + let mut seq = Sequence::new(); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &["other"]), + ); + gh_client + .expect_add_label() + .with(eq(event.ctx()), eq(42), eq(LABEL)) + .times(1) + .in_sequence(&mut seq) + .returning(|_, _, _| Box::pin(future::ready(Ok(())))); + let expected_ctx = event.ctx(); + gh_client + .expect_create_check_run() + .withf(move |ctx, check_run| { + *ctx == expected_ctx + && check_run.conclusion() == &CheckRunConclusion::ActionRequired + && check_run.external_id() == Some("42") + }) + .times(1) + .in_sequence(&mut seq) + .returning(|_, _| Box::pin(future::ready(Ok(())))); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_disabled() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, Config::default(), false); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_error_adding_label() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request(&mut gh_client, &event.ctx(), 42, pr_state("head_sha", true, &[])); + gh_client + .expect_add_label() + .with(eq(event.ctx()), eq(42), eq(LABEL)) + .times(1) + .returning(|_, _, _| Box::pin(future::ready(Err(anyhow!("test error"))))); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_error_checking_permissions() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + gh_client + .expect_can_manage_labels() + .with(eq(event.ctx())) + .times(1) + .returning(|_| Box::pin(future::ready(Err(anyhow!("test error"))))); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_error_getting_pull_request() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + gh_client + .expect_get_pull_request() + .with(eq(event.ctx()), eq(42)) + .times(1) + .returning(|_, _| Box::pin(future::ready(Err(anyhow!("test error"))))); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_invalid() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + let config = Config { + label: Some(" ".to_string()), + ..Default::default() + }; + expect_pr_check(&mut gh_client, &event, config, false); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_kept_when_check_fails_and_label_applied() { + let event = pr_opened_event(&["Needs-DCO"]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_not_added_when_check_passes() { + let event = pr_opened_event(&["other"]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), true); + gh_client.expect_can_manage_labels().never(); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_not_added_when_label_applied_after_event() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &["Needs-DCO"]), + ); + gh_client.expect_add_label().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_not_removed_when_label_removed_after_event() { + let event = pr_opened_event(&["Needs-DCO"]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), true); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request(&mut gh_client, &event.ctx(), 42, pr_state("head_sha", true, &[])); + gh_client.expect_add_label().never(); + gh_client.expect_remove_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_permission_not_granted() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), false); + gh_client.expect_get_pull_request().never(); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_removed_when_check_passes() { + let event = pr_opened_event(&["Needs-DCO"]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), true); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("head_sha", true, &["Needs-DCO"]), + ); + expect_remove_label(&mut gh_client, &event.ctx(), 42, "Needs-DCO"); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_skipped_when_head_moved() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request( + &mut gh_client, + &event.ctx(), + 42, + pr_state("new_head_sha", true, &[]), + ); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test] +async fn pull_request_event_label_skipped_when_pull_request_closed() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + expect_get_pull_request(&mut gh_client, &event.ctx(), 42, pr_state("head_sha", false, &[])); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + +#[tokio::test(start_paused = true)] +async fn pull_request_event_label_sync_timed_out() { + let event = pr_opened_event(&[]); + let mut gh_client = MockGHClient::new(); + expect_pr_check(&mut gh_client, &event, config_with_label(), false); + expect_can_manage_labels(&mut gh_client, &event.ctx(), true); + gh_client + .expect_get_pull_request() + .with(eq(event.ctx()), eq(42)) + .times(1) + .returning(|_, _| Box::pin(future::pending())); + gh_client.expect_add_label().never(); + + process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); +} + #[tokio::test] async fn pull_request_event_other_action() { let event = PullRequestEvent { action: PullRequestEventAction::Other, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -307,8 +744,10 @@ async fn pull_request_event_other_action() { sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -327,6 +766,7 @@ async fn pull_request_event_opened_action_error_getting_pr_commits() { let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -338,8 +778,10 @@ async fn pull_request_event_opened_action_error_getting_pr_commits() { sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -363,6 +805,7 @@ async fn pull_request_event_opened_action_error_getting_repository_configuration let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -374,8 +817,10 @@ async fn pull_request_event_opened_action_error_getting_repository_configuration sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -424,6 +869,7 @@ async fn pull_request_event_opened_action_error_checking_user_organization_membe let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: Some(Organization { login: "org".to_string(), }), @@ -437,8 +883,10 @@ async fn pull_request_event_opened_action_error_checking_user_organization_membe sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -491,6 +939,7 @@ async fn pull_request_event_opened_action_error_creating_check_run() { let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -502,8 +951,10 @@ async fn pull_request_event_opened_action_error_creating_check_run() { sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -566,6 +1017,7 @@ async fn pull_request_event_opened_action_success_check_passed() { let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -577,8 +1029,10 @@ async fn pull_request_event_opened_action_success_check_passed() { sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -641,6 +1095,7 @@ async fn pull_request_event_opened_action_success_check_passed_author_is_member( let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: Some(Organization { login: "org".to_string(), }), @@ -654,8 +1109,10 @@ async fn pull_request_event_opened_action_success_check_passed_author_is_member( sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -722,6 +1179,7 @@ async fn pull_request_event_opened_action_success_check_failed_author_is_not_mem let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: Some(Organization { login: "org".to_string(), }), @@ -735,8 +1193,10 @@ async fn pull_request_event_opened_action_success_check_failed_author_is_not_mem sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -808,6 +1268,7 @@ async fn pull_request_event_opened_action_success_check_passed_author_is_bot_mem let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: Some(Organization { login: "org".to_string(), }), @@ -821,8 +1282,10 @@ async fn pull_request_event_opened_action_success_check_passed_author_is_bot_mem sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -885,6 +1348,7 @@ async fn pull_request_event_opened_action_success_check_passed_author_membership let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: Some(Organization { login: "org".to_string(), }), @@ -898,8 +1362,10 @@ async fn pull_request_event_opened_action_success_check_passed_author_membership sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -985,6 +1451,7 @@ async fn pull_request_event_opened_action_success_check_failed() { let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -996,8 +1463,10 @@ async fn pull_request_event_opened_action_success_check_failed() { sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -1065,6 +1534,7 @@ async fn pull_request_event_opened_action_success_check_failed_override_action_d let event = PullRequestEvent { action: PullRequestEventAction::Opened, installation: Installation { id: 1 }, + number: 1, organization: None, pull_request: PullRequest { base: PullRequestBase { @@ -1076,8 +1546,10 @@ async fn pull_request_event_opened_action_success_check_failed_override_action_d sha: "head_sha".to_string(), }, html_url: "url".to_string(), + labels: vec![], }, repository: Repository { + id: 1, name: "repo".to_string(), owner: RepositoryOwner { login: "owner".to_string(), @@ -1135,3 +1607,176 @@ async fn pull_request_event_opened_action_success_check_failed_override_action_d process_event(Arc::new(gh_client), &Event::PullRequest(event)).await.unwrap(); } + +fn commit_with_signoff(is_valid: bool) -> Commit { + let signoff = if is_valid { + "user1 " + } else { + "userx " + }; + Commit { + author: Some(User { + name: "user1".to_string(), + email: "user1@email.test".to_string(), + ..Default::default() + }), + committer: Some(User { + name: "user1".to_string(), + email: "user1@email.test".to_string(), + ..Default::default() + }), + message: format!("Test commit message\n\nSigned-off-by: {signoff}\n"), + ..Default::default() + } +} + +fn config_with_label() -> Config { + Config { + label: Some(LABEL.to_string()), + ..Default::default() + } +} + +fn expect_can_manage_labels(gh_client: &mut MockGHClient, ctx: &Ctx, can_manage_labels: bool) { + gh_client + .expect_can_manage_labels() + .with(eq(ctx.clone())) + .times(1) + .returning(move |_| Box::pin(future::ready(Ok(can_manage_labels)))); +} + +fn expect_get_pull_request(gh_client: &mut MockGHClient, ctx: &Ctx, number: i64, pr: PullRequestState) { + gh_client + .expect_get_pull_request() + .with(eq(ctx.clone()), eq(number)) + .times(1) + .returning(move |_, _| Box::pin(future::ready(Ok(pr.clone())))); +} + +fn expect_override_check(gh_client: &mut MockGHClient, event: &CheckRunEvent, config: Config) { + gh_client + .expect_get_config() + .with(eq(event.ctx())) + .times(1) + .returning(move |_| Box::pin(future::ready(Ok(Some(config.clone()))))); + let expected_ctx = event.ctx(); + let expected_external_id = event.check_run.external_id.clone(); + gh_client + .expect_create_check_run() + .withf(move |ctx, check_run| { + *ctx == expected_ctx + && check_run.conclusion() == &CheckRunConclusion::Success + && check_run.external_id() == expected_external_id.as_deref() + }) + .times(1) + .returning(|_, _| Box::pin(future::ready(Ok(())))); +} + +fn expect_pr_check(gh_client: &mut MockGHClient, event: &PullRequestEvent, config: Config, passes: bool) { + let commit = commit_with_signoff(passes); + gh_client + .expect_compare_commits() + .with(eq(event.ctx()), eq("base_sha"), eq("head_sha")) + .times(1) + .returning(move |_, _, _| Box::pin(future::ready(Ok(vec![commit.clone()])))); + gh_client + .expect_get_config() + .with(eq(event.ctx())) + .times(1) + .returning(move |_| Box::pin(future::ready(Ok(Some(config.clone()))))); + let expected_conclusion = if passes { + CheckRunConclusion::Success + } else { + CheckRunConclusion::ActionRequired + }; + let expected_ctx = event.ctx(); + let expected_external_id = event.number.to_string(); + gh_client + .expect_create_check_run() + .withf(move |ctx, check_run| { + *ctx == expected_ctx + && check_run.conclusion() == &expected_conclusion + && check_run.external_id() == Some(expected_external_id.as_str()) + }) + .times(1) + .returning(|_, _| Box::pin(future::ready(Ok(())))); +} + +fn expect_remove_label(gh_client: &mut MockGHClient, ctx: &Ctx, number: i64, name: &'static str) { + gh_client + .expect_remove_label() + .with(eq(ctx.clone()), eq(number), eq(name)) + .times(1) + .returning(|_, _, _| Box::pin(future::ready(Ok(())))); +} + +fn override_event(external_id: Option<&str>, pull_requests: &[(i64, i64)]) -> CheckRunEvent { + CheckRunEvent { + action: CheckRunEventAction::RequestedAction, + check_run: CheckRunEventCheckRun { + external_id: external_id.map(ToString::to_string), + head_sha: "head_sha".to_string(), + pull_requests: pull_requests + .iter() + .map(|(number, repo_id)| CheckRunEventPullRequest { + base: CheckRunEventPullRequestBase { + repo: CheckRunEventPullRequestRepository { id: *repo_id }, + }, + number: *number, + }) + .collect(), + }, + installation: Installation { id: 10 }, + repository: Repository { + id: 20, + name: "repo".to_string(), + owner: RepositoryOwner { + login: "owner".to_string(), + }, + }, + requested_action: Some(RequestedAction { + identifier: OVERRIDE_ACTION_IDENTIFIER.to_string(), + }), + } +} + +fn pr_opened_event(labels: &[&str]) -> PullRequestEvent { + PullRequestEvent { + action: PullRequestEventAction::Opened, + installation: Installation { id: 10 }, + number: 42, + organization: None, + pull_request: PullRequest { + base: PullRequestBase { + ref_: "base_ref".to_string(), + sha: "base_sha".to_string(), + }, + head: PullRequestHead { + ref_: "head_ref".to_string(), + sha: "head_sha".to_string(), + }, + html_url: "url".to_string(), + labels: labels + .iter() + .map(|name| PullRequestLabel { + name: name.to_string(), + }) + .collect(), + }, + repository: Repository { + id: 20, + name: "repo".to_string(), + owner: RepositoryOwner { + login: "owner".to_string(), + }, + }, + } +} + +fn pr_state(head_sha: &str, is_open: bool, labels: &[&str]) -> PullRequestState { + PullRequestState { + head_sha: head_sha.to_string(), + is_open, + labels: labels.iter().map(ToString::to_string).collect(), + } +} diff --git a/dco2/src/github/client.rs b/dco2/src/github/client.rs index 68917ea..52961e0 100644 --- a/dco2/src/github/client.rs +++ b/dco2/src/github/client.rs @@ -1,29 +1,62 @@ //! This module defines an abstraction layer over the GitHub API. -use std::sync::{Arc, Mutex, PoisonError}; +use std::{ + sync::{Arc, Mutex, PoisonError}, + time::Duration, +}; -use anyhow::{Result, bail}; +use anyhow::{Context, Result, bail}; use async_trait::async_trait; use base64::{Engine as _, engine::general_purpose::STANDARD as b64}; -use cached::{Cached, LruCache, cached}; +use cached::{Cached, LruCache, LruTtlCache, cached}; use chrono::{DateTime, Utc}; -use http::StatusCode; +use http::{StatusCode, header::RETRY_AFTER}; #[cfg(test)] use mockall::automock; use serde::{Deserialize, Serialize}; -use tracing::warn; +use thiserror::Error; +use tracing::{debug, warn}; /// Path of the configuration file in the repository. const CONFIG_FILE_PATH: &str = ".github/dco.yml"; +/// Color used when creating the label applied to pull requests. +const LABEL_COLOR: &str = "ededed"; + +/// Description used when creating the label applied to pull requests. +const LABEL_DESCRIPTION: &str = "DCO check requires attention"; + +/// Time during which the labels permissions of an installation are cached. +const LABELS_PERMISSIONS_TTL: Duration = Duration::from_secs(3600); + /// Maximum number of installation clients kept in the clients cache. const MAX_CACHED_CLIENTS: usize = 1000; +/// Maximum number of installations kept in the labels permissions cache. +const MAX_CACHED_LABELS_PERMISSIONS: usize = 1000; + +/// Message returned by GitHub when the app lacks the permissions required. +const PERMISSIONS_NOT_GRANTED_MESSAGE: &str = "Resource not accessible by integration"; + +/// Header used by GitHub to report the requests remaining in the rate limit. +const RATE_LIMIT_REMAINING_HEADER: &str = "x-ratelimit-remaining"; + +/// User agent used in requests to the GitHub API. +const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); + /// Abstraction layer over a GitHub client. This trait defines the methods that /// a GHClient implementation must provide. #[async_trait] #[cfg_attr(test, automock)] pub trait GHClient { + /// Add a label to an issue or pull request, creating it in the repository + /// when it does not exist. + async fn add_label(&self, ctx: &Ctx, issue_number: i64, name: &str) -> Result<()>; + + /// Check if the installation has been granted the permissions required to + /// manage labels. + async fn can_manage_labels(&self, ctx: &Ctx) -> Result; + /// Compare two commits. async fn compare_commits(&self, ctx: &Ctx, base_sha: &str, head_sha: &str) -> Result>; @@ -33,8 +66,15 @@ pub trait GHClient { /// Get configuration. async fn get_config(&self, ctx: &Ctx) -> Result>; + /// Get the current state of a pull request. + async fn get_pull_request(&self, ctx: &Ctx, number: i64) -> Result; + /// Check if a user is a member of the organization. async fn is_organization_member(&self, ctx: &Ctx, org: &str, login: &str) -> Result; + + /// Remove a label from an issue or pull request. Removing a label that is + /// not applied is not an error. + async fn remove_label(&self, ctx: &Ctx, issue_number: i64, name: &str) -> Result<()>; } /// Type alias to represent a GHClient trait object. @@ -43,10 +83,14 @@ pub type DynGHClient = Arc; /// GHClient implementation powered by octorust. #[derive(Clone)] pub struct GHClientOctorust { + /// Client authenticated as the GitHub App (used for app level endpoints). + app_client: octorust::Client, /// GitHub App credentials used to request installation tokens. app_credentials: octorust::auth::JWTCredentials, /// Clients cache, keyed by installation id. clients: Arc>>, + /// Labels permissions cache, keyed by installation id. + labels_permissions: Arc>>, /// GitHub API host override (e.g. for GitHub Enterprise Server). api_host: Option, @@ -59,17 +103,93 @@ impl GHClientOctorust { let private_key = pem::parse(&cfg.private_key)?.contents().to_owned(); let app_credentials = octorust::auth::JWTCredentials::new(cfg.app_id, private_key)?; - // Setup clients cache + // Setup app client + let credentials = octorust::auth::Credentials::JWT(app_credentials.clone()); + let mut app_client = octorust::Client::new(USER_AGENT, credentials)?; + if let Some(api_host) = &cfg.api_host { + app_client.with_host_override(api_host); + } + + // Setup caches let clients = LruCache::builder().max_size(MAX_CACHED_CLIENTS).build()?; + let labels_permissions = LruTtlCache::builder() + .max_size(MAX_CACHED_LABELS_PERMISSIONS) + .ttl(LABELS_PERMISSIONS_TTL) + .build()?; Ok(Self { + app_client, app_credentials, clients: Arc::new(Mutex::new(clients)), + labels_permissions: Arc::new(Mutex::new(labels_permissions)), api_host: cfg.api_host.clone(), }) } + /// Create the label provided in the repository. The error returned when + /// the label already exists (e.g. created concurrently) is ignored. + async fn create_label(&self, client: &octorust::Client, ctx: &Ctx, name: &str) -> Result<()> { + // Prepare label details + let body = octorust::types::IssuesCreateLabelRequest { + color: LABEL_COLOR.to_string(), + description: LABEL_DESCRIPTION.to_string(), + name: name.to_string(), + }; + + // Create label, ignoring the error returned when it already exists + match client.issues().create_label(&ctx.owner, &ctx.repo, &body).await { + Ok(_) => Ok(()), + Err(octorust::ClientError::HttpError { status, .. }) + if status == StatusCode::UNPROCESSABLE_ENTITY => + { + Ok(()) + } + Err(err) => Err(self.labels_error(ctx.inst_id, err)), + } + } + + /// Create the label provided in the repository when it does not exist. + /// Existing labels are never modified. + async fn ensure_label(&self, client: &octorust::Client, ctx: &Ctx, name: &str) -> Result<()> { + // Check if the label exists, creating it when it's not found + let encoded_name = encode_label_name(name); + match client.issues().get_label(&ctx.owner, &ctx.repo, &encoded_name).await { + Ok(_) => Ok(()), + Err(octorust::ClientError::HttpError { status, .. }) if status == StatusCode::NOT_FOUND => { + self.create_label(client, ctx, name).await.context("error creating label") + } + Err(err) => Err(self.labels_error(ctx.inst_id, err)).context("error getting label"), + } + } + + /// Remove the installation's client and labels permissions from their + /// caches, so that a new client (with a new access token) is set up and + /// the permissions are checked again on the next request. + fn evict_installation(&self, inst_id: i64) { + // Evict client + let mut clients = self.clients.lock().unwrap_or_else(PoisonError::into_inner); + clients.cache_remove(&inst_id); + drop(clients); + + // Evict labels permissions + let mut labels_permissions = self.labels_permissions.lock().unwrap_or_else(PoisonError::into_inner); + labels_permissions.cache_remove(&inst_id); + } + + /// Convert an error returned by a labels endpoint. When GitHub reports + /// that the app lacks the permissions required, the installation is + /// evicted from the caches, as its access token may predate their approval + /// or the cached permissions may no longer be granted. Other errors are + /// returned unchanged. + fn labels_error(&self, inst_id: i64, err: octorust::ClientError) -> anyhow::Error { + if is_permissions_error(&err) { + self.evict_installation(inst_id); + return anyhow::Error::new(LabelsPermissionError); + } + err.into() + } + /// Returns the GitHub client for the installation id provided. /// /// Clients are cached per installation so that their access token and HTTP @@ -84,12 +204,11 @@ impl GHClientOctorust { } // Setup credentials - let user_agent = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); let tg = octorust::auth::InstallationTokenGenerator::new(inst_id, self.app_credentials.clone()); let credentials = octorust::auth::Credentials::InstallationToken(tg); // Setup client - let mut client = octorust::Client::new(user_agent, credentials)?; + let mut client = octorust::Client::new(USER_AGENT, credentials)?; if let Some(api_host) = &self.api_host { client.with_host_override(api_host); } @@ -103,6 +222,61 @@ impl GHClientOctorust { #[async_trait] impl GHClient for GHClientOctorust { + /// [GHClient::add_label] + async fn add_label(&self, ctx: &Ctx, issue_number: i64, name: &str) -> Result<()> { + // Setup client for installation provided + let client = self.setup_client(ctx.inst_id)?; + + // Make sure the label exists, so that it's created with the color and + // description used by DCO2 + self.ensure_label(&client, ctx, name).await?; + + // Add label to the issue + let body = octorust::types::IssuesAddLabelsRequestOneOf::IssuesAddLabelsRequest( + octorust::types::IssuesAddLabelsRequest { + labels: vec![name.to_string()], + }, + ); + if let Err(err) = client.issues().add_labels(&ctx.owner, &ctx.repo, issue_number, &body).await { + return Err(self.labels_error(ctx.inst_id, err)); + } + + Ok(()) + } + + /// [GHClient::can_manage_labels] + async fn can_manage_labels(&self, ctx: &Ctx) -> Result { + // Reuse the installation's cached permissions when available + let cached_granted = self + .labels_permissions + .lock() + .unwrap_or_else(PoisonError::into_inner) + .cache_get(&ctx.inst_id) + .copied(); + if let Some(granted) = cached_granted { + return Ok(granted); + } + + // Get installation details (a response that cannot be parsed is + // handled as if the permissions were not granted) + let granted = match self.app_client.apps().get_installation(ctx.inst_id).await { + Ok(resp) => labels_permissions_granted(&resp.body.permissions), + Err(octorust::ClientError::SerdeJsonError(err)) => { + warn!(?err, inst_id = ctx.inst_id, "error parsing installation details"); + false + } + Err(err) => bail!(err), + }; + + // Cache permissions for subsequent requests + self.labels_permissions + .lock() + .unwrap_or_else(PoisonError::into_inner) + .cache_set(ctx.inst_id, granted); + + Ok(granted) + } + /// [GHClient::compare_commits] async fn compare_commits(&self, ctx: &Ctx, base_sha: &str, head_sha: &str) -> Result> { // Setup client for installation provided @@ -134,7 +308,7 @@ impl GHClient for GHClientOctorust { completed_at: Some(check_run.completed_at), conclusion: Some(check_run.conclusion.clone().into()), details_url: String::new(), - external_id: String::new(), + external_id: check_run.external_id.clone().unwrap_or_default(), head_sha: check_run.head_sha.clone(), name: check_run.name.clone(), output: Some(octorust::types::ChecksCreateRequestOutput { @@ -194,6 +368,21 @@ impl GHClient for GHClientOctorust { inner(&client, ctx).await } + /// [GHClient::get_pull_request] + async fn get_pull_request(&self, ctx: &Ctx, number: i64) -> Result { + // Setup client for installation provided + let client = self.setup_client(ctx.inst_id)?; + + // Get pull request + let pr = client.pulls().get(&ctx.owner, &ctx.repo, number).await?.body; + + Ok(PullRequestState { + head_sha: pr.head.sha, + is_open: pr.state == octorust::types::State::Open, + labels: pr.labels.into_iter().map(|label| label.name).collect(), + }) + } + /// [GHClient::is_organization_member] async fn is_organization_member(&self, ctx: &Ctx, org: &str, username: &str) -> Result { #[cached( @@ -227,6 +416,23 @@ impl GHClient for GHClientOctorust { inner(&client, org, username).await } + + /// [GHClient::remove_label] + async fn remove_label(&self, ctx: &Ctx, issue_number: i64, name: &str) -> Result<()> { + // Setup client for installation provided + let client = self.setup_client(ctx.inst_id)?; + + // Remove label from the issue + let encoded_name = encode_label_name(name); + match client.issues().remove_label(&ctx.owner, &ctx.repo, issue_number, &encoded_name).await { + Ok(_) => Ok(()), + Err(octorust::ClientError::HttpError { status, .. }) if status == StatusCode::NOT_FOUND => { + debug!(owner = %ctx.owner, repo = %ctx.repo, issue_number, name, "label to remove not found"); + Ok(()) + } + Err(err) => Err(self.labels_error(ctx.inst_id, err)), + } + } } /// GitHub application configuration. @@ -245,6 +451,7 @@ pub struct CheckRun { actions: Vec, completed_at: DateTime, conclusion: CheckRunConclusion, + external_id: Option, head_sha: String, name: String, started_at: DateTime, @@ -261,6 +468,7 @@ impl CheckRun { actions: input.actions, completed_at: input.completed_at, conclusion: input.conclusion, + external_id: input.external_id, head_sha: input.head_sha, name: input.name, started_at: input.started_at, @@ -321,6 +529,12 @@ impl CheckRun { &self.conclusion } + /// Get the external id of the check run (the pull request number, when + /// the check run was created for a pull request). + pub fn external_id(&self) -> Option<&str> { + self.external_id.as_deref() + } + /// Get the head SHA of the check run. pub fn head_sha(&self) -> &str { &self.head_sha @@ -464,6 +678,7 @@ pub const DEFAULT_THIRD_PARTY_REMEDIATION_COMMITS_ALLOWED: bool = false; pub struct Config { pub allow_override_action: Option, pub allow_remediation_commits: Option, + pub label: Option, pub require: Option, } @@ -472,6 +687,7 @@ impl Default for Config { Self { allow_override_action: Some(DEFAULT_OVERRIDE_ACTION_ALLOWED), allow_remediation_commits: Some(ConfigAllowRemediationCommits::default()), + label: None, require: Some(ConfigRequire::default()), } } @@ -489,6 +705,20 @@ impl Config { } } + /// Get the label to apply to pull requests whose DCO check requires + /// attention. Returns `None` when labeling is disabled or the configured + /// name is not valid (empty, longer than 50 characters, or a dot segment, + /// which can't be used in the path of label requests). + pub fn label(&self) -> Option<&str> { + const MAX_LABEL_LENGTH: usize = 50; + + let label = self.label.as_deref()?.trim(); + if label.is_empty() || label.chars().count() > MAX_LABEL_LENGTH || label == "." || label == ".." { + return None; + } + Some(label) + } + /// Check if third party remediation commits are allowed. pub fn third_party_remediation_commits_are_allowed(&self) -> bool { if let Some(allow_remediation_commits) = &self.allow_remediation_commits { @@ -575,12 +805,19 @@ impl User { } } +/// Error returned when the app lacks the permissions required to manage +/// labels (e.g. the installation owner hasn't approved them yet). +#[derive(Error, Debug, Clone, PartialEq)] +#[error("permissions to manage labels not granted")] +pub struct LabelsPermissionError; + /// Input used to create a new check run. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct NewCheckRunInput { pub actions: Vec, pub completed_at: DateTime, pub conclusion: CheckRunConclusion, + pub external_id: Option, pub head_sha: String, pub name: String, pub started_at: DateTime, @@ -588,3 +825,242 @@ pub struct NewCheckRunInput { pub summary: String, pub title: String, } + +/// Current state of a pull request. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +pub struct PullRequestState { + pub head_sha: String, + pub is_open: bool, + pub labels: Vec, +} + +/// Check if the error provided was caused by GitHub rate limiting the requests +/// (primary or secondary rate limits). +pub fn is_rate_limit_error(err: &anyhow::Error) -> bool { + err.chain() + .filter_map(|cause| cause.downcast_ref::()) + .any(|err| match err { + octorust::ClientError::RateLimited { .. } => true, + octorust::ClientError::HttpError { + status, + headers, + error, + } => { + *status == StatusCode::TOO_MANY_REQUESTS + || (*status == StatusCode::FORBIDDEN + && (headers.contains_key(RETRY_AFTER) + || headers.get(RATE_LIMIT_REMAINING_HEADER).is_some_and(|v| v == "0") + || error.to_lowercase().contains("rate limit"))) + } + _ => false, + }) +} + +/// Encode the characters of a label name that octorust does not encode when +/// building path segments and that would change the request path (`%`, `/` +/// and `\`). Dot segments (`.` and `..`) are rejected by `Config::label`. +fn encode_label_name(name: &str) -> String { + name.replace('%', "%25").replace('/', "%2F").replace('\\', "%5C") +} + +/// Check if the error provided was returned because the app lacks the +/// permissions required. Other 403 responses (e.g. secondary rate limits) are +/// not considered permissions errors. +fn is_permissions_error(err: &octorust::ClientError) -> bool { + matches!( + err, + octorust::ClientError::HttpError { status, error, .. } + if *status == StatusCode::FORBIDDEN && error.contains(PERMISSIONS_NOT_GRANTED_MESSAGE) + ) +} + +/// Check if the permissions provided allow managing labels (write access to +/// pull requests or issues). +fn labels_permissions_granted(permissions: &octorust::types::AppPermissions) -> bool { + let is_write = |p: &Option| matches!(p, Some(octorust::types::Pages::Write)); + is_write(&permissions.pull_requests) || is_write(&permissions.issues) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn config_label_absent() { + let config = Config::default(); + assert_eq!(config.label(), None); + } + + #[test] + fn config_label_blank() { + let config = config_with_label(" "); + assert_eq!(config.label(), None); + } + + #[test] + fn config_label_dot_segment() { + assert_eq!(config_with_label(".").label(), None); + assert_eq!(config_with_label("..").label(), None); + } + + #[test] + fn config_label_max_length() { + let label = "a".repeat(50); + let config = config_with_label(&label); + assert_eq!(config.label(), Some(label.as_str())); + } + + #[test] + fn config_label_too_long() { + let config = config_with_label(&"a".repeat(51)); + assert_eq!(config.label(), None); + } + + #[test] + fn config_label_trimmed() { + let config = config_with_label(" needs-dco "); + assert_eq!(config.label(), Some("needs-dco")); + } + + #[test] + fn config_label_yaml() { + let config: Config = serde_yaml::from_str("label: needs-dco").unwrap(); + assert_eq!(config.label(), Some("needs-dco")); + } + + #[test] + fn encode_label_name_encodes_path_characters() { + assert_eq!( + encode_label_name("do-not-merge/dco%\\"), + "do-not-merge%2Fdco%25%5C" + ); + } + + #[test] + fn encode_label_name_keeps_other_characters() { + assert_eq!(encode_label_name("needs dco"), "needs dco"); + } + + #[test] + fn is_permissions_error_not_found() { + let err = http_error(StatusCode::NOT_FOUND, "Not Found"); + assert!(!is_permissions_error(&err)); + } + + #[test] + fn is_permissions_error_permissions_not_granted() { + let err = http_error( + StatusCode::FORBIDDEN, + r#"{"message":"Resource not accessible by integration","status":"403"}"#, + ); + assert!(is_permissions_error(&err)); + } + + #[test] + fn is_permissions_error_secondary_rate_limit() { + let err = http_error( + StatusCode::FORBIDDEN, + r#"{"message":"You have exceeded a secondary rate limit","status":"403"}"#, + ); + assert!(!is_permissions_error(&err)); + } + + #[test] + fn is_rate_limit_error_forbidden_with_rate_limit_message() { + let err = http_error(StatusCode::FORBIDDEN, "You have exceeded a secondary rate limit."); + assert!(is_rate_limit_error( + &anyhow::Error::new(err).context("error adding label") + )); + } + + #[test] + fn is_rate_limit_error_forbidden_with_rate_limit_remaining_zero() { + let err = http_error_with_header(StatusCode::FORBIDDEN, RATE_LIMIT_REMAINING_HEADER, "0"); + assert!(is_rate_limit_error(&anyhow::Error::new(err))); + } + + #[test] + fn is_rate_limit_error_forbidden_with_retry_after_header() { + let err = http_error_with_header(StatusCode::FORBIDDEN, RETRY_AFTER.as_str(), "60"); + assert!(is_rate_limit_error(&anyhow::Error::new(err))); + } + + #[test] + fn is_rate_limit_error_forbidden_without_rate_limit_details() { + let err = http_error(StatusCode::FORBIDDEN, PERMISSIONS_NOT_GRANTED_MESSAGE); + assert!(!is_rate_limit_error(&anyhow::Error::new(err))); + } + + #[test] + fn is_rate_limit_error_other_error() { + assert!(!is_rate_limit_error(&anyhow::anyhow!("test error"))); + } + + #[test] + fn is_rate_limit_error_primary_rate_limit() { + let err = octorust::ClientError::RateLimited { duration: 60 }; + assert!(is_rate_limit_error(&anyhow::Error::new(err))); + } + + #[test] + fn is_rate_limit_error_too_many_requests() { + let err = http_error(StatusCode::TOO_MANY_REQUESTS, ""); + assert!(is_rate_limit_error(&anyhow::Error::new(err))); + } + + #[test] + fn labels_permissions_granted_issues_write() { + let permissions = app_permissions(r#"{"issues": "write", "metadata": "read"}"#); + assert!(labels_permissions_granted(&permissions)); + } + + #[test] + fn labels_permissions_granted_not_granted() { + let permissions = app_permissions(r#"{"checks": "write", "metadata": "read"}"#); + assert!(!labels_permissions_granted(&permissions)); + } + + #[test] + fn labels_permissions_granted_pull_requests_write() { + let permissions = app_permissions(r#"{"metadata": "read", "pull_requests": "write"}"#); + assert!(labels_permissions_granted(&permissions)); + } + + #[test] + fn labels_permissions_granted_read_only() { + let permissions = app_permissions(r#"{"issues": "read", "pull_requests": "read"}"#); + assert!(!labels_permissions_granted(&permissions)); + } + + fn app_permissions(json: &str) -> octorust::types::AppPermissions { + serde_json::from_str(json).unwrap() + } + + fn config_with_label(label: &str) -> Config { + Config { + label: Some(label.to_string()), + ..Default::default() + } + } + + fn http_error(status: StatusCode, error: &str) -> octorust::ClientError { + octorust::ClientError::HttpError { + status, + headers: http::HeaderMap::new(), + error: error.to_string(), + } + } + fn http_error_with_header( + status: StatusCode, + name: &'static str, + value: &'static str, + ) -> octorust::ClientError { + let mut headers = http::HeaderMap::new(); + headers.insert(name, http::HeaderValue::from_static(value)); + octorust::ClientError::HttpError { + status, + headers, + error: String::new(), + } + } +} diff --git a/dco2/src/github/event.rs b/dco2/src/github/event.rs index 8da808b..48dd46b 100644 --- a/dco2/src/github/event.rs +++ b/dco2/src/github/event.rs @@ -96,7 +96,29 @@ pub enum CheckRunEventAction { /// Check run event check run details. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct CheckRunEventCheckRun { + pub external_id: Option, pub head_sha: String, + #[serde(default)] + pub pull_requests: Vec, +} + +/// Check run event pull request details. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct CheckRunEventPullRequest { + pub base: CheckRunEventPullRequestBase, + pub number: i64, +} + +/// Check run event pull request base details. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct CheckRunEventPullRequestBase { + pub repo: CheckRunEventPullRequestRepository, +} + +/// Check run event pull request repository details. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct CheckRunEventPullRequestRepository { + pub id: i64, } /// GitHub application installation information. @@ -158,6 +180,8 @@ pub struct PullRequest { pub base: PullRequestBase, pub head: PullRequestHead, pub html_url: String, + #[serde(default)] + pub labels: Vec, } /// Pull request base information. @@ -173,6 +197,7 @@ pub struct PullRequestBase { pub struct PullRequestEvent { pub action: PullRequestEventAction, pub installation: Installation, + pub number: i64, pub organization: Option, pub pull_request: PullRequest, pub repository: Repository, @@ -207,9 +232,16 @@ pub struct PullRequestHead { pub sha: String, } +/// Pull request label information. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct PullRequestLabel { + pub name: String, +} + /// Repository information. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Repository { + pub id: i64, pub name: String, pub owner: RepositoryOwner, } @@ -225,3 +257,113 @@ pub struct RepositoryOwner { pub struct RequestedAction { pub identifier: String, } + +#[cfg(test)] +mod tests { + use http::HeaderValue; + + use super::*; + + #[test] + fn check_run_event_with_external_id_and_pull_requests() { + let body = r#"{ + "action": "requested_action", + "check_run": { + "external_id": "123", + "head_sha": "head_sha", + "pull_requests": [{"base": {"repo": {"id": 1}}, "id": 1, "number": 123}] + }, + "installation": {"id": 1}, + "repository": {"id": 1, "name": "repo", "owner": {"login": "owner"}}, + "requested_action": {"identifier": "override"} + }"#; + + let Event::CheckRun(event) = parse_event("check_run", body) else { + panic!("unexpected event type"); + }; + + assert_eq!(event.check_run.external_id.as_deref(), Some("123")); + assert_eq!( + event.check_run.pull_requests, + vec![CheckRunEventPullRequest { + base: CheckRunEventPullRequestBase { + repo: CheckRunEventPullRequestRepository { id: 1 }, + }, + number: 123, + }] + ); + } + + #[test] + fn check_run_event_without_external_id_and_pull_requests() { + let body = r#"{ + "action": "requested_action", + "check_run": {"external_id": "", "head_sha": "head_sha"}, + "installation": {"id": 1}, + "repository": {"id": 1, "name": "repo", "owner": {"login": "owner"}}, + "requested_action": {"identifier": "override"} + }"#; + + let Event::CheckRun(event) = parse_event("check_run", body) else { + panic!("unexpected event type"); + }; + + assert_eq!(event.check_run.external_id.as_deref(), Some("")); + assert!(event.check_run.pull_requests.is_empty()); + } + + #[test] + fn pull_request_event_with_labels() { + let body = r#"{ + "action": "opened", + "installation": {"id": 1}, + "number": 123, + "pull_request": { + "base": {"ref": "main", "sha": "base_sha"}, + "head": {"ref": "feature", "sha": "head_sha"}, + "html_url": "url", + "labels": [{"id": 1, "name": "needs-dco", "color": "d73a4a"}] + }, + "repository": {"id": 1, "name": "repo", "owner": {"login": "owner"}} + }"#; + + let Event::PullRequest(event) = parse_event("pull_request", body) else { + panic!("unexpected event type"); + }; + + assert_eq!( + event.pull_request.labels, + vec![PullRequestLabel { + name: "needs-dco".to_string() + }] + ); + } + + #[test] + fn pull_request_event_with_number() { + let body = r#"{ + "action": "opened", + "installation": {"id": 1}, + "number": 123, + "pull_request": { + "base": {"ref": "main", "sha": "base_sha"}, + "head": {"ref": "feature", "sha": "head_sha"}, + "html_url": "url" + }, + "repository": {"id": 1, "name": "repo", "owner": {"login": "owner"}} + }"#; + + let Event::PullRequest(event) = parse_event("pull_request", body) else { + panic!("unexpected event type"); + }; + + assert_eq!(event.number, 123); + assert!(event.pull_request.labels.is_empty()); + } + + fn parse_event(name: &'static str, body: &str) -> Event { + let mut headers = HeaderMap::new(); + headers.insert(EVENT_NAME_HEADER, HeaderValue::from_static(name)); + Event::try_from((&headers, &Bytes::from(body.to_string()))).unwrap() + } +} diff --git a/docs/config/dco.yml b/docs/config/dco.yml index 17f37d7..29fed7a 100644 --- a/docs/config/dco.yml +++ b/docs/config/dco.yml @@ -17,6 +17,10 @@ allowRemediationCommits: # https://github.com/cncf/dco2?#third-party thirdParty: false +# Label to add to pull requests with a failing DCO check (disabled by default) +# https://github.com/cncf/dco2?#labeling-pull-requests +# label: needs-dco + require: # Members are required to sign-off commits # https://github.com/cncf/dco2?#skipping-sign-off-for-organization-members diff --git a/docs/scripts/label-open-prs.sh b/docs/scripts/label-open-prs.sh new file mode 100755 index 0000000..5ef3b68 --- /dev/null +++ b/docs/scripts/label-open-prs.sh @@ -0,0 +1,206 @@ +#!/usr/bin/env bash +# +# Sample script to label open pull requests whose DCO2 check is failing. +# +# DCO2 only labels pull requests when it processes new events for them, so this +# script can be used to label the pull requests that were already open when the +# `label` option was enabled. See the "Labeling pull requests" section in the +# README for more details. +# +# Requirements: bash 4 or later and the GitHub CLI (https://cli.github.com), +# authenticated with access to the repository (`gh auth login`). Labels with +# non-ASCII characters also require a UTF-8 locale. + +set -euo pipefail + +if ((BASH_VERSINFO[0] < 4)); then + echo "error: bash 4 or later is required" >&2 + exit 1 +fi + +usage() { + cat <&2 + exit 1 +} + +is_ascii() { + ! LC_ALL=C grep -q '[^[:print:]]' <<<"$1" +} + +# Parse arguments +app_slug="dco-2" +dry_run=false +positional=() +while [[ $# -gt 0 ]]; do + case "$1" in + --app-slug) + [[ $# -ge 2 ]] || die "--app-slug requires a value" + app_slug="$2" + shift 2 + ;; + --dry-run) + dry_run=true + shift + ;; + -h | --help) + usage + exit 0 + ;; + --) + shift + positional+=("$@") + break + ;; + -*) + usage >&2 + die "unknown option: $1" + ;; + *) + positional+=("$1") + shift + ;; + esac +done +if [[ ${#positional[@]} -ne 2 ]]; then + usage >&2 + exit 1 +fi +repo="${positional[0]}" +label="${positional[1]}" + +# Validate arguments +[[ "$repo" =~ ^[^/[:space:]]+/[^/[:space:]]+$ ]] || die "invalid repository: $repo" +[[ "$app_slug" =~ ^[A-Za-z0-9_-]+$ ]] || die "invalid app slug: $app_slug" +if ! is_ascii "$label" && [[ "$(locale charmap 2>/dev/null)" != "UTF-8" ]]; then + die "labels with non-ASCII characters require a UTF-8 locale" +fi +label="${label#"${label%%[![:space:]]*}"}" +label="${label%"${label##*[![:space:]]}"}" +[[ -n "$label" && ${#label} -le 50 ]] || die "the label must have 1-50 characters" +[[ "$label" != "." && "$label" != ".." ]] || die "invalid label: $label" +command -v gh >/dev/null 2>&1 || die "the GitHub CLI (gh) is required" + +label_lc="${label,,}" +tab=$'\t' + +# Check if the label exists in the repository (case-insensitive) +label_exists() { + local names + names="$(gh api --paginate "repos/$repo/labels?per_page=100" --jq '.[].name' &2 + failed=$((failed + 1)) + continue + fi + if [[ "$conclusion" != "action_required" ]]; then + if [[ "$conclusion" == "none" ]]; then + echo "#$number: skipped (no DCO check run from $app_slug)" + else + echo "#$number: skipped (DCO check: $conclusion)" + fi + skipped=$((skipped + 1)) + continue + fi + + # Label pull request + ensure_label + if [[ "$dry_run" == true ]]; then + echo "#$number: would add label \"$label\"" + labeled=$((labeled + 1)) + continue + fi + if gh api --method POST "repos/$repo/issues/$number/labels" \ + -f "labels[]=$label" --silent &2 + failed=$((failed + 1)) + fi + + # Pause between writes to respect GitHub's secondary rate limits + sleep 1 +done 3<<<"$prs" + +# Print summary +if [[ "$dry_run" == true ]]; then + labeled_msg="$labeled would be labeled" +else + labeled_msg="$labeled labeled" +fi +echo "Done: $checked checked, $labeled_msg, $skipped skipped, $failed failed" +[[ $failed -eq 0 ]] From 469bfccc1f481aa10484a024329a86d70135f70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Casta=C3=B1o=20Arteaga?= Date: Fri, 30 Oct 2026 10:47:44 +0100 Subject: [PATCH 2/2] Improve script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergio Castaño Arteaga --- docs/scripts/label-open-prs.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/scripts/label-open-prs.sh b/docs/scripts/label-open-prs.sh index 5ef3b68..f7545d4 100755 --- a/docs/scripts/label-open-prs.sh +++ b/docs/scripts/label-open-prs.sh @@ -155,17 +155,26 @@ while IFS="$tab" read -r number sha labels <&3; do # Get the conclusion of the latest DCO check run created by the DCO2 app for # this pull request (check runs reference it in their external id, and those - # with an empty external id apply to any pull request) - if ! conclusion="$(gh api \ - "repos/$repo/commits/$sha/check-runs?check_name=DCO&filter=latest&per_page=100" \ - --jq "[.check_runs[] | select(.app.slug == \"$app_slug\") - | select((.external_id // \"\") as \$id | \$id == \"\" or \$id == \"$number\")] - | max_by(.id) | if . == null then \"none\" else (.conclusion // .status) end" \ + # with an empty external id apply to any pull request). All check runs are + # requested because `filter=latest` only returns the latest one, which may + # belong to another pull request with the same head commit, and the latest is + # selected after fetching all pages because `--jq` is applied to each page. + # + # Check runs created by older DCO2 versions have an empty external id, so when + # several pull requests share the same head commit and only have those, the + # latest one may belong to another pull request + if ! runs="$(gh api --paginate \ + "repos/$repo/commits/$sha/check-runs?check_name=DCO&filter=all&per_page=100" \ + --jq ".check_runs[] | select(.app.slug == \"$app_slug\") + | select((.external_id // \"\") as \$id | \$id == \"\" or \$id == \"$number\") + | \"\(.id)\t\(.conclusion // .status)\"" \ &2 failed=$((failed + 1)) continue fi + conclusion="$(sort -n <<<"$runs" | tail -n 1 | cut -f 2)" + conclusion="${conclusion:-none}" if [[ "$conclusion" != "action_required" ]]; then if [[ "$conclusion" == "none" ]]; then echo "#$number: skipped (no DCO check run from $app_slug)"