Skip to content

Commit 638e417

Browse files
authored
Merge pull request #131 from DeterminateSystems/colemickens/improve-message
improve unauthenticated error message
2 parents d35e6e7 + 7e25ca3 commit 638e417

5 files changed

Lines changed: 140 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 76 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

magic-nix-cache/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ http-body-util = "0.1"
5858
hyper = { version = "1.0.0", features = ["full"] }
5959
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] }
6060
xdg = { version = "2.5.2" }
61+
color-eyre = { version = "0.6.3" }
6162

6263
[dependencies.tokio]
6364
version = "1.44.2"

magic-nix-cache/src/github.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
const GITHUB_ACTOR_TYPE_USER: &str = "User";
4+
const GITHUB_ACTOR_TYPE_ORGANIZATION: &str = "Organization";
5+
6+
#[derive(Serialize, Deserialize)]
7+
pub struct WorkflowData {
8+
event: WorkflowDataEvent,
9+
}
10+
11+
#[derive(Serialize, Deserialize)]
12+
pub struct WorkflowDataEvent {
13+
repository: WorkflowDataEventRepo,
14+
}
15+
16+
#[derive(Serialize, Deserialize)]
17+
pub struct WorkflowDataEventRepo {
18+
owner: WorkflowDataEventRepoOwner,
19+
}
20+
21+
#[derive(Serialize, Deserialize)]
22+
pub struct WorkflowDataEventRepoOwner {
23+
login: String,
24+
#[serde(rename = "type")]
25+
kind: String,
26+
}
27+
28+
pub(crate) fn get_actions_event_data() -> color_eyre::Result<WorkflowData> {
29+
let github_context = std::env::var("GITHUB_CONTEXT")?;
30+
let workflow_data: WorkflowData = serde_json::from_str::<WorkflowData>(&github_context)?;
31+
32+
Ok(workflow_data)
33+
}
34+
35+
pub(crate) fn print_unauthenticated_error() {
36+
let mut msg = "::error title=FlakeHub registration required.::Unable to authenticate to FlakeHub. Individuals must register at FlakeHub.com; Organizations must create an organization at FlakeHub.com.".to_string();
37+
if let Ok(workflow_data) = get_actions_event_data() {
38+
let owner = workflow_data.event.repository.owner;
39+
if owner.kind == GITHUB_ACTOR_TYPE_USER {
40+
msg = format!(
41+
"::error title=FlakeHub registration required.::Please create an account for {} on FlakeHub.com to publish flakes.",
42+
&owner.login
43+
);
44+
} else if owner.kind == GITHUB_ACTOR_TYPE_ORGANIZATION {
45+
msg = format!(
46+
"::error title=FlakeHub registration required.::Please create an organization for {} on FlakeHub.com to publish flakes.",
47+
&owner.login
48+
);
49+
}
50+
};
51+
println!("{}", msg);
52+
}

magic-nix-cache/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod env;
1818
mod error;
1919
mod flakehub;
2020
mod gha;
21+
mod github;
2122
mod pbh;
2223
mod telemetry;
2324
mod util;
@@ -366,8 +367,11 @@ async fn main_cli() -> Result<()> {
366367
Some(state)
367368
}
368369
Err(err) => {
369-
tracing::error!("FlakeHub cache initialization failed: {}. Unable to authenticate to FlakeHub. Individuals must register at FlakeHub.com; Organizations must create an organization at FlakeHub.com.", err);
370-
println!("::error title={{FlakeHub: Unauthenticated}}::{{Unable to authenticate to FlakeHub. Individuals must register at FlakeHub.com; Organizations must create an organization at FlakeHub.com.}}");
370+
tracing::error!(
371+
"FlakeHub: cache initialized failed: Unauthenticated: {}",
372+
err
373+
);
374+
github::print_unauthenticated_error();
371375
None
372376
}
373377
}

0 commit comments

Comments
 (0)