Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Less GitHub dependence #111

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 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,28 @@ 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,
commit_count = 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 @@ -390,6 +413,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,
Expand Down Expand Up @@ -434,24 +458,56 @@ impl FlakeHubPushCli {
}
};

let commit_count = github_graphql_data_result.rev_count;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This appears to be a logic change, but it's not.

span.record("commit_count", commit_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
45 changes: 30 additions & 15 deletions src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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)]
Expand All @@ -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<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 @@ -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")?;
Expand Down
88 changes: 28 additions & 60 deletions src/release_metadata.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<String>,
spdx_expression: Option<spdx::Expression>,
labels: Vec<String>,
spdx_identifier: Option<spdx::Expression>,
project_id: i64,
owner_id: i64,
) -> color_eyre::Result<ReleaseMetadata> {
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was effectively dead, we'd not actually use revision_count anywhere except on L127 on the span.record.

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<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,
revision: revision_info.revision,
commit_count: github_graphql_data_result.rev_count,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how this matches with the apparent logic change above.

readme: readme_path,
revision,
commit_count,
visibility,
outputs: flake_outputs,
source_subdirectory: Some(
Expand All @@ -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,
})
}
Expand Down
Loading