Skip to content

Commit

Permalink
Allow codemod to specify rollout percentage
Browse files Browse the repository at this point in the history
Reviewed By: captbaritone

Differential Revision: D64425741

fbshipit-source-id: 59f78cde8622d9f4c0be34e570844d5ee652073f
  • Loading branch information
gordyf authored and facebook-github-bot committed Oct 16, 2024
1 parent e007500 commit e21e162
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compiler/crates/common/src/rollout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::Serialize;
/// Can be constructed as the Default which passes or a percentage between 0 and
/// 100.
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, JsonSchema)]
pub struct Rollout(Option<u8>);
pub struct Rollout(pub Option<u8>);

impl Rollout {
/// Checks some key deterministically and passes on average the given
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct CodemodCommand {
config: Option<PathBuf>,

/// The name of the codemod to run
#[clap(long, short, value_enum)]
#[clap(subcommand)]
codemod: AvailableCodemod,
}

Expand Down
36 changes: 30 additions & 6 deletions compiler/crates/relay-codemod/src/codemod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use std::fs;
use std::sync::Arc;

use clap::ValueEnum;
use clap::Args;
use clap::Subcommand;
use common::FeatureFlag;
use common::Rollout;
use log::info;
use lsp_types::CodeActionOrCommand;
use lsp_types::TextEdit;
Expand All @@ -18,10 +20,17 @@ use relay_compiler::config::Config;
use relay_transforms::fragment_alias_directive;
use relay_transforms::Programs;

#[derive(ValueEnum, Debug, Clone)]
#[derive(Subcommand, Debug, Clone)]
pub enum AvailableCodemod {
/// Marks unaliased conditional fragment spreads as @dangerously_unaliased_fixme
MarkDangerousConditionalFragmentSpreads,
MarkDangerousConditionalFragmentSpreads(MarkDangerousConditionalFragmentSpreadsArgs),
}

#[derive(Args, Debug, Clone)]
pub struct MarkDangerousConditionalFragmentSpreadsArgs {
/// If using a feature flag, specify the rollout percentage. If omitted, the codemod is fully enabled.
#[clap(long, short, value_parser=valid_percent)]
pub rollout_percentage: Option<u8>,
}

pub async fn run_codemod(
Expand All @@ -32,9 +41,13 @@ pub async fn run_codemod(
let diagnostics = programs
.iter()
.flat_map(|programs| match &codemod {
AvailableCodemod::MarkDangerousConditionalFragmentSpreads => {
// TODO: Figure out how to accept FeatureFlag as an optional CLI argument
match fragment_alias_directive(&programs.source, &FeatureFlag::Enabled) {
AvailableCodemod::MarkDangerousConditionalFragmentSpreads(opts) => {
match fragment_alias_directive(
&programs.source,
&FeatureFlag::Rollout {
rollout: Rollout(opts.rollout_percentage),
},
) {
Ok(_) => vec![],
Err(e) => e,
}
Expand Down Expand Up @@ -122,3 +135,14 @@ fn sort_changes(url: &Url, changes: &mut Vec<TextEdit>) -> Result<(), std::io::E
}
Ok(())
}

fn valid_percent(s: &str) -> Result<u8, String> {
// turn s into a u8
let s = s.parse::<u8>().map_err(|_| "not a number".to_string())?;
// check if s is less than 100
if (0..=100).contains(&s) {
Ok(s)
} else {
Err("number must be between 0 and 100, inclusive".to_string())
}
}

0 comments on commit e21e162

Please sign in to comment.