@git.zone/cli 8.3.2 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/helpers.workflow.d.ts +29 -0
  3. package/dist_ts/helpers.workflow.js +37 -2
  4. package/dist_ts/mod_config/index.js +45 -3
  5. package/dist_ts/mod_release/classes.releasejournal.d.ts +19 -3
  6. package/dist_ts/mod_release/classes.releasejournal.js +108 -13
  7. package/dist_ts/mod_release/helpers.giteaassets.d.ts +47 -1
  8. package/dist_ts/mod_release/helpers.giteaassets.js +236 -56
  9. package/dist_ts/mod_release/helpers.npmartifact.d.ts +68 -7
  10. package/dist_ts/mod_release/helpers.npmartifact.js +192 -26
  11. package/dist_ts/mod_release/helpers.releasebranch.d.ts +11 -0
  12. package/dist_ts/mod_release/helpers.releasebranch.js +88 -61
  13. package/dist_ts/mod_release/helpers.releaseline.d.ts +60 -0
  14. package/dist_ts/mod_release/helpers.releaseline.js +129 -0
  15. package/dist_ts/mod_release/helpers.releasephase.d.ts +9 -0
  16. package/dist_ts/mod_release/helpers.releasephase.js +25 -0
  17. package/dist_ts/mod_release/helpers.releasepreparation.js +6 -3
  18. package/dist_ts/mod_release/helpers.releasepublication.d.ts +12 -1
  19. package/dist_ts/mod_release/helpers.releasepublication.js +235 -52
  20. package/dist_ts/mod_release/helpers.releaseversion.d.ts +2 -0
  21. package/dist_ts/mod_release/helpers.releaseversion.js +4 -2
  22. package/dist_ts/mod_release/index.js +149 -44
  23. package/dist_ts/mod_release/interfaces.giteaassets.d.ts +16 -0
  24. package/package.json +5 -5
  25. package/readme.md +224 -17
  26. package/ts/00_commitinfo_data.ts +1 -1
  27. package/ts/helpers.workflow.ts +87 -0
  28. package/ts/mod_config/index.ts +50 -1
  29. package/ts/mod_release/classes.releasejournal.ts +165 -14
  30. package/ts/mod_release/helpers.giteaassets.ts +319 -64
  31. package/ts/mod_release/helpers.npmartifact.ts +302 -22
  32. package/ts/mod_release/helpers.releasebranch.ts +120 -45
  33. package/ts/mod_release/helpers.releaseline.ts +214 -0
  34. package/ts/mod_release/helpers.releasephase.ts +26 -0
  35. package/ts/mod_release/helpers.releasepreparation.ts +12 -3
  36. package/ts/mod_release/helpers.releasepublication.ts +425 -78
  37. package/ts/mod_release/helpers.releaseversion.ts +5 -1
  38. package/ts/mod_release/index.ts +213 -32
  39. package/ts/mod_release/interfaces.giteaassets.ts +19 -0
