release-tool 0.2.1

Configuration-driven release lifecycle for computed-parameter repositories
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
use super::{
    PublicationReceipt, PublicationState, Publisher, VerificationReport, receipt, validate_manifest,
};
use crate::command::{CommandRequest, CommandRunner};
use crate::config::PublisherConfig;
use crate::domain::{ArtifactIdentity, ArtifactManifest, PreparedArtifact, TargetPlan};
use crate::lifecycle::expand;
use anyhow::{Context, Result, bail};
use serde::Deserialize;
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

pub struct GithubReleasePublisher {
    root: PathBuf,
    repository: String,
    name: String,
    title: String,
    prerelease: bool,
    runner: Arc<dyn CommandRunner>,
}

#[derive(Clone, Debug, Deserialize)]
struct ReleaseInfo {
    tag_name: String,
    prerelease: bool,
    #[serde(default)]
    draft: bool,
    assets: Vec<ReleaseAsset>,
}

#[derive(Clone, Debug, Deserialize)]
struct ReleaseAsset {
    name: String,
}

impl GithubReleasePublisher {
    pub fn new(
        root: impl Into<PathBuf>,
        repository: &str,
        name: &str,
        config: &PublisherConfig,
        runner: Arc<dyn CommandRunner>,
    ) -> Result<Self> {
        let PublisherConfig::GithubRelease { title, prerelease } = config else {
            bail!("publisher `{name}` is not a github_release publisher");
        };
        Ok(Self {
            root: root.into(),
            repository: repository.to_owned(),
            name: name.to_owned(),
            title: title.clone(),
            prerelease: *prerelease,
            runner,
        })
    }

    fn release_info(&self, plan: &TargetPlan) -> Result<Option<ReleaseInfo>> {
        let endpoint = format!(
            "repos/{}/releases/tags/{}",
            self.repository, plan.release.tag
        );
        let request = CommandRequest::new("gh", ["api", endpoint.as_str()], &self.root);
        let result = self.runner.execute(&request)?;
        if result.status != 0 {
            if result.stderr.contains("404") || result.stderr.contains("Not Found") {
                return Ok(None);
            }
            return Err(result
                .require_success("query GitHub Release")
                .expect_err("non-zero command must fail"));
        }
        let info: ReleaseInfo =
            serde_json::from_str(&result.stdout).context("invalid GitHub Release response")?;
        Ok(Some(info))
    }

    fn plan_for_manifest(manifest: &ArtifactManifest) -> TargetPlan {
        TargetPlan {
            name: manifest.target.clone(),
            publisher: manifest.publisher.clone(),
            release: manifest.release.clone(),
            artifacts: manifest
                .artifacts
                .iter()
                .map(|artifact| artifact.identity.clone())
                .collect(),
        }
    }

    fn inspect_info(&self, plan: &TargetPlan, info: Option<&ReleaseInfo>) -> PublicationState {
        let Some(info) = info else {
            return PublicationState::Absent;
        };
        if info.tag_name != plan.release.tag.to_string() {
            return PublicationState::Invalid {
                reason: format!(
                    "GitHub Release tag is {}, expected {}",
                    info.tag_name, plan.release.tag
                ),
            };
        }
        if info.draft {
            return PublicationState::Invalid {
                reason: "GitHub Release is still a draft; automatic publication is unsafe"
                    .to_owned(),
            };
        }
        if info.prerelease != self.prerelease {
            return PublicationState::Invalid {
                reason: format!(
                    "GitHub Release prerelease is {}, expected {}",
                    info.prerelease, self.prerelease
                ),
            };
        }
        let remote: HashSet<_> = info
            .assets
            .iter()
            .map(|asset| asset.name.as_str())
            .collect();
        if remote.len() != info.assets.len() {
            return PublicationState::Invalid {
                reason: "GitHub Release contains duplicate asset names".to_owned(),
            };
        }
        let expected = expected_asset_names(plan);
        let present: Vec<_> = expected
            .iter()
            .filter(|name| remote.contains(name.as_str()))
            .cloned()
            .collect();
        let missing: Vec<_> = expected
            .iter()
            .filter(|name| !remote.contains(name.as_str()))
            .cloned()
            .collect();
        if present.is_empty() {
            PublicationState::Absent
        } else if missing.is_empty() {
            PublicationState::Complete
        } else {
            PublicationState::Partial { present, missing }
        }
    }

