Skip to content

Commit 03e5f78

Browse files
committed
fix(xtask): review round 1
1 parent aaef376 commit 03e5f78

4 files changed

Lines changed: 73 additions & 51 deletions

File tree

‎docs/dev/releasing.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ toolchain of `rust-toolchain.toml`, Node and pnpm.
144144
5. For a release that breaks existing apps, edit its GitHub release to say how
145145
to upgrade.
146146

147-
The release notes list what was published, then the pull requests merged since
147+
The release notes list what was published, the npm package only when its
148+
version changed since the previous release, then the pull requests merged since
148149
the previous release, from the titles of their squashed commits on `main`:
149150
breaking changes (a `!` after the type, as in `feat(sdk)!:`), features (`feat`),
150151
fixes (`fix`), and the rest. A title's scope leads its line, in bold.
@@ -176,9 +177,10 @@ closed.
176177
- **Tagging fails.** Everything is published. If the tag was created but not
177178
pushed, remove it with `git tag --delete v<version>`. Then run `just release`
178179
again.
179-
- **Creating the GitHub release or closing the milestone fails.** The tag is
180-
pushed, so `just release` refuses to run again. Run the `gh` command it printed
181-
for the step that failed, and the ones after it.
180+
- **Creating the GitHub release fails.** The tag is pushed, so `just release`
181+
refuses to run again. Run the `gh release create` command it printed, then
182+
close the `v<major>.<minor>` milestone on GitHub.
183+
- **Closing the milestone fails.** Close it on GitHub.
182184
- **A published version is broken.** A version can't be replaced. Yank each
183185
crate with `cargo yank --version <version> <crate>`, deprecate the npm version
184186
with `npm deprecate @sourceorigin/auth@<version> "<why>"`, and release a fixed

‎tools/xtask/src/main.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ impl Workspace {
138138
Ok(&first.version)
139139
}
140140