@@ -11,6 +11,7 @@ import {
11
11
  type TReleaseJournal,
12
12
  type IReleaseJournalV2,
13
13
  type IReleaseJournalV4,
14
+ type IReleaseNpmRegistryJournal,
14
15
  type IReleaseTargetStatus,
15
16
  type TReleaseErrorCode,
16
17
  type TReleaseTargetState,
@@ -18,9 +19,16 @@ import {
18
19
  import {
19
20
  GiteaAssetsClient,
20
21
  GiteaAssetConflictError,
22
+ defaultGiteaAssetConcurrency,
23
+ maximumGiteaAssetConcurrency,
21
24
  validateGiteaAssets,
22
25
  type IGiteaAssetTransferLimits,
26
+ type IGiteaFrozenAsset,
23
27
  } from "./helpers.giteaassets.js";
28
+ import {
29
+ formatReleaseDuration,
30
+ printReleasePhase,
31
+ } from "./helpers.releasephase.js";
24
32
  import { assertGiteaAssetsGitDestination } from "../helpers.workflow.js";
25
33
  import {
26
34
  defaultNpmVerificationWindowMinutes,
@@ -29,6 +37,7 @@ import {
29
37
  probeAnonymousNpmArtifact,
30
38
  probeAuthenticatedNpmArtifact,
31
39
  publishNpmArtifact,
40
+ readNpmDistTags,
32
41
  verifyStoredNpmArtifact,
33
42
  waitForAnonymousNpmArtifact,
34
43
  waitForAuthenticatedNpmArtifact,
@@ -38,6 +47,12 @@ import {
38
47
  type INpmArtifactProbeOptions,
39
48
  type INpmArtifactProbeResult,
40
49
  } from "./helpers.npmartifact.js";
50
+ import {
51
+ describeLatestMovedConflict,
52
+ findDistTagPublicationRefusal,
53
+ getReleaseBranch,
54
+ latestDistTag,
55
+ } from "./helpers.releaseline.js";
41
56
  import { isAbsentGitOid, releaseGitEnv } from "./helpers.releasebranch.js";
42
57
  import { describeCommandFailure } from "./helpers.commandfailure.js";
43
58
  import {
@@ -63,6 +78,8 @@ const gitPushTimeoutMs = 5 * 60_000;
63
78
 
64
79
  export interface IBuildReleaseGitPushArgsOptions {
65
80
  destination: string;
81
+ /** `main`, or the branch of the journaled release line. */
82
+ releaseBranch: string;
66
83
  newVersion: string;
67
84
  pushBranch: boolean;
68
85
  pushTags: boolean;
@@ -76,7 +93,7 @@ export function buildReleaseGitPushArgs(
76
93
  ): string[] {
77
94
  const refspecs: string[] = [];
78
95
  if (optionsArg.pushBranch) {
79
- refspecs.push(`${optionsArg.mainOid}:refs/heads/main`);
96
+ refspecs.push(`${optionsArg.mainOid}:refs/heads/${optionsArg.releaseBranch}`);
80
97
  }
81
98
  if (optionsArg.pushTags) {
82
99
  refspecs.push(`${optionsArg.tagOid}:refs/tags/v${optionsArg.newVersion}`);
@@ -87,7 +104,7 @@ export function buildReleaseGitPushArgs(
87
104
  }
88
105
  if (optionsArg.pushBranch && optionsArg.expectedRemoteMainOid) {
89
106
  args.push(
90
- `--force-with-lease=refs/heads/main:${optionsArg.expectedRemoteMainOid}`,
107
+ `--force-with-lease=refs/heads/${optionsArg.releaseBranch}:${optionsArg.expectedRemoteMainOid}`,
91
108
  );
92
109
  }
93
110
  args.push(optionsArg.destination, ...refspecs);
@@ -132,6 +149,8 @@ export interface IReleasePublicationOptions {
132
149
  tsdocker?: ITsdockerReleaseClient;
133
150
  /** Transfer bounds for the Gitea client built from the journal's destination. */
134
151
  giteaAssetTransfer?: IGiteaAssetTransferLimits;
152
+ /** Attachments published at the same time; default 2. */
153
+ giteaAssetConcurrency?: number;
135
154
  /** Bound and retries of the Git target's remote inspection. */
136
155
  gitRemotePolicy?: IReleaseGitRemotePolicy;
137
156
  giteaAssets?: Pick<
@@ -142,6 +161,7 @@ export interface IReleasePublicationOptions {
142
161
  | "inspectAssets"
143
162
  | "uploadAsset"
144
163
  | "verifyComplete"
164
+ | "publishRelease"
145
165
  >;
146
166
  ensurePublicationState: (expectedRemoteMainOidArg?: string) => Promise<void>;
147
167
  }
@@ -231,15 +251,25 @@ const updateTarget = async (
231
251
  );
232
252
  }
233
253
  updateArg(status);
234
- return finalizeJournalCompletion(currentArg);
254
+ const finalized = finalizeJournalCompletion(currentArg);
255
+ // A Gitea target never completes a journal on its own: completion is
256
+ // recorded only after the complete attachment set verified.
257
+ return isGiteaSelector(selectorArg) && currentArg.completedAt === null
258
+ ? { ...finalized, completedAt: null }
259
+ : finalized;
235
260
  },
236
261
  );
237
262
  };
238
263
 
264
+ const isGiteaSelector = (selectorArg: TReleaseTargetSelector): boolean =>
265
+ selectorArg.kind === "gitea-release" || selectorArg.kind === "gitea-asset";
266
+
239
267
  const claimTarget = async (
240
268
  storeArg: ReleaseJournalStore,
241
269
  journalArg: TReleaseJournal,
242
270
  selectorArg: TReleaseTargetSelector,
271
+ /** A release line records the `latest` version it found, see {@link IReleaseNpmRegistryJournal}. */
272
+ previousLatestArg?: string,
243
273
  ): Promise<TReleaseJournal> =>
244
274
  updateTarget(
245
275
  storeArg,
@@ -252,6 +282,10 @@ const claimTarget = async (
252
282
  statusArg.attempts += 1;
253
283
  statusArg.attempt = createReleaseAttempt();
254
284
  statusArg.error = null;
285
+ if (previousLatestArg !== undefined) {
286
+ (statusArg as IReleaseNpmRegistryJournal).previousLatest =
287
+ previousLatestArg;
288
+ }
255
289
  },
256
290
  );
257
291
 
@@ -329,6 +363,7 @@ const inspectRemoteRelease = async (
329
363
  journalArg: TReleaseJournal,
330
364
  ): Promise<IGitProbeResult> => {
331
365
  const tagRef = `refs/tags/${journalArg.release.tag}`;
366
+ const releaseBranchRef = `refs/heads/${getReleaseBranch(journalArg.release.line)}`;
332
367
  const policy = optionsArg.gitRemotePolicy ?? createReleaseGitRemotePolicy();
333
368
  const result = await runGitRemoteInspection({
334
369
  smartshell: optionsArg.smartshell,
@@ -336,7 +371,7 @@ const inspectRemoteRelease = async (
336
371
  args: [
337
372
  "ls-remote",
338
373
  destinationArg,
339
- "refs/heads/main",
374
+ releaseBranchRef,
340
375
  tagRef,
341
376
  `${tagRef}^{}`,
342
377
  ],
@@ -354,7 +389,7 @@ const inspectRemoteRelease = async (
354
389
  const match = line.trim().match(/^([0-9a-f]{40}|[0-9a-f]{64})\s+(.+)$/);
355
390
  if (match) refs.set(match[2], match[1]);
356
391
  }
357
- const mainOid = refs.get("refs/heads/main");
392
+ const mainOid = refs.get(releaseBranchRef);
358
393
  const tagOid = refs.get(tagRef);
359
394
  const tagCommitOid = refs.get(`${tagRef}^{}`);
360
395
  // A published release is identified by its annotated tag. Main carrying
@@ -508,6 +543,7 @@ const executeGitTarget = async (
508
543
  "git",
509
544
  buildReleaseGitPushArgs({
510
545
  destination: optionsArg.pushUrl,
546
+ releaseBranch: getReleaseBranch(journal.release.line),
511
547
  newVersion: journal.release.version,
512
548
  pushBranch: true,
513
549
  pushTags: true,
@@ -668,6 +704,46 @@ const assertNpmProbeAuthenticated = (
668
704
  }
669
705
  };
670
706
 
707
+ /** A line that found `latest` on its own version is a dist-tag conflict, not a byte conflict. */
708
+ const npmConflictCode = (
709
+ probeArg: INpmArtifactProbeResult,
710
+ ): TReleaseErrorCode =>
711
+ probeArg.code === "dist-tag-conflict"
712
+ ? "dist-tag-conflict"
713
+ : "artifact-conflict";
714
+
715
+ /**
716
+ * The error a conflicting probe ends the run with. A dist-tag conflict names
717
+ * the command that restores `latest`; gitzone never writes dist-tags itself.
718
+ */
719
+ const npmConflictError = (
720
+ journalArg: TReleaseJournal,
721
+ selectorArg: Extract<TReleaseTargetSelector, { kind: "npm" }>,
722
+ probeArg: INpmArtifactProbeResult,
723
+ byteConflictMessageArg: string,
724
+ ): Error => {
725
+ if (probeArg.code !== "dist-tag-conflict") {
726
+ return new Error(byteConflictMessageArg);
727
+ }
728
+ const registry = getTargetStatus(
729
+ journalArg,
730
+ selectorArg,
731
+ ) as IReleaseNpmRegistryJournal;
732
+ return new Error(
733
+ describeLatestMovedConflict(
734
+ {
735
+ packageName: getReleaseNpmArtifact(journalArg, selectorArg.packageName)
736
+ .packageName,
737
+ registry: selectorArg.registry,
738
+ version: journalArg.release.version,
739
+ distTag: journalArg.npm.tag,
740
+ },
741
+ probeArg.observedLatest,
742
+ registry.previousLatest,
743
+ ),
744
+ );
745
+ };
746
+
671
747
  const markNpmProbeFailure = async (
672
748
  optionsArg: IReleasePublicationOptions,
673
749
  journalArg: TReleaseJournal,
@@ -682,10 +758,72 @@ const markNpmProbeFailure = async (
682
758
  selectorArg,
683
759
  expectedStatesArg,
684
760
  conflict ? "conflict" : "failed",
685
- conflict ? "artifact-conflict" : "verification-inconclusive",
761
+ conflict ? npmConflictCode(probeArg) : "verification-inconclusive",
686
762
  );
687
763
  };
688
764
 
765
+ /**
766
+ * Right before a version is published, the registry's dist-tags must still
767
+ * allow it: the guard the release ran in preflight, repeated per package and
768
+ * registry, because a registry can change between the two. A refusal is a
769
+ * terminal dist-tag conflict and nothing is published. A line returns the
770
+ * `latest` version it found, which its claim records.
771
+ */
772
+ const assertNpmDistTagsBeforePublication = async (
773
+ optionsArg: IReleasePublicationOptions,
774
+ journalArg: TReleaseJournal,
775
+ selectorArg: Extract<TReleaseTargetSelector, { kind: "npm" }>,
776
+ ): Promise<string | undefined> => {
777
+ const packageName = getReleaseNpmArtifact(
778
+ journalArg,
779
+ selectorArg.packageName,
780
+ ).packageName;
781
+ const read = await readNpmDistTags(
782
+ journalArg.npm.access,
783
+ {
784
+ smartshell: optionsArg.smartshell,
785
+ cwd: optionsArg.cwd,
786
+ capability: optionsArg.pnpmCapability,
787
+ },
788
+ selectorArg.registry,
789
+ packageName,
790
+ optionsArg.npmProbeOptions,
791
+ );
792
+ if (read.status === "inconclusive") {
793
+ if (read.code === "authentication-required") {
794
+ throw new NpmRegistryAuthenticationError(
795
+ selectorArg.registry,
796
+ `run \`gitzone release resume ${journalArg.release.version} -y\``,
797
+ read.failure,
798
+ );
799
+ }
800
+ throw new Error(
801
+ `Unable to read the dist-tags of ${packageName} at ${selectorArg.registry} (${read.code}); nothing was published. Run \`gitzone release resume ${journalArg.release.version} -y\` once the registry answers.`,
802
+ );
803
+ }
804
+ const distTags = read.status === "present" ? read.distTags : undefined;
805
+ const refusal = findDistTagPublicationRefusal(distTags, {
806
+ packageName,
807
+ registry: selectorArg.registry,
808
+ version: journalArg.release.version,
809
+ distTag: journalArg.npm.tag,
810
+ });
811
+ if (refusal) {
812
+ await markUnclaimedTarget(
813
+ optionsArg.store,
814
+ journalArg,
815
+ selectorArg,
816
+ ["pending", "failed"],
817
+ "conflict",
818
+ "dist-tag-conflict",
819
+ );
820
+ throw new Error(`${refusal} Nothing was published.`);
821
+ }
822
+ if (journalArg.npm.tag === latestDistTag) return undefined;
823
+ const latest = distTags?.[latestDistTag];
824
+ return typeof latest === "string" ? latest : undefined;
825
+ };
826
+
689
827
  const executeNpmRegistry = async (
690
828
  optionsArg: IReleasePublicationOptions,
691
829
  journalArg: TReleaseJournal,
@@ -693,7 +831,7 @@ const executeNpmRegistry = async (
693
831
  packageNameArg?: string,
694
832
  deferVerificationArg = false,
695
833
  ): Promise<TReleaseJournal> => {
696
- const selector: TReleaseTargetSelector = {
834
+ const selector: Extract<TReleaseTargetSelector, { kind: "npm" }> = {
697
835
  kind: "npm",
698
836
  registry: registryArg,
699
837
  packageName: packageNameArg,
@@ -721,9 +859,12 @@ const executeNpmRegistry = async (
721
859
  selector,
722
860
  ["verified"],
723
861
  "conflict",
724
- "artifact-conflict",
862
+ npmConflictCode(probe),
725
863
  );
726
- throw new Error(
864
+ throw npmConflictError(
865
+ journal,
866
+ selector,
867
+ probe,
727
868
  `Previously verified npm registry drifted: ${registryArg}`,
728
869
  );
729
870
  }
@@ -747,9 +888,12 @@ const executeNpmRegistry = async (
747
888
  journal,
748
889
  selector,
749
890
  "conflict",
750
- "artifact-conflict",
891
+ npmConflictCode(probe),
751
892
  );
752
- throw new Error(
893
+ throw npmConflictError(
894
+ journal,
895
+ selector,
896
+ probe,
753
897
  `npm registry contains conflicting bytes: ${registryArg}`,
754
898
  );
755
899
  }
@@ -794,9 +938,14 @@ const executeNpmRegistry = async (
794
938
  selector,
795
939
  ["accepted"],
796
940
  "conflict",
797
- "artifact-conflict",
941
+ npmConflictCode(probeArg),
942
+ );
943
+ throw npmConflictError(
944
+ journal,
945
+ selector,
946
+ probeArg,
947
+ `npm registry contains conflicting bytes: ${registryArg}`,
798
948
  );
799
- throw new Error(`npm registry contains conflicting bytes: ${registryArg}`);
800
949
  };
801
950
  const alreadyReadable = await settleAccepted(probe);
802
951
  if (alreadyReadable) return alreadyReadable;
@@ -867,7 +1016,12 @@ const executeNpmRegistry = async (
867
1016
  ["pending", "failed"],
868
1017
  probe,
869
1018
  );
870
- throw new Error(`npm registry cannot be verified safely: ${registryArg}`);
1019
+ throw npmConflictError(
1020
+ journal,
1021
+ selector,
1022
+ probe,
1023
+ `npm registry cannot be verified safely: ${registryArg}`,
1024
+ );
871
1025
  }
872
1026
 
873
1027
  const artifact = getReleaseNpmArtifact(journal, packageNameArg);
@@ -878,13 +1032,24 @@ const executeNpmRegistry = async (
878
1032
  await verifyStoredNpmArtifact(artifactPath, artifact);
879
1033
  // Reject a missing invocation before claiming a potentially publishing target.
880
1034
  buildQualifiedPnpmArgs(optionsArg.pnpmCapability, []);
881
- journal = await claimTarget(optionsArg.store, journal, selector);
1035
+ const previousLatest = await assertNpmDistTagsBeforePublication(
1036
+ optionsArg,
1037
+ journal,
1038
+ selector,
1039
+ );
1040
+ journal = await claimTarget(
1041
+ optionsArg.store,
1042
+ journal,
1043
+ selector,
1044
+ previousLatest,
1045
+ );
882
1046
  const publishResult = await publishNpmArtifact(
883
1047
  optionsArg.smartshell,
884
1048
  optionsArg.cwd,
885
1049
  artifactPath,
886
1050
  registryArg,
887
1051
  optionsArg.pnpmCapability,
1052
+ journal.npm.tag,
888
1053
  journal.npm.access,
889
1054
  artifact.packageName,
890
1055
  );
@@ -911,9 +1076,14 @@ const executeNpmRegistry = async (
911
1076
  journal,
912
1077
  selector,
913
1078
  "conflict",
914
- "artifact-conflict",
1079
+ npmConflictCode(probe),
1080
+ );
1081
+ throw npmConflictError(
1082
+ journal,
1083
+ selector,
1084
+ probe,
1085
+ `npm registry published conflicting bytes: ${registryArg}`,
915
1086
  );
916
- throw new Error(`npm registry published conflicting bytes: ${registryArg}`);
917
1087
  }
918
1088
  if (npmPublishWasAccepted(publishResult)) {
919
1089
  journal = await finishPublishingTarget(
@@ -1611,37 +1781,76 @@ const executeDockerPromotion = async (
1611
1781
  );
1612
1782
  };
1613
1783
 
1784
+ /**
1785
+ * Serializes journal transactions of work that runs concurrently: every
1786
+ * update starts from the journal the previous one produced, so concurrent
1787
+ * attachment publications never race on the journal revision.
1788
+ */
1789
+ interface IReleaseJournalWriter {
1790
+ current(): TReleaseJournal;
1791
+ write(
1792
+ updateArg: (journalArg: TReleaseJournal) => Promise<TReleaseJournal>,
1793
+ ): Promise<TReleaseJournal>;
1794
+ }
1795
+
1796
+ const createReleaseJournalWriter = (
1797
+ journalArg: TReleaseJournal,
1798
+ ): IReleaseJournalWriter => {
1799
+ let current = journalArg;
1800
+ let tail: Promise<unknown> = Promise.resolve();
1801
+ return {
1802
+ current: () => current,
1803
+ write: (updateArg) => {
1804
+ const next = tail.then(async () => {
1805
+ current = await updateArg(current);
1806
+ return current;
1807
+ });
1808
+ tail = next.catch(() => undefined);
1809
+ return next;
1810
+ },
1811
+ };
1812
+ };
1813
+
1814
+ const requireGiteaJournal = (
1815
+ journalArg: TReleaseJournal,
1816
+ ): IReleaseJournalV4 => {
1817
+ if (journalArg.schemaVersion !== 4)
1818
+ throw new Error("Release journal has no Gitea assets.");
1819
+ return journalArg;
1820
+ };
1821
+
1614
1822
  const executeGiteaTarget = async (
1615
1823
  options: IReleasePublicationOptions,
1616
- journalArg: TReleaseJournal,
1824
+ writer: IReleaseJournalWriter,
1617
1825
  selector: TReleaseTargetSelector,
1618
1826
  /** `verifyBytes` is false only for a target the journal already recorded as verified. */
1619
1827
  inspect: (verifyBytes: boolean) => Promise<boolean>,
1620
1828
  publish: () => Promise<void>,
1621
1829
  onVerified?: (target: IReleaseTargetStatus) => void,
1622
- ): Promise<TReleaseJournal> => {
1623
- let journal = journalArg;
1624
- let status = getTargetStatus(journal, selector);
1830
+ ): Promise<void> => {
1831
+ const status = getTargetStatus(writer.current(), selector);
1625
1832
  if (status.state === "conflict")
1626
1833
  throw new Error("Gitea publication has a recorded destination conflict.");
1627
1834
  const mark = async (
1628
1835
  state: "verified" | "failed" | "conflict",
1629
1836
  error: TReleaseErrorCode | null,
1630
1837
  ) => {
1631
- status = getTargetStatus(journal, selector);
1632
- journal = await updateTarget(
1633
- options.store,
1634
- journal,
1635
- selector,
1636
- status.state,
1637
- status.attempt?.id || null,
1638
- (target) => {
1639
- target.state = state;
1640
- target.error = error;
1641
- target.attempt = null;
1642
- if (state === "verified") onVerified?.(target);
1643
- },
1644
- );
1838
+ await writer.write(async (journal) => {
1839
+ const current = getTargetStatus(journal, selector);
1840
+ return updateTarget(
1841
+ options.store,
1842
+ journal,
1843
+ selector,
1844
+ current.state,
1845
+ current.attempt?.id || null,
1846
+ (target) => {
1847
+ target.state = state;
1848
+ target.error = error;
1849
+ target.attempt = null;
1850
+ if (state === "verified") onVerified?.(target);
1851
+ },
1852
+ );
1853
+ });
1645
1854
  };
1646
1855
  let present: boolean;
1647
1856
  try {
@@ -1655,7 +1864,7 @@ const executeGiteaTarget = async (
1655
1864
  }
1656
1865
  if (present) {
1657
1866
  if (status.state !== "verified") await mark("verified", null);
1658
- return journal;
1867
+ return;
1659
1868
  }
1660
1869
  if (status.state === "verified") {
1661
1870
  await mark("conflict", "destination-conflict");
@@ -1664,15 +1873,19 @@ const executeGiteaTarget = async (
1664
1873
  );
1665
1874
  }
1666
1875
  if (status.state === "publishing") {
1667
- journal = await recoverPublishingTarget(
1668
- options.store,
1669
- journal,
1670
- selector,
1671
- options.recoveryAttemptId,
1876
+ await writer.write((journal) =>
1877
+ recoverPublishingTarget(
1878
+ options.store,
1879
+ journal,
1880
+ selector,
1881
+ options.recoveryAttemptId,
1882
+ ),
1672
1883
  );
1673
1884
  }
1674
- await options.ensurePublicationState(journal.release.mainOid);
1675
- journal = await claimTarget(options.store, journal, selector);
1885
+ await options.ensurePublicationState(writer.current().release.mainOid);
1886
+ await writer.write((journal) =>
1887
+ claimTarget(options.store, journal, selector),
1888
+ );
1676
1889
  // One mutation per claimed attempt. Reconcile an accepted request even when
1677
1890
  // its response was lost; never overwrite an attachment or recreate a tag.
1678
1891
  let publicationError: unknown;
@@ -1694,7 +1907,7 @@ const executeGiteaTarget = async (
1694
1907
  }
1695
1908
  if (present) {
1696
1909
  await mark("verified", null);
1697
- return journal;
1910
+ return;
1698
1911
  }
1699
1912
  await mark(
1700
1913
  publicationError instanceof GiteaAssetConflictError ? "conflict" : "failed",
@@ -1710,15 +1923,78 @@ const executeGiteaTarget = async (
1710
1923
  );
1711
1924
  };
1712
1925
 
1926
+ /**
1927
+ * Runs `workArg` for every item with at most `concurrencyArg` at a time,
1928
+ * starting them in the items' order. After the first failure no further item starts; the ones
1929
+ * already running finish, because cutting a running transfer off would leave
1930
+ * its result inconclusive. Every failure is reported.
1931
+ */
1932
+ export const runBoundedInOrder = async <T>(
1933
+ itemsArg: readonly T[],
1934
+ concurrencyArg: number,
1935
+ workArg: (itemArg: T) => Promise<void>,
1936
+ describeArg: (itemArg: T) => string,
1937
+ ): Promise<void> => {
1938
+ const failures: { item: T; error: unknown }[] = [];
1939
+ let nextIndex = 0;
1940
+ const worker = async (): Promise<void> => {
1941
+ while (failures.length === 0 && nextIndex < itemsArg.length) {
1942
+ const item = itemsArg[nextIndex];
1943
+ nextIndex += 1;
1944
+ try {
1945
+ await workArg(item);
1946
+ } catch (error) {
1947
+ failures.push({ item, error });
1948
+ }
1949
+ }
1950
+ };
1951
+ await Promise.all(
1952
+ Array.from(
1953
+ { length: Math.max(1, Math.min(concurrencyArg, itemsArg.length)) },
1954
+ () => worker(),
1955
+ ),
1956
+ );
1957
+ if (failures.length === 1) throw failures[0].error;
1958
+ if (failures.length > 1)
1959
+ throw new AggregateError(
1960
+ failures.map(({ error }) => error),
1961
+ `${failures.length} publications failed: ${failures
1962
+ .map(
1963
+ ({ item, error }) =>
1964
+ `${describeArg(item)}: ${error instanceof Error ? error.message : String(error)}`,
1965
+ )
1966
+ .join("; ")}`,
1967
+ );
1968
+ };
1969
+
1970
+ const resolveGiteaAssetConcurrency = (valueArg: number | undefined): number => {
1971
+ const value = valueArg ?? defaultGiteaAssetConcurrency;
1972
+ if (
1973
+ !Number.isSafeInteger(value) ||
1974
+ value < 1 ||
1975
+ value > maximumGiteaAssetConcurrency
1976
+ )
1977
+ throw new Error(
1978
+ `Gitea asset concurrency must be a whole number between 1 and ${maximumGiteaAssetConcurrency}.`,
1979
+ );
1980
+ return value;
1981
+ };
1982
+
1713
1983
  const executeGiteaAssets = async (
1714
1984
  options: IReleasePublicationOptions,
1715
1985
  journalArg: IReleaseJournalV4,
1716
1986
  client: NonNullable<IReleasePublicationOptions["giteaAssets"]>,
1717
1987
  ): Promise<IReleaseJournalV4> => {
1988
+ const concurrency = resolveGiteaAssetConcurrency(
1989
+ options.giteaAssetConcurrency,
1990
+ );
1991
+ const writer = createReleaseJournalWriter(journalArg);
1718
1992
  let releaseId = journalArg.giteaAssets.release.id;
1719
- let journal = (await executeGiteaTarget(
1993
+ let releaseIsDraft = true;
1994
+ printReleasePhase("Gitea release");
1995
+ await executeGiteaTarget(
1720
1996
  options,
1721
- journalArg,
1997
+ writer,
1722
1998
  { kind: "gitea-release" },
1723
1999
  async () => {
1724
2000
  const release = await client.inspectRelease();
@@ -1726,56 +2002,117 @@ const executeGiteaAssets = async (
1726
2002
  if (releaseId !== null && release.id !== releaseId)
1727
2003
  throw new GiteaAssetConflictError("Gitea release identity changed.");
1728
2004
  releaseId = release.id;
2005
+ releaseIsDraft = release.draft;
1729
2006
  return true;
1730
2007
  },
1731
2008
  async () => {
1732
2009
  await client.createRelease();
1733
2010
  },
1734
2011
  (target) => {
1735
- (target as IReleaseJournalV4["giteaAssets"]["release"]).id = releaseId;
2012
+ if ("id" in target) target.id = releaseId;
1736
2013
  },
1737
- )) as IReleaseJournalV4;
1738
- if (releaseId === null)
2014
+ );
2015
+ const verifiedReleaseId = releaseId;
2016
+ if (verifiedReleaseId === null)
1739
2017
  throw new Error("Gitea release has no verified identity.");
2018
+ const journal = requireGiteaJournal(writer.current());
1740
2019
  const expected = journal.giteaAssets.assets.map((entry) => entry.asset);
1741
2020
  const directory = options.store.getReleaseDirectory(journal.release.version);
1742
- for (const { asset } of journal.giteaAssets.assets) {
1743
- journal = (await executeGiteaTarget(
1744
- options,
1745
- journal,
1746
- { kind: "gitea-asset", name: asset.name },
1747
- async (verifyBytes) => {
1748
- const inspection = await client.inspectAssets(
1749
- releaseId!,
1750
- expected,
1751
- verifyBytes ? [asset] : [],
2021
+ // A draft stays invisible until every attachment verified, so no attachment
2022
+ // is public before the notices and sources beside it: the largest start
2023
+ // first, which shortens the whole publication. A release an older gitzone
2024
+ // already published shows every attachment the moment it lands, so its
2025
+ // missing attachments upload one at a time in manifest order, sources and
2026
+ // notices first, as that gitzone did.
2027
+ const order = releaseIsDraft
2028
+ ? expected
2029
+ .map((asset, index) => ({ asset, index }))
2030
+ .sort((a, b) => b.asset.size - a.asset.size || a.index - b.index)
2031
+ .map(({ asset }) => asset)
2032
+ : expected;
2033
+ const effectiveConcurrency = releaseIsDraft ? concurrency : 1;
2034
+ printReleasePhase(
2035
+ releaseIsDraft
2036
+ ? `Gitea assets: ${expected.length} attachments into the draft, ${effectiveConcurrency} at a time, largest first`
2037
+ : `Gitea assets: ${expected.length} attachments into the already published release, one at a time in manifest order`,
2038
+ );
2039
+ await runBoundedInOrder(
2040
+ order,
2041
+ effectiveConcurrency,
2042
+ async (asset: IGiteaFrozenAsset) => {
2043
+ let startedAt: number | undefined;
2044
+ await executeGiteaTarget(
2045
+ options,
2046
+ writer,
2047
+ { kind: "gitea-asset", name: asset.name },
2048
+ async (verifyBytes) => {
2049
+ const inspection = await client.inspectAssets(
2050
+ verifiedReleaseId,
2051
+ expected,
2052
+ verifyBytes ? [asset] : [],
2053
+ );
2054
+ return inspection.present.some((entry) => entry.name === asset.name);
2055
+ },
2056
+ async () => {
2057
+ startedAt = Date.now();
2058
+ printReleasePhase(
2059
+ `Gitea asset ${asset.name}: uploading ${asset.size} bytes`,
2060
+ );
2061
+ await validateGiteaAssets(directory, [asset]);
2062
+ await client.uploadAsset(verifiedReleaseId, asset, directory);
2063
+ },
2064
+ );
2065
+ if (startedAt !== undefined)
2066
+ printReleasePhase(
2067
+ `Gitea asset ${asset.name}: uploaded, downloaded and hashed in ${formatReleaseDuration(Date.now() - startedAt)}`,
1752
2068
  );
1753
- return inspection.present.some((entry) => entry.name === asset.name);
1754
- },
1755
- async () => {
1756
- await validateGiteaAssets(directory, expected);
1757
- await client.uploadAsset(releaseId!, asset, directory);
1758
- },
1759
- )) as IReleaseJournalV4;
1760
- }
1761
- // One full anonymous verification immediately before entering the npm phase.
1762
- // Resume always repeats it, including an otherwise completed journal.
2069
+ },
2070
+ (asset) => `Gitea asset ${asset.name}`,
2071
+ );
2072
+ // One full verification of the set immediately before publishing it and
2073
+ // before npm: authenticated while the release is a draft, anonymous for a
2074
+ // published one. The client downloads again every attachment it did not
2075
+ // itself hash in this run, so a resume, even of a completed journal,
2076
+ // downloads them all.
2077
+ printReleasePhase("Gitea assets: verifying the complete attachment set");
1763
2078
  try {
1764
- await client.verifyComplete(releaseId, expected);
2079
+ await client.verifyComplete(verifiedReleaseId, expected);
2080
+ // Publish the draft only now. A release published by an older gitzone is
2081
+ // only verified. Setting `draft: false` is idempotent and observable, so
2082
+ // it needs no journal attempt: a resume after a crash finds the release
2083
+ // published, verifies it and continues.
2084
+ printReleasePhase("Gitea release: publishing the verified draft");
2085
+ await client.publishRelease(verifiedReleaseId, expected);
1765
2086
  } catch (error) {
1766
2087
  if (error instanceof GiteaAssetConflictError) {
1767
- await markUnclaimedTarget(
1768
- options.store,
1769
- journal,
1770
- { kind: "gitea-release" },
1771
- ["verified"],
1772
- "conflict",
1773
- "destination-conflict",
2088
+ await writer.write((current) =>
2089
+ markUnclaimedTarget(
2090
+ options.store,
2091
+ current,
2092
+ { kind: "gitea-release" },
2093
+ ["verified"],
2094
+ "conflict",
2095
+ "destination-conflict",
2096
+ ),
1774
2097
  );
1775
2098
  }
1776
2099
  throw error;
1777
2100
  }
1778
- return journal;
2101
+ // Only now may a journal whose last target was a Gitea attachment record
2102
+ // its completion.
2103
+ const verified = writer.current();
2104
+ if (
2105
+ verified.completedAt === null &&
2106
+ finalizeJournalCompletion(verified).completedAt !== null
2107
+ )
2108
+ await writer.write((current) =>
2109
+ options.store.transact(
2110
+ current.release.version,
2111
+ current.revision,
2112
+ (latest) => finalizeJournalCompletion(latest),
2113
+ ),
2114
+ );
2115
+ return requireGiteaJournal(writer.current());
1779
2116
  };
1780
2117
 
1781
2118
  export const executeReleasePublication = async (
@@ -1817,6 +2154,7 @@ export const executeReleasePublication = async (
1817
2154
  await giteaClient.preflight();
1818
2155
  }
1819
2156
  if (journal.schemaVersion === 2) {
2157
+ printReleasePhase("Docker qualification");
1820
2158
  journal = await executeDockerQualification(optionsArg, journal);
1821
2159
  if (
1822
2160
  journal.docker.cleanup.state === "publishing" ||
@@ -1825,6 +2163,7 @@ export const executeReleasePublication = async (
1825
2163
  journal = await executeDockerCleanup(optionsArg, journal);
1826
2164
  }
1827
2165
  }
2166
+ printReleasePhase("Git target");
1828
2167
  journal = await executeGitTarget(optionsArg, journal);
1829
2168
  if (journal.schemaVersion === 4)
1830
2169
  journal = await executeGiteaAssets(optionsArg, journal, giteaClient!);
@@ -1833,6 +2172,9 @@ export const executeReleasePublication = async (
1833
2172
  ? journal.release.mainOid
1834
2173
  : journal.git.expectedRemoteMainOid || undefined;
1835
2174
  for (const registry of journal.npm.registries) {
2175
+ printReleasePhase(
2176
+ `npm ${describeNpmTarget(registry.registry, registry.packageName)}`,
2177
+ );
1836
2178
  await optionsArg.ensurePublicationState(expectedRemoteMainOid);
1837
2179
  journal = await executeNpmRegistry(
1838
2180
  optionsArg,
@@ -1847,6 +2189,9 @@ export const executeReleasePublication = async (
1847
2189
  if (hasReleasePackages(journal)) {
1848
2190
  for (const registry of journal.npm.registries) {
1849
2191
  if (registry.state !== "accepted") continue;
2192
+ printReleasePhase(
2193
+ `npm availability of ${describeNpmTarget(registry.registry, registry.packageName)}`,
2194
+ );
1850
2195
  await optionsArg.ensurePublicationState(expectedRemoteMainOid);
1851
2196
  journal = await executeNpmRegistry(
1852
2197
  optionsArg,
@@ -1876,12 +2221,14 @@ export const executeReleasePublication = async (
1876
2221
  journal.docker.cleanup.state !== "verified"
1877
2222
  ) {
1878
2223
  for (const promotion of journal.docker.promotions) {
2224
+ printReleasePhase(`Docker promotion ${promotion.promotionId}`);
1879
2225
  journal = await executeDockerPromotion(
1880
2226
  optionsArg,
1881
2227
  journal,
1882
2228
  promotion.promotionId,
1883
2229
  );
1884
2230
  }
2231
+ printReleasePhase("Docker cleanup");
1885
2232
  journal = await executeDockerCleanup(optionsArg, journal);
1886
2233
  }
1887
2234
  return journal;