    fn upload_missing(&self, manifest: &ArtifactManifest, missing: &[String]) -> Result<()> {
        let mut arguments = vec![
            "release".to_owned(),
            "upload".to_owned(),
            manifest.release.tag.to_string(),
        ];
        for name in missing {
            let artifact = artifact_by_name(manifest, name)?;
            arguments.push(artifact.path.display().to_string());
        }
        arguments.extend(["--repo".to_owned(), self.repository.clone()]);
        self.run(arguments, "upload GitHub Release assets")?;
        Ok(())
    }

    fn create(&self, manifest: &ArtifactManifest) -> Result<()> {
        let version = manifest.release.tag.to_string();
        let title = expand(&self.title, &version, None)?;
        let mut arguments = vec!["release".to_owned(), "create".to_owned(), version];
        for artifact in &manifest.artifacts {
            arguments.push(artifact.path.display().to_string());
        }
        arguments.extend([
            "--repo".to_owned(),
            self.repository.clone(),
            "--verify-tag".to_owned(),
            "--target".to_owned(),
            manifest.release.commit.clone(),
            "--title".to_owned(),
            title,
            "--notes".to_owned(),
            format!(
                "Commit: `{}`\nTool: `release-tool/{}`",
                manifest.release.commit,
                env!("CARGO_PKG_VERSION")
            ),
            "--latest=false".to_owned(),
        ]);
        if self.prerelease {
            arguments.push("--prerelease".to_owned());
        }
        self.run(arguments, "create GitHub Release")?;
        Ok(())
    }

    fn verify_selected(
        &self,
        manifest: &ArtifactManifest,
        names: impl IntoIterator<Item = String>,
    ) -> Result<Vec<String>> {
        let temporary = tempfile::tempdir().context("failed to create verification directory")?;
        let mut verified = Vec::new();
        for name in names {
            let expected = artifact_by_name(manifest, &name)?;
            let directory = temporary.path().join(verified.len().to_string());
            fs::create_dir_all(&directory)?;
            self.run(
                vec![
                    "release".to_owned(),
                    "download".to_owned(),
                    manifest.release.tag.to_string(),
                    "--pattern".to_owned(),
                    name.clone(),
                    "--dir".to_owned(),
                    directory.display().to_string(),
                    "--repo".to_owned(),
                    self.repository.clone(),
                ],
                "download GitHub Release asset",
            )?;
            let actual_path = directory.join(&name);
            let actual = sha256(&actual_path)?;
            if actual != expected.sha256 {
                bail!(
                    "remote asset digest mismatch for {name}: expected {}, found {actual}",
                    expected.sha256
                );
            }
            verified.push(name);
        }
        Ok(verified)
    }

    fn download(&self, tag: &str, name: &str, directory: &Path) -> Result<PathBuf> {
        fs::create_dir_all(directory)?;
        self.run(
            vec![
                "release".to_owned(),
                "download".to_owned(),
                tag.to_owned(),
                "--pattern".to_owned(),
                name.to_owned(),
                "--dir".to_owned(),
                directory.display().to_string(),
                "--repo".to_owned(),
                self.repository.clone(),
            ],
            "download GitHub Release asset",
        )?;
        Ok(directory.join(name))
    }

    fn run(&self, arguments: Vec<String>, description: &str) -> Result<()> {
        let request = CommandRequest::new("gh", arguments, &self.root);
        self.runner
            .execute(&request)?
            .require_success(description)?;
        Ok(())
    }
}

impl Publisher for GithubReleasePublisher {
    fn inspect(&self, plan: &TargetPlan) -> Result<PublicationState> {
        let info = self.release_info(plan)?;
        Ok(self.inspect_info(plan, info.as_ref()))
    }

