anodizer-core 0.25.1

Core configuration, context, and template engine for the anodizer release tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Release-asset download-URL derivation, shared by every consumer of
//! `metadata["url"]`.
//!
//! The download URL for a release asset is fully derivable from
//! `(provider, download_base, owner, repo, tag, asset name)` — all known
//! before any network call. The release stage stamps it after a live upload,
//! its dry-run stamps the identical derivation, and the emission-validate
//! pass seeds it ahead of the publisher renders (homebrew casks, winget,
//! scoop, …) so snapshot / dry-run pipelines validate the same URL a real
//! release would ship instead of failing on its absence.

use crate::config::{CrateConfig, ForceTokenKind, ReleaseConfig, ScmRepoConfig};
use crate::context::Context;
use crate::scm::ScmTokenType;
use anyhow::{Context as _, Result};

/// Seed the derived download URL onto every url-less artifact of one crate,
/// mirroring the release stage's derivation so a pre-release validation pass
/// (emission-validate) checks the SAME URL a real release would stamp instead
/// of failing on its absence.
///
/// Returns the URL prefix that was applied, or `None` when a real release
/// would leave the URLs absent too — no `release:` config, a truthy
/// `release.skip`, a `skip_upload` that suppresses uploads on a real release
/// (`auto` maps to `false` here: it skips only snapshots, and a snapshot
/// never runs the release stage at all — the question this pass answers is
/// "would a real release seed the URL?"), an unresolvable repo, or a tag
/// template that renders empty/erroneous. In each `None` case the downstream
/// publisher render surfaces its own actionable missing-url error, exactly as
/// the corresponding real run would.
pub fn seed_missing_download_urls_for_crate(
    ctx: &mut Context,
    crate_cfg: &CrateConfig,
) -> Result<Option<String>> {
    let Some(release_cfg) = crate_cfg.release.clone() else {
        return Ok(None);
    };
    if let Some(skip) = release_cfg.skip.as_ref()
        && skip.try_evaluates_to_true(|s| ctx.render_template(s))?
    {
        return Ok(None);
    }
    if let Some(su) = release_cfg.skip_upload.as_ref() {
        let rendered = if su.is_template() {
            ctx.render_template(su.as_str()).unwrap_or_default()
        } else {
            su.as_str().to_string()
        };
        if matches!(rendered.trim(), "true" | "1") {
            return Ok(None);
        }
    }

    // Same template-precedence the release stage's `resolve_release_tag`
    // applies: an explicit `release.tag:` override wins over `tag_template`.
    // A render error or empty result skips seeding rather than bailing — the
    // release stage owns the loud diagnostic for that config bug.
    let tag_tmpl = release_cfg
        .tag
        .as_deref()
        .unwrap_or(crate_cfg.resolved_tag_template());
    let tag = match ctx.render_template(tag_tmpl) {
        Ok(t) if !t.is_empty() => t,
        _ => return Ok(None),
    };

    let token_type = ctx.token_type;
    let Some(repo) = resolve_release_repo(&release_cfg, token_type, ctx)? else {
        return Ok(None);
    };
    let base = default_download_base(ctx);
    let prefix = release_download_url_prefix(token_type, &base, &repo.owner, &repo.name, &tag);
    seed_missing_artifact_download_urls(
        ctx,
        &crate_cfg.name,
        token_type,
        &base,
        &repo.owner,
        &repo.name,
        &tag,
    );
    Ok(Some(prefix))
}

/// The provider-shaped URL prefix every asset name is appended to:
/// `{base}/{owner}/{repo}/releases/download/{tag}` for GitHub/Gitea,
/// the `/-/releases/{tag}/downloads` form for GitLab (owner segment
/// omitted when empty — a top-level project with no namespace).
pub fn release_download_url_prefix(
    token_type: ScmTokenType,
    download_base: &str,
    owner: &str,
    repo: &str,
    tag: &str,
) -> String {
    let dl_base = download_base.trim_end_matches('/');
    let url_tag = crate::url::percent_encode_path_segment(tag);
    match token_type {
        ScmTokenType::GitLab => {
            if owner.is_empty() {
                format!("{dl_base}/{repo}/-/releases/{url_tag}/downloads")
            } else {
                format!("{dl_base}/{owner}/{repo}/-/releases/{url_tag}/downloads")
            }
        }
        ScmTokenType::GitHub | ScmTokenType::Gitea => {
            format!("{dl_base}/{owner}/{repo}/releases/download/{url_tag}")
        }
    }
}

