Skip to content

Commit

Permalink
Move github data from push_new_release
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Feb 15, 2024
1 parent c122851 commit c28b807
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 71 deletions.
76 changes: 68 additions & 8 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<std::process::ExitCode> {
tracing::trace!(?self, "Executing");
let span = tracing::Span::current();
tracing::trace!("Executing");
let Self {
host,
visibility,
Expand Down Expand Up @@ -391,7 +413,6 @@ impl FlakeHubPushCli {

let revision_info = RevisionInfo::from_git_root(&git_root)?;


let github_graphql_data_result = GithubGraphqlDataQuery::get(
&github_api_client,
&github_token,
Expand Down Expand Up @@ -436,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<String> = extra_labels
.into_iter()
.chain(github_graphql_data_result.topics.into_iter())
.collect::<HashSet<String>>()
.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?;

Expand Down
89 changes: 32 additions & 57 deletions src/push.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use color_eyre::eyre::{eyre, WrapErr};
use reqwest::{header::HeaderMap, StatusCode};
use std::{
collections::HashSet, path::{Path, PathBuf}, str::FromStr
path::{Path, PathBuf},
str::FromStr,
};
use tokio::io::AsyncWriteExt;
use uuid::Uuid;
Expand All @@ -10,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, MAX_LABEL_LENGTH, MAX_NUM_TOTAL_LABELS},
release_metadata::{ReleaseMetadata, RevisionInfo},
release_metadata::ReleaseMetadata,
Visibility,
};

Expand All @@ -20,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)]
Expand All @@ -34,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<String>,
rolling: bool,
rolling_minor: Option<u64>,
github_graphql_data_result: GithubGraphqlDataResult,
extra_labels: Vec<String>,
labels: Vec<String>,
spdx_expression: Option<spdx::Expression>,
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()));
Expand Down Expand Up @@ -198,59 +212,20 @@ pub(crate) async fn push_new_release(
.await
.wrap_err("Writing compressed tarball to tempfile")?;


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
}
};

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
};

// 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<String> = extra_labels
.into_iter()
.chain(github_graphql_data_result.topics.into_iter())
.collect::<HashSet<String>>()
.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();

let release_metadata = ReleaseMetadata::build(
&source,
subdir,
revision_info,
revision,
revision_count,
flake_metadata,
flake_outputs,
upload_name.clone(),
mirror,
visibility,
revision_count,
labels,
spdx_identifier,
github_graphql_data_result.project_id,
github_graphql_data_result.owner_id,
spdx_expression,
project_id,
owner_id,
)
.await
.wrap_err("Building release metadata")?;
Expand Down
12 changes: 6 additions & 6 deletions src/release_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl ReleaseMetadata {
subdir = %subdir.display(),
description = tracing::field::Empty,
readme_path = tracing::field::Empty,
revision = revision_info.revision,
%revision_count,
%revision,
%commit_count,
spdx_identifier = tracing::field::Empty,
visibility = ?visibility,
%project_id,
Expand All @@ -97,13 +97,13 @@ impl ReleaseMetadata {
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,
revision_count: i64,
labels: Vec<String>,
spdx_identifier: Option<spdx::Expression>,
project_id: i64,
Expand Down Expand Up @@ -142,8 +142,8 @@ impl ReleaseMetadata {
repo: upload_name.to_string(),
raw_flake_metadata: flake_metadata.clone(),
readme: readme_path,
revision: revision_info.revision,
commit_count: revision_count,
revision,
commit_count,
visibility,
outputs: flake_outputs,
source_subdirectory: Some(
Expand Down

0 comments on commit c28b807

Please sign in to comment.