diff --git a/action.yaml b/action.yaml index 6782616a..ef885811 100644 --- a/action.yaml +++ b/action.yaml @@ -88,6 +88,10 @@ inputs: description: Please read the source code of flakehub-push before using this. required: false default: false + disable-github-api: + description: Disables communicating to the GitHub API. + required: false + default: false # Used to construct the binary download URL source-binary: diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 55677547..dedea37e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -88,6 +88,14 @@ pub(crate) struct FlakeHubPushCli { )] pub(crate) spdx_expression: OptionSpdxExpression, + #[clap( + long, + env = "FLAKEHUB_PUSH_DISABLE_GITHUB_API", + value_parser = EmptyBoolParser, + default_value_t = false + )] + pub(crate) disable_github_api: bool, + #[clap( long, env = "FLAKEHUB_PUSH_ERROR_ON_CONFLICT", diff --git a/src/git_context.rs b/src/git_context.rs index f562905e..49908c13 100644 --- a/src/git_context.rs +++ b/src/git_context.rs @@ -14,13 +14,16 @@ pub struct GitContext { impl GitContext { pub fn from_cli_and_github( cli: &FlakeHubPushCli, - github_graphql_data_result: &GithubGraphqlDataResult, + github_graphql_data_result: Option, ) -> Result { // step: validate spdx, backfill from GitHub API let spdx_expression = if cli.spdx_expression.0.is_none() { - if let Some(spdx_string) = &github_graphql_data_result.spdx_identifier { + if let Some(spdx_string) = github_graphql_data_result + .clone() + .and_then(|result| result.spdx_identifier.clone()) + { tracing::debug!("Recieved SPDX identifier `{}` from GitHub API", spdx_string); - let parsed = spdx::Expression::parse(spdx_string) + let parsed = spdx::Expression::parse(&spdx_string) .wrap_err("Invalid SPDX license identifier reported from the GitHub API, either you are using a non-standard license or GitHub has returned a value that cannot be validated")?; Some(parsed) } else { @@ -28,29 +31,35 @@ impl GitContext { } } else { // Provide the user notice if the SPDX expression passed differs from the one detected on GitHub -- It's probably something they care about. - if github_graphql_data_result.spdx_identifier - != cli.spdx_expression.0.as_ref().map(|v| v.to_string()) + if github_graphql_data_result + .clone() + .map(|result| result.spdx_identifier) + != Some(cli.spdx_expression.0.as_ref().map(|v| v.to_string())) { tracing::warn!( "SPDX identifier `{}` was passed via argument, but GitHub's API suggests it may be `{}`", cli.spdx_expression.0.as_ref().map(|v| v.to_string()).unwrap_or_else(|| "None".to_string()), - github_graphql_data_result.spdx_identifier.clone().unwrap_or_else(|| "None".to_string()), + github_graphql_data_result.clone().map(|result| result.spdx_identifier.clone().unwrap_or_else(|| "None".to_string())).unwrap_or_else(|| "None".to_string()), ) } cli.spdx_expression.0.clone() }; - let rev = cli - .rev - .0 - .as_ref() - .unwrap_or(&github_graphql_data_result.revision); + let rev = cli.rev.0.clone().unwrap_or_else(|| { + github_graphql_data_result + .clone() + .expect("GitHub API is disabled and rev is not specified") + .revision + }); let ctx = GitContext { spdx_expression, - repo_topics: github_graphql_data_result.topics.clone(), + repo_topics: github_graphql_data_result + .clone() + .map(|result| result.topics.clone()) + .unwrap_or_default(), revision_info: RevisionInfo { - commit_count: Some(github_graphql_data_result.rev_count as usize), + commit_count: github_graphql_data_result.map(|result| result.rev_count as usize), revision: rev.to_string(), }, }; diff --git a/src/github/graphql/mod.rs b/src/github/graphql/mod.rs index 84f14ec7..1a6a2804 100644 --- a/src/github/graphql/mod.rs +++ b/src/github/graphql/mod.rs @@ -143,7 +143,7 @@ impl GithubGraphqlDataQuery { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub(crate) struct GithubGraphqlDataResult { pub(crate) revision: String, pub(crate) rev_count: i64, diff --git a/src/push_context.rs b/src/push_context.rs index cd9a0fc6..616a8cf4 100644 --- a/src/push_context.rs +++ b/src/push_context.rs @@ -78,16 +78,22 @@ impl PushContext { .clone() .expect("failed to get github token when running in GitHub Actions"); - let github_graphql_data_result = GithubGraphqlDataQuery::get( - &client, - &github_token, - &project_owner, - &project_name, - &local_rev_info.revision, - ) - .await?; - - let git_ctx = GitContext::from_cli_and_github(cli, &github_graphql_data_result)?; + let github_graphql_data_result = if cli.disable_github_api { + None + } else { + Some( + GithubGraphqlDataQuery::get( + &client, + &github_token, + &project_owner, + &project_name, + &local_rev_info.revision, + ) + .await?, + ) + }; + + let git_ctx = GitContext::from_cli_and_github(cli, github_graphql_data_result)?; let token = crate::github::get_actions_id_bearer_token(&cli.host) .await @@ -131,8 +137,14 @@ impl PushContext { ) .await?; - let git_ctx: GitContext = - GitContext::from_cli_and_github(cli, &github_graphql_data_result)?; + let git_ctx: GitContext = GitContext::from_cli_and_github( + cli, + if cli.disable_github_api { + None + } else { + Some(github_graphql_data_result.clone()) + }, + )?; let token = flakehub_auth_fake::get_fake_bearer_token( u, diff --git a/ts/index.ts b/ts/index.ts index 3e892093..be94f32a 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -27,6 +27,7 @@ type ExecutionEnvironment = { FLAKEHUB_PUSH_ROLLING?: string; FLAKEHUB_PUSH_MIRROR?: string; FLAKEHUB_PUSH_ROLLING_MINOR?: string; + FLAKEHUB_PUSH_DISABLE_GITHUB_API?: string; GITHUB_CONTEXT?: string; }; @@ -50,6 +51,7 @@ class FlakeHubPushAction extends DetSysAction { private mirror: boolean; private name: string | null; private rollingMinor: number | null; + private disableGitHubAPI: boolean; constructor() { super({ @@ -79,6 +81,7 @@ class FlakeHubPushAction extends DetSysAction { this.mirror = inputs.getBool("mirror"); this.name = inputs.getStringOrNull("name"); this.rollingMinor = inputs.getNumberOrNull("rolling-minor"); + this.disableGitHubAPI = inputs.getBool("disable-github-api"); } async main(): Promise { @@ -133,6 +136,7 @@ class FlakeHubPushAction extends DetSysAction { env.FLAKEHUB_PUSH_INCLUDE_OUTPUT_PATHS = this.includeOutputPaths.toString(); env.FLAKEHUB_PUSH_ROLLING = this.rolling.toString(); env.FLAKEHUB_PUSH_MIRROR = this.mirror.toString(); + env.FLAKEHUB_PUSH_DISABLE_GITHUB_API = this.disableGitHubAPI.toString(); env.GITHUB_CONTEXT = JSON.stringify(actionsGithub.context);