141-
/// The TypeScript SDK's name and version, which it is published at.
142-
fn npm_package(&self) -> Result<NpmPackage> {
143-
let path = self.root.join(NPM_PACKAGE).join("package.json");
144-
let text = fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
145-
serde_json::from_str(&text).with_context(|| format!("parse {}", path.display()))
141+
/// The TypeScript SDK's name and version, which it is published at: in the
142+
/// working tree, or at `revision`, such as a release tag.
143+
fn npm_package(&self, revision: Option<&str>) -> Result<NpmPackage> {
144+
let path = format!("{NPM_PACKAGE}/package.json");
145+
let text = match revision {
146+
None => {
147+
fs::read_to_string(self.root.join(&path)).with_context(|| format!("read {path}"))?
148+
}
149+
Some(revision) => self.git(&["show", &format!("{revision}:{path}")])?,
150+
};
151+
serde_json::from_str(&text).with_context(|| format!("parse {path}"))
146152
}
147153

148154
/// Cargo, run from the workspace root.

‎tools/xtask/src/publish.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Publish<'_> {
9797

9898
/// Publishes the npm package, unless npm has its version already.
9999
fn npm(&self) -> Result<()> {
100-
let package = self.workspace.npm_package()?;
100+
let package = self.workspace.npm_package(None)?;
101101
let mut npm = self.workspace.command("npm");
102102
npm.arg("publish")
103103
.current_dir(self.workspace.root().join(NPM_PACKAGE));

‎tools/xtask/src/release.rs‎

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,45 @@ pub struct Release<'a> {
1010
workspace: &'a Workspace,
1111
/// The workspace version, such as `0.2.0`.
1212
version: String,
13-
/// The latest release tag `HEAD` contains, such as `v0.1.0`; `None` before
14-
/// the first release.
15-
previous: Option<String>,
1613
/// Runs the checks and the publish dry run, and only prints what the other
1714
/// steps would do.
1815
dry_run: bool,
1916
}
2017

2118
impl<'a> Release<'a> {
2219
pub fn new(workspace: &'a Workspace, dry_run: bool) -> Result<Self> {
23-
let tags = workspace.git(&[
24-
"tag",
25-
"--list",
26-
"v*",
27-
"--merged",
28-
"HEAD",
29-
"--sort=-v:refname",
30-
])?;
3120
Ok(Self {
3221
workspace,
3322
version: workspace.version()?.to_owned(),
34-
previous: tags.lines().next().map(str::to_owned),
3523
dry_run,
3624
})
3725
}
3826

3927
/// Checks that `HEAD` is the latest `origin/main`, that the version isn't
40-
/// tagged yet, and that the crates' API changes fit it. Then publishes the
41-
/// SDKs, tags `HEAD` `v<version>`, pushes the tag, creates its GitHub
42-
/// release, and closes its minor version's milestone, such as `v0.2`.
28+
/// tagged yet, and that the crates' API changes fit it. Then writes the
29+
/// release notes, publishes the SDKs, tags `HEAD` `v<version>`, pushes the
30+
/// tag, creates its GitHub release, and closes its minor version's
31+
/// milestone, such as `v0.2`.
4332
pub fn run(&self) -> Result<()> {
4433
self.check()?;
45-
self.semver()?;
34+
let previous = self.previous()?;
35+
let previous = previous.as_deref();
36+
self.semver(previous)?;
37+
let tag = self.tag();
38+
let notes = self
39+
.workspace
40+
.root()
41+
.join(format!("target/release-notes-{tag}.md"));
42+
fs::write(&notes, self.notes(previous)?)
43+
.with_context(|| format!("write {}", notes.display()))?;
44+
println!("the release notes are in {}", notes.display());
4645
Publish {
4746
workspace: self.workspace,
4847
dry_run: self.dry_run,
4948
allow_dirty: false,
5049
}
5150
.run()?;
52-
let tag = self.tag();
5351
let title = format!("SourceOrigin {}", self.version);
54-
let notes = self
55-
.workspace
56-
.root()
57-
.join(format!("target/release-notes-{tag}.md"));
58-
fs::write(&notes, self.notes()?).with_context(|| format!("write {}", notes.display()))?;
59-
println!("the release notes are in {}", notes.display());
6052
let notes = notes.to_string_lossy();
6153
self.step("git", &["tag", "--annotate", &tag, "--message", &title])?;
6254
self.step("git", &["push", "origin", &tag])?;
@@ -82,9 +74,10 @@ impl<'a> Release<'a> {
8274
}
8375

8476
/// Fails unless `HEAD` is the latest `origin/main`, and the version isn't
85-
/// tagged on `origin` yet.
77+
/// tagged on `origin` yet. Fetches `origin`'s tags too, so the previous
78+
/// release is known.
8679
fn check(&self) -> Result<()> {
87-
self.workspace.git(&["fetch", "origin", "main"])?;
80+
self.workspace.git(&["fetch", "--tags", "origin", "main"])?;
8881
ensure!(
8982
self.workspace.git(&["rev-parse", "HEAD"])?
9083
== self.workspace.git(&["rev-parse", "FETCH_HEAD"])?,
@@ -101,10 +94,27 @@ impl<'a> Release<'a> {
10194
Ok(())
10295
}
10396

104-
/// Fails when a published crate's API breaks compatibility with the previous
105-
/// release without a version that allows it, by `cargo semver-checks`.
106-
fn semver(&self) -> Result<()> {
107-
let Some(previous) = &self.previous else {
97+
/// The latest release tag `HEAD` contains, such as `v0.1.0`, other than this
98+
/// release's own, which a failed earlier run may have left; `None` before
99+
/// the first release.
100+
fn previous(&self) -> Result<Option<String>> {
101+
let tag = self.tag();
102+
let tags = self.workspace.git(&[
103+
"tag",
104+
"--list",
105+
"v*",
106+
"--merged",
107+
"HEAD",
108+
"--sort=-v:refname",
109+
])?;
110+
Ok(tags.lines().find(|line| *line != tag).map(str::to_owned))
111+
}
112+
113+
/// Fails when a published crate's API breaks compatibility with the
114+
/// `previous` release without a version that allows it, by
115+
/// `cargo semver-checks`.
116+
fn semver(&self, previous: Option<&str>) -> Result<()> {
117+
let Some(previous) = previous else {
108118
println!("no earlier release to check the crates' API against");
109119
return Ok(());
110120
};
@@ -122,9 +132,10 @@ impl<'a> Release<'a> {
122132
}
123133

124134
/// The release notes: what was published, then the pull requests merged
125-
/// since the previous release, by the titles' Conventional Commits type.
126-
fn notes(&self) -> Result<String> {
127-
let npm = self.workspace.npm_package()?;
135+
/// since the `previous` release, by the titles' Conventional Commits type.
136+
/// The npm package is listed only when its version changed since then,
137+
/// since only then is it published.
138+
fn notes(&self, previous: Option<&str>) -> Result<String> {
128139
let mut notes = String::from("Published:\n\n");
129140
for package in self.workspace.published() {
130141
let (name, version) = (&package.name, &package.version);
@@ -133,16 +144,19 @@ impl<'a> Release<'a> {
133144
"- [`{name}` {version}](https://crates.io/crates/{name}/{version}) on crates.io"
134145
)?;
135146
}
136-
let (name, version) = (&npm.name, &npm.version);
137-
writeln!(
138-
notes,
139-
"- [`{name}` {version}](https://www.npmjs.com/package/{name}/v/{version}) on npm"
140-
)?;
147+
let npm = self.workspace.npm_package(None)?;
148+
let released = previous
149+
.map(|tag| self.workspace.npm_package(Some(tag)))
150+
.transpose()?;
151+
if released.is_none_or(|released| released.version != npm.version) {
152+
let (name, version) = (&npm.name, &npm.version);
153+
writeln!(
154+
notes,
155+
"- [`{name}` {version}](https://www.npmjs.com/package/{name}/v/{version}) on npm"
156+
)?;
157+
}
141158

142-
let range = self
143-
.previous
144-
.as_ref()
145-
.map_or_else(|| "HEAD".to_owned(), |tag| format!("{tag}..HEAD"));
159+
let range = previous.map_or_else(|| "HEAD".to_owned(), |tag| format!("{tag}..HEAD"));
146160
let log =
147161
self.workspace
148162
.git(&["log", "--first-parent", "--reverse", "--format=%s", &range])?;

0 commit comments

Comments
 (0)