Skip to content
Merged
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ crates_io_diesel_helpers = { path = "crates/crates_io_diesel_helpers" }
crates_io_docs_rs = { path = "crates/crates_io_docs_rs" }
crates_io_env_vars = { path = "crates/crates_io_env_vars" }
crates_io_github = { path = "crates/crates_io_github" }
crates_io_heroku = { path = "crates/crates_io_heroku" }
crates_io_index = { path = "crates/crates_io_index" }
crates_io_linecount = { path = "crates/crates_io_linecount" }
crates_io_markdown = { path = "crates/crates_io_markdown" }
Expand Down
1 change: 1 addition & 0 deletions crates/crates_io_database_dump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true
[dependencies]
anyhow = "=1.0.100"
chrono = { version = "=0.4.42", default-features = false, features = ["clock", "serde"] }
crates_io_heroku = { path = "../crates_io_heroku" }
flate2 = "=1.1.5"
minijinja = "=2.12.0"
serde = { version = "=1.0.228", features = ["derive"] }
Expand Down
6 changes: 4 additions & 2 deletions crates/crates_io_database_dump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ impl DumpDirectory {
}
let metadata = Metadata {
timestamp: &self.timestamp,
crates_io_commit: std::env::var("HEROKU_SLUG_COMMIT")
.unwrap_or_else(|_| "unknown".to_owned()),
crates_io_commit: crates_io_heroku::commit()
.ok()
.flatten()
.unwrap_or_else(|| "unknown".to_owned()),
};
let path = self.path().join("metadata.json");
debug!(?path, "Writing metadata.json file…");
Expand Down
12 changes: 12 additions & 0 deletions crates/crates_io_heroku/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "crates_io_heroku"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2024"

[lints]
workspace = true

[dependencies]
anyhow = "=1.0.100"
crates_io_env_vars = { path = "../crates_io_env_vars" }
11 changes: 11 additions & 0 deletions crates/crates_io_heroku/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# crates_io_heroku

This package contains utilities for accessing Heroku-specific environment
variables.

When the `runtime-dyno-metadata` Heroku Labs feature is enabled, Heroku
exposes application and environment information through environment variables.
This crate provides convenient functions to access these values.

For more information, see:
<https://devcenter.heroku.com/articles/dyno-metadata>
97 changes: 97 additions & 0 deletions crates/crates_io_heroku/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#![doc = include_str!("../README.md")]

use crates_io_env_vars::var;

/// Returns the Git SHA of the currently deployed commit.
///
/// This function tries `HEROKU_BUILD_COMMIT` first (the current standard),
/// and falls back to `HEROKU_SLUG_COMMIT` (deprecated) if the former is not
/// set. This provides compatibility with both old and new Heroku deployments.
///
/// Both environment variables are set by Heroku when the appropriate Labs
/// features are enabled (`runtime-dyno-build-metadata` for `HEROKU_BUILD_COMMIT`,
/// `runtime-dyno-metadata` for `HEROKU_SLUG_COMMIT`).
///
/// Returns `None` if neither variable is set (e.g., in local development).
///
/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more
/// information.
///
/// # Examples
///
/// ```
/// use crates_io_heroku::commit;
///
/// if let Ok(Some(commit)) = commit() {
/// println!("Running commit: {}", commit);
/// } else {
/// println!("Commit SHA unknown");
/// }
/// ```
pub fn commit() -> anyhow::Result<Option<String>> {
// Try the current standard first
if let Some(commit) = build_commit()? {
return Ok(Some(commit));
}

// Fall back to the deprecated variable for backward compatibility
slug_commit()
}

/// Returns the Git SHA of the currently deployed commit.
///
/// This value comes from the `HEROKU_SLUG_COMMIT` environment variable,
/// which is set by Heroku when the `runtime-dyno-metadata` Labs feature
/// is enabled. If the variable is not set (e.g., in local development
/// or when the feature is disabled), returns `None`.
///
/// Note: `HEROKU_SLUG_COMMIT` is deprecated by Heroku in favor of
/// `HEROKU_BUILD_COMMIT`, but this function continues to use
/// `HEROKU_SLUG_COMMIT` for backward compatibility with existing
/// deployments.
///
/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more
/// information.
///
/// # Examples
///
/// ```
/// use crates_io_heroku::slug_commit;
///
/// if let Ok(Some(commit)) = slug_commit() {
/// println!("Running commit: {}", commit);
/// } else {
/// println!("Commit SHA unknown");
/// }
/// ```
pub fn slug_commit() -> anyhow::Result<Option<String>> {
var("HEROKU_SLUG_COMMIT")
}

/// Returns the Git SHA of the currently deployed commit.
///
/// This value comes from the `HEROKU_BUILD_COMMIT` environment variable,
/// which is set by Heroku when the `runtime-dyno-build-metadata` Labs
/// feature is enabled. If the variable is not set (e.g., in local development
/// or when the feature is disabled), returns `None`.
///
/// This is the recommended function to use, as `HEROKU_BUILD_COMMIT` is
/// the current standard while `HEROKU_SLUG_COMMIT` is deprecated.
///
/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more
/// information.
///
/// # Examples
///
/// ```
/// use crates_io_heroku::build_commit;
///
/// if let Ok(Some(commit)) = build_commit() {
/// println!("Running commit: {}", commit);
/// } else {
/// println!("Commit SHA unknown");
/// }
/// ```
pub fn build_commit() -> anyhow::Result<Option<String>> {
var("HEROKU_BUILD_COMMIT")
}
3 changes: 2 additions & 1 deletion src/config/sentry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Context;
use crates_io_env_vars::{required_var, var, var_parsed};
use crates_io_heroku::commit;
use sentry::IntoDsn;
use sentry::types::Dsn;

Expand Down Expand Up @@ -28,7 +29,7 @@ impl SentryConfig {
Ok(Self {
dsn,
environment,
release: var("HEROKU_SLUG_COMMIT")?,
release: commit()?,
traces_sample_rate: var_parsed("SENTRY_TRACES_SAMPLE_RATE")?.unwrap_or(0.0),
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/site_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::app::AppState;
use axum::Json;
use axum::response::IntoResponse;
use crates_io_heroku::commit;
use serde::Serialize;

#[derive(Debug, Serialize, utoipa::ToSchema)]
Expand Down Expand Up @@ -33,7 +34,7 @@ pub struct MetadataResponse<'a> {
pub async fn get_site_metadata(state: AppState) -> impl IntoResponse {
let read_only = state.config.db.are_all_read_only();

let deployed_sha = dotenvy::var("HEROKU_SLUG_COMMIT");
let deployed_sha = commit().ok().flatten();
let deployed_sha = deployed_sha.as_deref().unwrap_or("unknown");

Json(MetadataResponse {
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use tracing::warn;
/// be set if a DSN is provided.
///
/// `HEROKU_SLUG_COMMIT`, if present, will be used as the `release` property
/// on all events.
/// on all events. This environment variable is provided by Heroku when the
/// `runtime-dyno-metadata` Labs feature is enabled.
pub fn init() -> Option<ClientInitGuard> {
let config = match SentryConfig::from_environment() {
Ok(config) => config,
Expand Down