|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This software may be used and distributed according to the terms of the |
| 5 | + * GNU General Public License version 2. |
| 6 | + */ |
| 7 | + |
| 8 | +use anyhow::Result; |
| 9 | +use async_trait::async_trait; |
| 10 | +use blobstore::StoreLoadable; |
| 11 | +use bookmarks::BookmarkKey; |
| 12 | +use context::CoreContext; |
| 13 | +use hook_manager::ChangesetHook; |
| 14 | +use hook_manager::HookRejectionInfo; |
| 15 | +use manifest::ManifestOps; |
| 16 | +use mononoke_types::BonsaiChangeset; |
| 17 | +use repo_blobstore::RepoBlobstoreRef; |
| 18 | +use repo_derived_data::RepoDerivedDataRef; |
| 19 | +use serde::Deserialize; |
| 20 | +use skeleton_manifest::RootSkeletonManifestId; |
| 21 | + |
| 22 | +use crate::CrossRepoPushSource; |
| 23 | +use crate::HookConfig; |
| 24 | +use crate::HookExecution; |
| 25 | +use crate::HookRepo; |
| 26 | +use crate::PushAuthoredBy; |
| 27 | + |
| 28 | +#[derive(Deserialize, Clone, Debug)] |
| 29 | +pub struct LimitSubtreeOpSizeConfig { |
| 30 | + #[serde(default)] |
| 31 | + source_file_count_limit: Option<u64>, |
| 32 | + |
| 33 | + #[serde(default)] |
| 34 | + dest_min_path_depth: Option<usize>, |
| 35 | + |
| 36 | + /// Message to use when rejecting a commit because the source directory is too large. |
| 37 | + /// |
| 38 | + /// The following variables are available: |
| 39 | + /// - ${dest_path}: The path of the destination. |
| 40 | + /// - ${source}: The changeset id of the source. |
| 41 | + /// - ${source_path}: The path of the source. |
| 42 | + /// - ${count}: The number of files in the source. |
| 43 | + /// - ${limit}: The limit on the number of files in the source. |
| 44 | + too_many_files_rejection_message: String, |
| 45 | + |
| 46 | + /// Message to use when rejecting a commit because the destination path is too shallow. |
| 47 | + /// |
| 48 | + /// The following variables are available: |
| 49 | + /// - ${dest_path}: The path of the destination. |
| 50 | + /// - ${dest_min_path_depth}: The minimum depth of the destination path. |
| 51 | + dest_too_shallow_rejection_message: String, |
| 52 | +} |
| 53 | + |
| 54 | +/// Hook to limit the size of subtree operations. |
| 55 | +pub struct LimitSubtreeOpSizeHook { |
| 56 | + config: LimitSubtreeOpSizeConfig, |
| 57 | +} |
| 58 | + |
| 59 | +impl LimitSubtreeOpSizeHook { |
| 60 | + pub fn new(config: &HookConfig) -> Result<Self> { |
| 61 | + Self::with_config(config.parse_options()?) |
| 62 | + } |
| 63 | + |
| 64 | + pub fn with_config(config: LimitSubtreeOpSizeConfig) -> Result<Self> { |
| 65 | + Ok(Self { config }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[async_trait] |
| 70 | +impl ChangesetHook for LimitSubtreeOpSizeHook { |
| 71 | + async fn run<'this: 'cs, 'ctx: 'this, 'cs, 'repo: 'cs>( |
| 72 | + &'this self, |
| 73 | + ctx: &'ctx CoreContext, |
| 74 | + hook_repo: &'repo HookRepo, |
| 75 | + _bookmark: &BookmarkKey, |
| 76 | + changeset: &'cs BonsaiChangeset, |
| 77 | + cross_repo_push_source: CrossRepoPushSource, |
| 78 | + push_authored_by: PushAuthoredBy, |
| 79 | + ) -> Result<HookExecution> { |
| 80 | + if push_authored_by.service() { |
| 81 | + return Ok(HookExecution::Accepted); |
| 82 | + } |
| 83 | + if cross_repo_push_source == CrossRepoPushSource::PushRedirected { |
| 84 | + // For push-redirected commits, we rely on running source-repo hooks |
| 85 | + return Ok(HookExecution::Accepted); |
| 86 | + } |
| 87 | + |
| 88 | + for (path, change) in changeset.subtree_changes() { |
| 89 | + if let Some(source_file_count_limit) = self.config.source_file_count_limit { |
| 90 | + if let Some((source, source_path)) = change.change_source() { |
| 91 | + let source_root = hook_repo |
| 92 | + .repo_derived_data() |
| 93 | + .derive::<RootSkeletonManifestId>(ctx, source) |
| 94 | + .await? |
| 95 | + .into_skeleton_manifest_id(); |
| 96 | + let source_entry = source_root |
| 97 | + .find_entry( |
| 98 | + ctx.clone(), |
| 99 | + hook_repo.repo_blobstore().clone(), |
| 100 | + source_path.clone(), |
| 101 | + ) |
| 102 | + .await? |
| 103 | + .ok_or_else(|| { |
| 104 | + anyhow::anyhow!(concat!( |
| 105 | + "Source directory {source_path} ", |
| 106 | + "not found in source changeset {source}" |
| 107 | + )) |
| 108 | + })?; |
| 109 | + let source_count = match source_entry { |
| 110 | + manifest::Entry::Tree(tree_id) => { |
| 111 | + let source_tree = tree_id.load(ctx, hook_repo.repo_blobstore()).await?; |
| 112 | + source_tree.summary().descendant_files_count |
| 113 | + } |
| 114 | + manifest::Entry::Leaf(_) => 1, |
| 115 | + }; |
| 116 | + |
| 117 | + if source_count > source_file_count_limit { |
| 118 | + return Ok(HookExecution::Rejected(HookRejectionInfo::new_long( |
| 119 | + "Subtree source is too large", |
| 120 | + self.config |
| 121 | + .too_many_files_rejection_message |
| 122 | + .replace("${dest_path}", &path.to_string()) |
| 123 | + .replace("${source}", &source.to_string()) |
| 124 | + .replace("${source_path}", &source_path.to_string()) |
| 125 | + .replace("${count}", &source_count.to_string()) |
| 126 | + .replace("${limit}", &source_file_count_limit.to_string()), |
| 127 | + ))); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if let Some(dest_min_path_depth) = self.config.dest_min_path_depth { |
| 133 | + if path.num_components() < dest_min_path_depth { |
| 134 | + return Ok(HookExecution::Rejected(HookRejectionInfo::new_long( |
| 135 | + "Subtree destination is too shallow", |
| 136 | + self.config |
| 137 | + .dest_too_shallow_rejection_message |
| 138 | + .replace("${dest_path}", &path.to_string()) |
| 139 | + .replace("${dest_min_path_depth}", &dest_min_path_depth.to_string()), |
| 140 | + ))); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + Ok(HookExecution::Accepted) |
| 146 | + } |
| 147 | +} |
0 commit comments