/// Set `metadata["url"]` on every artifact for the given crate, constructing
/// the download URL from the SCM backend's download base, owner/repo, tag, and
/// artifact name. This lets publishers resolve download URLs without an
/// explicit `url_template`. Overwrites any prior value — the release stage's
/// post-upload call is authoritative.
pub fn populate_artifact_download_urls(
    ctx: &mut Context,
    crate_name: &str,
    token_type: ScmTokenType,
    download_base: &str,
    owner: &str,
    repo: &str,
    tag: &str,
) {
    set_artifact_download_urls(
        ctx,
        crate_name,
        token_type,
        download_base,
        owner,
        repo,
        tag,
        true,
    );
}

/// [`populate_artifact_download_urls`] that only fills artifacts still
/// MISSING a `url` — the emission-validate seeding path. Never clobbers a
/// value a release upload (or an earlier authoritative pass) already stamped.
pub fn seed_missing_artifact_download_urls(
    ctx: &mut Context,
    crate_name: &str,
    token_type: ScmTokenType,
    download_base: &str,
    owner: &str,
    repo: &str,
    tag: &str,
) {
    set_artifact_download_urls(
        ctx,
        crate_name,
        token_type,
        download_base,
        owner,
        repo,
        tag,
        false,
    );
}

#[allow(clippy::too_many_arguments)]
fn set_artifact_download_urls(
    ctx: &mut Context,
    crate_name: &str,
    token_type: ScmTokenType,
    download_base: &str,
    owner: &str,
    repo: &str,
    tag: &str,
    overwrite: bool,
) {
    let url_prefix = release_download_url_prefix(token_type, download_base, owner, repo, tag);
    for artifact in ctx.artifacts.all_mut() {
        if artifact.crate_name == crate_name
            && !artifact.name.is_empty()
            && (overwrite || !artifact.metadata.contains_key("url"))
        {
            let encoded_name = crate::url::percent_encode_path_segment(&artifact.name);
            artifact
                .metadata
                .insert("url".to_string(), format!("{url_prefix}/{encoded_name}"));
        }
    }
}

/// The provider's public download base when no live backend response is
/// available (dry-run, snapshot, offline seeding): the configured
/// `<provider>_urls.download` override, else the hosted default.
pub fn default_download_base(ctx: &Context) -> String {
    match ctx.token_type {
        ScmTokenType::GitHub => ctx
            .config
            .github_urls
            .as_ref()
            .and_then(|u| u.download.clone())
            .unwrap_or_else(|| "https://github.com".to_string()),
        ScmTokenType::GitLab => ctx
            .config
            .gitlab_urls
            .as_ref()
            .and_then(|u| u.download.clone())
            .unwrap_or_else(|| "https://gitlab.com".to_string()),
        ScmTokenType::Gitea => ctx
            .config
            .gitea_urls
            .as_ref()
            .and_then(|u| u.download.clone())
            .unwrap_or_else(|| {
                ctx.config
                    .gitea_urls
                    .as_ref()
                    .and_then(|u| u.api.as_deref())
                    .map(|api| {
                        api.trim_end_matches('/')
                            .trim_end_matches("/api/v1")
                            .to_string()
                    })
                    .unwrap_or_else(|| "https://gitea.com".to_string())
            }),
    }
}

