|
| 1 | +#' Check changed files for auto-approval eligibility. |
| 2 | +#' |
| 3 | +#' This function processes a list of changed files from a |
| 4 | +#' GitHub workflow, errors on any changes outside |
| 5 | +#' model-output directory, and passes model IDs to |
| 6 | +#' check_authorized_users for authorization validation. |
| 7 | +#' |
| 8 | +#' @param changed_files Character vector. List of changed |
| 9 | +#' file paths from the GitHub changed-files workflow output. |
| 10 | +#' @param gh_actor Character. GitHub username of the |
| 11 | +#' person making changes. |
| 12 | +#' @param base_hub_path Character. Path to the base hub |
| 13 | +#' directory. |
| 14 | +#' |
| 15 | +#' @return `NULL`, invisibly, raising an error if changes |
| 16 | +#' are outside model-output or if the user is unauthorized. |
| 17 | +#' |
| 18 | +#' @export |
| 19 | +check_changes_for_autoapproval <- function( |
| 20 | + changed_files, |
| 21 | + gh_actor, |
| 22 | + base_hub_path |
| 23 | +) { |
| 24 | + checkmate::assert_string(gh_actor) |
| 25 | + checkmate::assert_string(base_hub_path) |
| 26 | + checkmate::assert_character(changed_files) |
| 27 | + if (length(changed_files) < 1) { |
| 28 | + cli::cli_abort( |
| 29 | + "Empty PRs cannot be autoapproved. At least one file must be changed in the pull request." |
| 30 | + ) |
| 31 | + } |
| 32 | + changed_files_tbl <- tibble::tibble( |
| 33 | + full_path = changed_files |
| 34 | + ) |> |
| 35 | + dplyr::mutate( |
| 36 | + path_rel_root = fs::path_rel(.data$full_path, start = !!base_hub_path), |
| 37 | + in_model_output = fs::path_has_parent( |
| 38 | + .data$path_rel_root, |
| 39 | + "model-output" |
| 40 | + ), |
| 41 | + model_id = ifelse( |
| 42 | + .data$in_model_output, |
| 43 | + fs::path_dir(.data$path_rel_root) |> fs::path_file(), |
| 44 | + NA_character_ |
| 45 | + ) |
| 46 | + ) |
| 47 | + files_outside_model_output <- changed_files_tbl |> |
| 48 | + dplyr::filter(!.data$in_model_output) |> |
| 49 | + dplyr::pull(.data$full_path) |
| 50 | + |
| 51 | + if (length(files_outside_model_output) > 0) { |
| 52 | + cli::cli_abort( |
| 53 | + c( |
| 54 | + "Auto-approval failed: Changes detected outside 'model-output' directory.", |
| 55 | + "The following files are outside 'model-output':", |
| 56 | + files_outside_model_output |
| 57 | + ) |
| 58 | + ) |
| 59 | + } |
| 60 | + changed_model_ids <- changed_files_tbl |> |
| 61 | + dplyr::filter(.data$in_model_output) |> |
| 62 | + dplyr::pull(.data$model_id) |> |
| 63 | + unique() |
| 64 | + |
| 65 | + if (length(changed_model_ids) > 0) { |
| 66 | + cli::cli_inform( |
| 67 | + "Checking authorization for {length(changed_model_ids)} model director{?y/ies}: {.val {changed_model_ids}}" |
| 68 | + ) |
| 69 | + |
| 70 | + check_authorized_users( |
| 71 | + changed_dirs = changed_model_ids, |
| 72 | + gh_actor = gh_actor, |
| 73 | + base_hub_path = base_hub_path |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + invisible() |
| 78 | +} |
0 commit comments