From ed24fc78501bf3b9cc96e3c83ed0cf7f090ec908 Mon Sep 17 00:00:00 2001 From: Youssef Ibrahim Date: Wed, 28 Aug 2024 11:13:37 -0700 Subject: [PATCH] newadmin: add command for changing git source of truth config Summary: Adds a newadmin command `git-source-of-truth set` for displaying changing the git source of truth config for a repo. Reviewed By: RajivTS Differential Revision: D61920226 fbshipit-source-id: 1a958bea2649f8240fff3d3397a07e857e665058 --- .../test-newadmin-git-source-of-truth.t | 28 +++++++++++++ .../admin/src/commands/git_source_of_truth.rs | 6 +++ .../src/commands/git_source_of_truth/set.rs | 39 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 eden/mononoke/tests/integration/newadmin/test-newadmin-git-source-of-truth.t create mode 100644 eden/mononoke/tools/admin/src/commands/git_source_of_truth/set.rs diff --git a/eden/mononoke/tests/integration/newadmin/test-newadmin-git-source-of-truth.t b/eden/mononoke/tests/integration/newadmin/test-newadmin-git-source-of-truth.t new file mode 100644 index 0000000000000..62b07d08739be --- /dev/null +++ b/eden/mononoke/tests/integration/newadmin/test-newadmin-git-source-of-truth.t @@ -0,0 +1,28 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This software may be used and distributed according to the terms of the +# GNU General Public License found in the LICENSE file in the root +# directory of this source tree. + + $ . "${TEST_FIXTURES}/library.sh" + +setup configuration + $ setup_common_config + + $ mononoke_newadmin git-source-of-truth -R repo show + No git source of truth config entry found for repo repo + + $ mononoke_newadmin git-source-of-truth -R repo set locked + + $ mononoke_newadmin git-source-of-truth -R repo show + GitSourceOfTruthConfigEntry { id: RowId(1), repo_id: RepositoryId(0), repo_name: RepositoryName("repo"), source_of_truth: Locked } + + $ mononoke_newadmin git-source-of-truth -R repo set mononoke + + $ mononoke_newadmin git-source-of-truth -R repo show + GitSourceOfTruthConfigEntry { id: RowId(2), repo_id: RepositoryId(0), repo_name: RepositoryName("repo"), source_of_truth: Mononoke } + + $ mononoke_newadmin git-source-of-truth -R repo set metagit + + $ mononoke_newadmin git-source-of-truth -R repo show + GitSourceOfTruthConfigEntry { id: RowId(3), repo_id: RepositoryId(0), repo_name: RepositoryName("repo"), source_of_truth: Metagit } diff --git a/eden/mononoke/tools/admin/src/commands/git_source_of_truth.rs b/eden/mononoke/tools/admin/src/commands/git_source_of_truth.rs index 4d8f88dd49880..1dd75dc366b53 100644 --- a/eden/mononoke/tools/admin/src/commands/git_source_of_truth.rs +++ b/eden/mononoke/tools/admin/src/commands/git_source_of_truth.rs @@ -5,6 +5,7 @@ * GNU General Public License version 2. */ +mod set; mod show; use anyhow::Result; @@ -14,6 +15,8 @@ use git_source_of_truth::GitSourceOfTruthConfig; use mononoke_app::args::RepoArgs; use mononoke_app::MononokeApp; use repo_identity::RepoIdentity; +use set::set; +use set::SetArgs; use show::show; use show::ShowArgs; @@ -31,6 +34,8 @@ pub struct CommandArgs { pub enum GitSourceOfTruthSubcommand { /// Show git source of truth config for this repo. Show(ShowArgs), + /// Set git source of truth config for this repo. + Set(SetArgs), } #[facet::container] @@ -48,5 +53,6 @@ pub async fn run(app: MononokeApp, args: CommandArgs) -> Result<()> { match args.subcommand { GitSourceOfTruthSubcommand::Show(args) => show(&ctx, &repo, args).await, + GitSourceOfTruthSubcommand::Set(args) => set(&ctx, &repo, args).await, } } diff --git a/eden/mononoke/tools/admin/src/commands/git_source_of_truth/set.rs b/eden/mononoke/tools/admin/src/commands/git_source_of_truth/set.rs new file mode 100644 index 0000000000000..23995e2076739 --- /dev/null +++ b/eden/mononoke/tools/admin/src/commands/git_source_of_truth/set.rs @@ -0,0 +1,39 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This software may be used and distributed according to the terms of the + * GNU General Public License version 2. + */ + +use anyhow::Result; +use clap::builder::PossibleValuesParser; +use clap::Args; +use context::CoreContext; +use git_source_of_truth::GitSourceOfTruth; +use git_source_of_truth::GitSourceOfTruthConfigRef; +use git_source_of_truth::RepositoryName; +use repo_identity::RepoIdentityRef; + +use super::Repo; + +#[derive(Args)] +pub struct SetArgs { + /// Source of truth to set for this repo. [possible values: metagit, mononoke, locked] + source_of_truth: GitSourceOfTruth, +} + +pub async fn set(ctx: &CoreContext, repo: &Repo, args: SetArgs) -> Result<()> { + let repo_id = repo.repo_identity().id(); + let repo_name = repo.repo_identity().name(); + + repo.git_source_of_truth_config() + .set( + ctx, + repo_id, + RepositoryName(repo_name.to_string()), + args.source_of_truth, + ) + .await?; + + Ok(()) +}