/// Pick the `ScmRepoConfig` for the active publish target and template-render
/// its `owner` and `name` fields.
///
/// Resolution order:
/// 1. Explicit `release.provider:`.
/// 2. Active SCM token type with provider-side fallback (the historical
///    behaviour — preserved so existing configs don't change shape).
///
/// Returns `Ok(None)` when no matching block is configured.
pub fn resolve_release_repo(
    release_cfg: &ReleaseConfig,
    token_type: ScmTokenType,
    ctx: &Context,
) -> Result<Option<ScmRepoConfig>> {
    // Explicit `release.provider:` wins over token-type inference. This
    // is the cross-platform publishing seam: a project hosted on GitLab
    // (so `GITLAB_TOKEN` is the active token) can declare
    // `provider: github` to redirect publish output to GitHub.
    let raw = match release_cfg.provider {
        Some(ForceTokenKind::GitHub) => release_cfg.github.as_ref(),
        Some(ForceTokenKind::GitLab) => release_cfg.gitlab.as_ref(),
        Some(ForceTokenKind::Gitea) => release_cfg.gitea.as_ref(),
        None => match token_type {
            ScmTokenType::GitLab => release_cfg.gitlab.as_ref().or(release_cfg.github.as_ref()),
            ScmTokenType::Gitea => release_cfg.gitea.as_ref().or(release_cfg.github.as_ref()),
            ScmTokenType::GitHub => release_cfg.github.as_ref(),
        },
    };
    let Some(repo) = raw else {
        return Ok(None);
    };
    let owner = ctx
        .render_template(&repo.owner)
        .with_context(|| format!("release: render repo.owner '{}'", repo.owner))?;
    let name = ctx
        .render_template(&repo.name)
        .with_context(|| format!("release: render repo.name '{}'", repo.name))?;
    Ok(Some(ScmRepoConfig {
        owner,
        name,
        token: repo.token.clone(),
    }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::artifact::{Artifact, ArtifactKind};
    use crate::config::{Config, CrateConfig, StringOrBool};
    use crate::context::ContextOptions;
    use std::collections::HashMap;

    fn archive(crate_name: &str, name: &str, target: &str) -> Artifact {
        Artifact {
            kind: ArtifactKind::Archive,
            path: format!("dist/{name}").into(),
            name: name.to_string(),
            target: Some(target.to_string()),
            crate_name: crate_name.to_string(),
            metadata: HashMap::new(),
            size: None,
        }
    }

    fn crate_cfg_with_release() -> CrateConfig {
        CrateConfig {
            name: "myapp".to_string(),
            path: ".".to_string(),
            tag_template: Some("v{{ .Version }}".to_string()),
            release: Some(ReleaseConfig {
                github: Some(ScmRepoConfig {
                    owner: "octocat".to_string(),
                    name: "hello".to_string(),
                    token: None,
                }),
                ..Default::default()
            }),
            ..Default::default()
        }
    }

    fn ctx_with(crate_cfg: &CrateConfig, artifacts: Vec<Artifact>) -> Context {
        let config = Config {
            crates: vec![crate_cfg.clone()],
            ..Default::default()
        };
        let mut ctx = Context::new(config, ContextOptions::default());
        ctx.template_vars_mut().set("Version", "1.0.0");
        for a in artifacts {
            ctx.artifacts.add(a);
        }
        ctx
    }

    /// The seeding path derives the exact GitHub download URL a real release
    /// stamps — repo from `release.github`, tag from `tag_template`.
    #[test]
    fn seed_fills_missing_url_with_release_derivation() {
        let cfg = crate_cfg_with_release();
        let mut ctx = ctx_with(
            &cfg,
            vec![archive(
                "myapp",
                "myapp-1.0.0-aarch64-apple-darwin.tar.gz",
                "aarch64-apple-darwin",
            )],
        );
        let prefix = seed_missing_download_urls_for_crate(&mut ctx, &cfg)
            .unwrap()
            .expect("derivable config must seed");
        assert_eq!(
            prefix,
            "https://github.com/octocat/hello/releases/download/v1.0.0"
        );
        assert_eq!(
            ctx.artifacts.all()[0]
                .metadata
                .get("url")
                .map(String::as_str),
            Some(
                "https://github.com/octocat/hello/releases/download/v1.0.0/myapp-1.0.0-aarch64-apple-darwin.tar.gz"
            )
        );
    }

    /// A url stamped by an earlier authoritative pass (a release upload) must
    /// survive the seeding — fill-missing only, never overwrite.
    #[test]
    fn seed_never_overwrites_existing_url() {
        let cfg = crate_cfg_with_release();
        let mut art = archive("myapp", "myapp.tar.gz", "aarch64-apple-darwin");
        art.metadata.insert(
            "url".to_string(),
            "https://real.example/x.tar.gz".to_string(),
        );
        let mut ctx = ctx_with(&cfg, vec![art]);
        seed_missing_download_urls_for_crate(&mut ctx, &cfg).unwrap();
        assert_eq!(
            ctx.artifacts.all()[0]
                .metadata
                .get("url")
                .map(String::as_str),
            Some("https://real.example/x.tar.gz")
        );
    }

    /// A crate whose real release is skipped gets no seeded URL — the real run
    /// would leave it absent too, and the validators must see that truth.
    #[test]
    fn seed_skips_when_release_skipped_or_absent() {
        // release.skip: true
        let mut skipped = crate_cfg_with_release();
        skipped.release.as_mut().unwrap().skip = Some(StringOrBool::Bool(true));
        let mut ctx = ctx_with(&skipped, vec![archive("myapp", "a.tar.gz", "x")]);
        assert!(
            seed_missing_download_urls_for_crate(&mut ctx, &skipped)
                .unwrap()
                .is_none()
        );
        assert!(!ctx.artifacts.all()[0].metadata.contains_key("url"));

        // no release: block at all
        let mut no_release = crate_cfg_with_release();
        no_release.release = None;
        let mut ctx = ctx_with(&no_release, vec![archive("myapp", "a.tar.gz", "x")]);
        assert!(
            seed_missing_download_urls_for_crate(&mut ctx, &no_release)
                .unwrap()
                .is_none()
        );

        // skip_upload: true (a real release would never stamp urls)
        let mut no_upload = crate_cfg_with_release();
        no_upload.release.as_mut().unwrap().skip_upload = Some(StringOrBool::Bool(true));
        let mut ctx = ctx_with(&no_upload, vec![archive("myapp", "a.tar.gz", "x")]);
        assert!(
            seed_missing_download_urls_for_crate(&mut ctx, &no_upload)
                .unwrap()
                .is_none()
        );
    }

    /// `release.tag:` override wins over `tag_template`, mirroring
    /// `resolve_release_tag`'s precedence in the live release stage.
    #[test]
    fn seed_honors_release_tag_override() {
        let mut cfg = crate_cfg_with_release();
        cfg.release.as_mut().unwrap().tag = Some("app-v{{ .Version }}".to_string());
        let mut ctx = ctx_with(&cfg, vec![archive("myapp", "a.tar.gz", "x")]);
        let prefix = seed_missing_download_urls_for_crate(&mut ctx, &cfg)
            .unwrap()
            .unwrap();
        assert_eq!(
            prefix,
            "https://github.com/octocat/hello/releases/download/app-v1.0.0"
        );
    }

    /// Seeding scopes strictly by crate: a sibling crate's artifacts are
    /// untouched (per-crate config mode).
    #[test]
    fn seed_scopes_to_the_named_crate_only() {
        let cfg = crate_cfg_with_release();
        let mut ctx = ctx_with(
            &cfg,
            vec![
                archive("myapp", "a.tar.gz", "x"),
                archive("other", "b.tar.gz", "x"),
            ],
        );
        seed_missing_download_urls_for_crate(&mut ctx, &cfg).unwrap();
        let arts = ctx.artifacts.all();
        assert!(
            arts.iter()
                .any(|a| a.crate_name == "myapp" && a.metadata.contains_key("url"))
        );
        assert!(
            arts.iter()
                .all(|a| a.crate_name != "other" || !a.metadata.contains_key("url"))
        );
    }

    /// GitLab with an empty owner omits the namespace segment — same shape the
    /// release stage derives.
    #[test]
    fn prefix_gitlab_empty_owner_omits_namespace() {
        assert_eq!(
            release_download_url_prefix(
                ScmTokenType::GitLab,
                "https://gitlab.com/",
                "",
                "proj",
                "v1"
            ),
            "https://gitlab.com/proj/-/releases/v1/downloads"
        );
        assert_eq!(
            release_download_url_prefix(
                ScmTokenType::GitLab,
                "https://gitlab.com",
                "grp",
                "proj",
                "v1"
            ),
            "https://gitlab.com/grp/proj/-/releases/v1/downloads"
        );
    }
}