    fn publish(&self, manifest: &ArtifactManifest) -> Result<PublicationReceipt> {
        if !manifest.release.tag_already_sealed {
            bail!(
                "release {} is not sealed on the remote",
                manifest.release.tag
            );
        }
        if manifest.publisher != self.name {
            bail!(
                "artifact manifest belongs to publisher `{}`",
                manifest.publisher
            );
        }
        validate_manifest(manifest)?;
        let plan = Self::plan_for_manifest(manifest);
        let info = self.release_info(&plan)?;
        let state = self.inspect_info(&plan, info.as_ref());
        let write_result = match state {
            PublicationState::Complete => {
                self.verify(manifest)?;
                return Ok(receipt(manifest, &self.name, true));
            }
            PublicationState::Invalid { reason } => bail!("invalid publication: {reason}"),
            PublicationState::Partial { present, missing } => {
                self.verify_selected(manifest, present)?;
                self.upload_missing(manifest, &missing)
            }
            PublicationState::Absent if info.is_some() => {
                let missing = expected_asset_names(&plan);
                self.upload_missing(manifest, &missing)
            }
            PublicationState::Absent => self.create(manifest),
        };
        let state_after_write = self.inspect(&plan);
        match (write_result, state_after_write) {
            (_, Ok(PublicationState::Complete)) => {}
            (Err(write_error), Ok(state)) => {
                return Err(write_error.context(format!(
                    "GitHub Release write failed and remote state is {state:?}"
                )));
            }
            (Err(write_error), Err(inspect_error)) => {
                return Err(write_error.context(format!(
                    "GitHub Release write failed; remote reconciliation also failed: {inspect_error:#}"
                )));
            }
            (Ok(()), Ok(state)) => {
                bail!("GitHub Release is not complete after publish: {state:?}");
            }
            (Ok(()), Err(inspect_error)) => return Err(inspect_error),
        }
        self.verify(manifest)?;
        Ok(receipt(manifest, &self.name, false))
    }

    fn verify(&self, manifest: &ArtifactManifest) -> Result<VerificationReport> {
        let plan = Self::plan_for_manifest(manifest);
        match self.inspect(&plan)? {
            PublicationState::Complete => {}
            state => bail!("cannot verify incomplete GitHub Release: {state:?}"),
        }
        let names = expected_asset_names(&plan);
        let artifacts = self.verify_selected(manifest, names)?;
        Ok(VerificationReport {
            target: manifest.target.clone(),
            verified: true,
            artifacts,
        })
    }

    fn verify_existing(&self, plan: &TargetPlan) -> Result<VerificationReport> {
        match self.inspect(plan)? {
            PublicationState::Complete => {}
            state => bail!("cannot verify incomplete GitHub Release: {state:?}"),
        }
        let names = expected_asset_names(plan);
        let checksum_name = names
            .iter()
            .find(|name| name.ends_with(".sha256"))
            .context("GitHub Release target has no SHA-256 checksum asset")?;
        let archive_name = checksum_name.trim_end_matches(".sha256");
        if !names.iter().any(|name| name == archive_name) {
            bail!("checksum asset `{checksum_name}` has no matching archive asset");
        }
        let temporary = tempfile::tempdir().context("failed to create verification directory")?;
        let archive = self.download(
            &plan.release.tag.to_string(),
            archive_name,
            &temporary.path().join("archive"),
        )?;
        let checksum = self.download(
            &plan.release.tag.to_string(),
            checksum_name,
            &temporary.path().join("checksum"),
        )?;
        let checksum_source = fs::read_to_string(&checksum)
            .with_context(|| format!("failed to read {}", checksum.display()))?;
        let expected_line = checksum_source
            .strip_suffix('\n')
            .unwrap_or(&checksum_source);
        let (expected_digest, expected_name) = expected_line
            .split_once("  ")
            .context("invalid SHA-256 checksum asset")?;
        if expected_name != archive_name {
            bail!("checksum asset names `{expected_name}`, expected `{archive_name}`");
        }
        let actual_digest = sha256(&archive)?;
        if expected_digest != actual_digest {
            bail!(
                "remote archive digest mismatch for {archive_name}: checksum declares {expected_digest}, found {actual_digest}"
            );
        }
        Ok(VerificationReport {
            target: plan.name.clone(),
            verified: true,
            artifacts: names,
        })
    }
}

fn expected_asset_names(plan: &TargetPlan) -> Vec<String> {
    plan.artifacts
        .iter()
        .filter_map(|identity| match identity {
            ArtifactIdentity::GithubReleaseAsset { name } => Some(name.clone()),
            _ => None,
        })
        .collect()
}

fn artifact_by_name<'a>(
    manifest: &'a ArtifactManifest,
    name: &str,
) -> Result<&'a PreparedArtifact> {
    manifest
        .artifacts
        .iter()
        .find(|artifact| {
            matches!(
                &artifact.identity,
                ArtifactIdentity::GithubReleaseAsset { name: artifact_name }
                    if artifact_name == name
            )
        })
        .with_context(|| format!("artifact manifest does not contain `{name}`"))
}

fn sha256(path: &Path) -> Result<String> {
    let bytes = fs::read(path).with_context(|| format!("failed to read {}", path.display()))?;
    Ok(hex::encode(Sha256::digest(bytes)))
}