Skip to content

Commit

Permalink
Merge pull request #110 from DeterminateSystems/revert-108-less-githu…
Browse files Browse the repository at this point in the history
…b-dependence

Revert "Less GitHub dependence"
  • Loading branch information
cole-h authored Feb 15, 2024
2 parents 508b6de + 282b268 commit b1964c8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 127 deletions.
76 changes: 7 additions & 69 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ 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, MAX_LABEL_LENGTH, MAX_NUM_TOTAL_LABELS},
},
github::{get_actions_id_bearer_token, graphql::GithubGraphqlDataQuery},
push::push_new_release,
release_metadata::RevisionInfo,
};
Expand Down Expand Up @@ -251,27 +247,9 @@ 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> {
let span = tracing::Span::current();
tracing::trace!("Executing");
tracing::trace!(?self, "Executing");
let Self {
host,
visibility,
Expand Down Expand Up @@ -412,7 +390,6 @@ 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,
Expand Down Expand Up @@ -457,63 +434,24 @@ 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.revision,
commit_count,
revision_info,
&repository,
upload_name,
mirror,
visibility,
tag,
rolling,
rolling_minor.0,
labels,
spdx_identifier,
github_graphql_data_result,
extra_labels,
spdx_expression.0,
error_on_conflict,
include_output_paths,
github_graphql_data_result.project_id,
github_graphql_data_result.owner_id,
)
.await?;

Expand Down
45 changes: 15 additions & 30 deletions src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::{
build_http_client,
error::Error,
flake_info::{check_flake_evaluates, get_flake_metadata, get_flake_outputs, get_flake_tarball},
release_metadata::ReleaseMetadata,
github::graphql::GithubGraphqlDataResult,
release_metadata::{ReleaseMetadata, RevisionInfo},
Visibility,
};

Expand All @@ -20,25 +21,12 @@ const DEFAULT_ROLLING_PREFIX: &str = "0.1";
#[tracing::instrument(
skip_all,
fields(
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,
repository = %repository,
upload_name = tracing::field::Empty,
mirror = %mirror,
tag = tracing::field::Empty,
source = tracing::field::Empty,
mirrored = tracing::field::Empty,
)
)]
#[allow(clippy::too_many_arguments)]
Expand All @@ -47,20 +35,19 @@ pub(crate) async fn push_new_release(
upload_bearer_token: &str,
flake_root: &Path,
subdir: &Path,
revision: String,
revision_count: i64,
revision_info: RevisionInfo,
repository: &str,
upload_name: String,
mirror: bool,
visibility: Visibility,
tag: Option<String>,
rolling: bool,
rolling_minor: Option<u64>,
labels: Vec<String>,
github_graphql_data_result: GithubGraphqlDataResult,
extra_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 @@ -215,17 +202,15 @@ pub(crate) async fn push_new_release(
let release_metadata = ReleaseMetadata::build(
&source,
subdir,
revision,
revision_count,
revision_info,
flake_metadata,
flake_outputs,
upload_name.clone(),
mirror,
visibility,
labels,
github_graphql_data_result,
extra_labels,
spdx_expression,
project_id,
owner_id,
)
.await
.wrap_err("Building release metadata")?;
Expand Down
88 changes: 60 additions & 28 deletions src/release_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use color_eyre::eyre::{eyre, WrapErr};
use std::path::Path;
use std::{collections::HashSet, path::Path};

use crate::Visibility;
use crate::{
github::graphql::{GithubGraphqlDataResult, MAX_LABEL_LENGTH, MAX_NUM_TOTAL_LABELS},
Visibility,
};

const README_FILENAME_LOWERCASE: &str = "readme.md";

Expand Down Expand Up @@ -87,63 +90,92 @@ impl ReleaseMetadata {
subdir = %subdir.display(),
description = tracing::field::Empty,
readme_path = tracing::field::Empty,
%revision,
%commit_count,
revision = tracing::field::Empty,
revision_count = tracing::field::Empty,
commit_count = tracing::field::Empty,
spdx_identifier = tracing::field::Empty,
visibility = ?visibility,
%project_id,
%owner_id
))]
pub(crate) async fn build(
flake_store_path: &Path,
subdir: &Path,
revision: String,
commit_count: i64,
revision_info: RevisionInfo,
flake_metadata: serde_json::Value,
flake_outputs: serde_json::Value,
upload_name: String,
mirror: bool,
visibility: Visibility,
labels: Vec<String>,
spdx_identifier: Option<spdx::Expression>,
project_id: i64,
owner_id: i64,
github_graphql_data_result: GithubGraphqlDataResult,
extra_labels: Vec<String>,
spdx_expression: Option<spdx::Expression>,
) -> color_eyre::Result<ReleaseMetadata> {
let span = tracing::Span::current();

if let Some(spdx_identifier) = &spdx_identifier {
span.record("spdx_identifier", tracing::field::display(spdx_identifier));
}
span.record("revision_string", &revision_info.revision);

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") {
let description_value = description
Some(description
.as_str()
.ok_or_else(|| {
eyre!("`nix flake metadata --json` does not have a string `description` field")
})?
.to_string();
span.record("description", tracing::field::display(&description_value));
Some(description_value)
.to_string())
} 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));
}
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
};

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<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();

Ok(ReleaseMetadata {
description,
repo: upload_name.to_string(),
raw_flake_metadata: flake_metadata.clone(),
readme: readme_path,
revision,
commit_count,
readme,
revision: revision_info.revision,
commit_count: github_graphql_data_result.rev_count,
visibility,
outputs: flake_outputs,
source_subdirectory: Some(
Expand All @@ -154,8 +186,8 @@ impl ReleaseMetadata {
),
mirrored: mirror,
spdx_identifier,
project_id,
owner_id,
project_id: github_graphql_data_result.project_id,
owner_id: github_graphql_data_result.owner_id,
labels,
})
}
Expand Down

0 comments on commit b1964c8

Please sign in to comment.