From ab2c147a10a6e5f4aaa05d2d122a8e160d8bd10c Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Fri, 16 Feb 2024 12:10:33 -0800 Subject: [PATCH] Revert "Revert "Less GitHub dependence"" --- src/cli/mod.rs | 76 +++++++++++++++++++++++++++++++---- src/push.rs | 45 ++++++++++++++------- src/release_metadata.rs | 88 +++++++++++++---------------------------- 3 files changed, 127 insertions(+), 82 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index baddb80..070432b 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -2,13 +2,17 @@ mod instrumentation; use color_eyre::eyre::{eyre, WrapErr}; use std::{ + collections::HashSet, path::{Path, PathBuf}, process::ExitCode, }; use crate::{ build_http_client, - github::{get_actions_id_bearer_token, graphql::GithubGraphqlDataQuery}, + github::{ + get_actions_id_bearer_token, + graphql::{GithubGraphqlDataQuery, MAX_LABEL_LENGTH, MAX_NUM_TOTAL_LABELS}, + }, push::push_new_release, release_metadata::RevisionInfo, }; @@ -247,9 +251,27 @@ impl FlakeHubPushCli { #[tracing::instrument( name = "flakehub_push" skip_all, + fields( + host = self.host, + visibility = ?self.visibility, + name = self.name.0, + tag = tracing::field::Empty, + rolling_minor = tracing::field::Empty, + rolling = self.rolling, + directory = tracing::field::Empty, + repository = tracing::field::Empty, + git_root = tracing::field::Empty, + mirror = self.mirror, + jwt_issuer_uri = tracing::field::Empty, + extra_labels = self.extra_labels.join(","), + spdx_identifier = tracing::field::Empty, + error_on_conflict = self.error_on_conflict, + include_output_paths = self.include_output_paths, + ) )] pub(crate) async fn execute(self) -> color_eyre::Result { - tracing::trace!(?self, "Executing"); + let span = tracing::Span::current(); + tracing::trace!("Executing"); let Self { host, visibility, @@ -390,6 +412,7 @@ impl FlakeHubPushCli { let github_api_client = build_http_client().build()?; let revision_info = RevisionInfo::from_git_root(&git_root)?; + let github_graphql_data_result = GithubGraphqlDataQuery::get( &github_api_client, &github_token, @@ -434,24 +457,63 @@ impl FlakeHubPushCli { } }; + let commit_count = match revision_info.local_revision_count { + Some(n) => n as i64, + None => { + tracing::debug!( + "Getting revision count locally failed, using data from github instead" + ); + github_graphql_data_result.rev_count + } + }; + + let spdx_identifier = if spdx_expression.0.is_some() { + spdx_expression.0 + } else if let Some(spdx_string) = &github_graphql_data_result.spdx_identifier { + 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")?; + span.record("spdx_identifier", tracing::field::display(&parsed)); + Some(parsed) + } else { + None + }; + + // Here we merge explicitly user-supplied labels and the labels ("topics") + // associated with the repo. Duplicates are excluded and all + // are converted to lower case. + let labels: Vec = extra_labels + .into_iter() + .chain(github_graphql_data_result.topics.into_iter()) + .collect::>() + .into_iter() + .take(MAX_NUM_TOTAL_LABELS) + .map(|s| s.trim().to_lowercase()) + .filter(|t: &String| { + !t.is_empty() + && t.len() <= MAX_LABEL_LENGTH + && t.chars().all(|c| c.is_alphanumeric() || c == '-') + }) + .collect(); + push_new_release( &host, &upload_bearer_token, &git_root, &subdir, - revision_info, - &repository, + revision_info.revision, + commit_count, upload_name, mirror, visibility, tag, rolling, rolling_minor.0, - github_graphql_data_result, - extra_labels, - spdx_expression.0, + labels, + spdx_identifier, error_on_conflict, include_output_paths, + github_graphql_data_result.project_id, + github_graphql_data_result.owner_id, ) .await?; diff --git a/src/push.rs b/src/push.rs index d584a0b..188602b 100644 --- a/src/push.rs +++ b/src/push.rs @@ -11,8 +11,7 @@ use crate::{ build_http_client, error::Error, flake_info::{check_flake_evaluates, get_flake_metadata, get_flake_outputs, get_flake_tarball}, - github::graphql::GithubGraphqlDataResult, - release_metadata::{ReleaseMetadata, RevisionInfo}, + release_metadata::ReleaseMetadata, Visibility, }; @@ -21,12 +20,25 @@ const DEFAULT_ROLLING_PREFIX: &str = "0.1"; #[tracing::instrument( skip_all, fields( - repository = %repository, - upload_name = tracing::field::Empty, - mirror = %mirror, - tag = tracing::field::Empty, - source = tracing::field::Empty, - mirrored = tracing::field::Empty, + host, + flake_root, + subdir, + revision, + revision_count, + repository, + upload_name, + mirror, + %visibility, + tag, + rolling, + rolling_minor, + labels = labels.join(","), + mirror, + spdx_expression, + error_if_release_conflicts, + include_output_paths, + project_id, + owner_id, ) )] #[allow(clippy::too_many_arguments)] @@ -35,19 +47,20 @@ pub(crate) async fn push_new_release( upload_bearer_token: &str, flake_root: &Path, subdir: &Path, - revision_info: RevisionInfo, - repository: &str, + revision: String, + revision_count: i64, upload_name: String, mirror: bool, visibility: Visibility, tag: Option, rolling: bool, rolling_minor: Option, - github_graphql_data_result: GithubGraphqlDataResult, - extra_labels: Vec, + labels: Vec, spdx_expression: Option, error_if_release_conflicts: bool, include_output_paths: bool, + project_id: i64, + owner_id: i64, ) -> color_eyre::Result<()> { let span = tracing::Span::current(); span.record("upload_name", tracing::field::display(upload_name.clone())); @@ -202,15 +215,17 @@ pub(crate) async fn push_new_release( let release_metadata = ReleaseMetadata::build( &source, subdir, - revision_info, + revision, + revision_count, flake_metadata, flake_outputs, upload_name.clone(), mirror, visibility, - github_graphql_data_result, - extra_labels, + labels, spdx_expression, + project_id, + owner_id, ) .await .wrap_err("Building release metadata")?; diff --git a/src/release_metadata.rs b/src/release_metadata.rs index 7f39f28..e545e51 100644 --- a/src/release_metadata.rs +++ b/src/release_metadata.rs @@ -1,10 +1,7 @@ use color_eyre::eyre::{eyre, WrapErr}; -use std::{collections::HashSet, path::Path}; +use std::path::Path; -use crate::{ - github::graphql::{GithubGraphqlDataResult, MAX_LABEL_LENGTH, MAX_NUM_TOTAL_LABELS}, - Visibility, -}; +use crate::Visibility; const README_FILENAME_LOWERCASE: &str = "readme.md"; @@ -90,92 +87,63 @@ impl ReleaseMetadata { subdir = %subdir.display(), description = tracing::field::Empty, readme_path = tracing::field::Empty, - revision = tracing::field::Empty, - revision_count = tracing::field::Empty, - commit_count = tracing::field::Empty, + %revision, + %commit_count, spdx_identifier = tracing::field::Empty, visibility = ?visibility, + %project_id, + %owner_id ))] pub(crate) async fn build( flake_store_path: &Path, subdir: &Path, - revision_info: RevisionInfo, + revision: String, + commit_count: i64, flake_metadata: serde_json::Value, flake_outputs: serde_json::Value, upload_name: String, mirror: bool, visibility: Visibility, - github_graphql_data_result: GithubGraphqlDataResult, - extra_labels: Vec, - spdx_expression: Option, + labels: Vec, + spdx_identifier: Option, + project_id: i64, + owner_id: i64, ) -> color_eyre::Result { let span = tracing::Span::current(); - span.record("revision_string", &revision_info.revision); + if let Some(spdx_identifier) = &spdx_identifier { + span.record("spdx_identifier", tracing::field::display(spdx_identifier)); + } assert!(subdir.is_relative()); - let revision_count = match revision_info.local_revision_count { - Some(n) => n as i64, - None => { - tracing::debug!( - "Getting revision count locally failed, using data from github instead" - ); - github_graphql_data_result.rev_count - } - }; - span.record("revision_count", revision_count); - let description = if let Some(description) = flake_metadata.get("description") { - Some(description + let description_value = description .as_str() .ok_or_else(|| { eyre!("`nix flake metadata --json` does not have a string `description` field") })? - .to_string()) + .to_string(); + span.record("description", tracing::field::display(&description_value)); + Some(description_value) } else { None }; - let readme = get_readme(flake_store_path).await?; - - let spdx_identifier = if spdx_expression.is_some() { - spdx_expression - } else if let Some(spdx_string) = github_graphql_data_result.spdx_identifier { - 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")?; - span.record("spdx_identifier", tracing::field::display(&parsed)); - Some(parsed) - } else { - None - }; + let readme_path = get_readme(flake_store_path).await?; + if let Some(readme_path) = &readme_path { + span.record("readme_path", tracing::field::display(readme_path)); + } tracing::trace!("Collected ReleaseMetadata information"); - // Here we merge explicitly user-supplied labels and the labels ("topics") - // associated with the repo. Duplicates are excluded and all - // are converted to lower case. - let labels: Vec = extra_labels - .into_iter() - .chain(github_graphql_data_result.topics.into_iter()) - .collect::>() - .into_iter() - .take(MAX_NUM_TOTAL_LABELS) - .map(|s| s.trim().to_lowercase()) - .filter(|t: &String| { - !t.is_empty() - && t.len() <= MAX_LABEL_LENGTH - && t.chars().all(|c| c.is_alphanumeric() || c == '-') - }) - .collect(); - Ok(ReleaseMetadata { description, repo: upload_name.to_string(), raw_flake_metadata: flake_metadata.clone(), - readme, - revision: revision_info.revision, - commit_count: github_graphql_data_result.rev_count, + readme: readme_path, + revision, + commit_count, visibility, outputs: flake_outputs, source_subdirectory: Some( @@ -186,8 +154,8 @@ impl ReleaseMetadata { ), mirrored: mirror, spdx_identifier, - project_id: github_graphql_data_result.project_id, - owner_id: github_graphql_data_result.owner_id, + project_id, + owner_id, labels, }) }