@git.zone/cli 8.5.0 → 8.6.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.
@@ -19,11 +19,13 @@ import {
19
19
  import {
20
20
  GiteaAssetsClient,
21
21
  GiteaAssetConflictError,
22
+ GiteaReleaseStateChangedError,
22
23
  defaultGiteaAssetConcurrency,
23
24
  maximumGiteaAssetConcurrency,
24
25
  validateGiteaAssets,
25
26
  type IGiteaAssetTransferLimits,
26
27
  type IGiteaFrozenAsset,
28
+ type IGiteaRelease,
27
29
  } from "./helpers.giteaassets.js";
28
30
  import {
29
31
  formatReleaseDuration,
@@ -149,7 +151,7 @@ export interface IReleasePublicationOptions {
149
151
  tsdocker?: ITsdockerReleaseClient;
150
152
  /** Transfer bounds for the Gitea client built from the journal's destination. */
151
153
  giteaAssetTransfer?: IGiteaAssetTransferLimits;
152
- /** Attachments published at the same time; default 2. */
154
+ /** Attachments of a draft in progress at the same time (1 or 2); default 1. */
153
155
  giteaAssetConcurrency?: number;
154
156
  /** Bound and retries of the Git target's remote inspection. */
155
157
  gitRemotePolicy?: IReleaseGitRemotePolicy;
@@ -1967,6 +1969,16 @@ export const runBoundedInOrder = async <T>(
1967
1969
  );
1968
1970
  };
1969
1971
 
1972
+ /** Runs the tasks it is given one at a time, in the order they were given. */
1973
+ const createExclusiveSlot = (): (<T>(taskArg: () => Promise<T>) => Promise<T>) => {
1974
+ let tail: Promise<unknown> = Promise.resolve();
1975
+ return <T>(taskArg: () => Promise<T>): Promise<T> => {
1976
+ const run = tail.then(taskArg);
1977
+ tail = run.catch(() => undefined);
1978
+ return run;
1979
+ };
1980
+ };
1981
+
1970
1982
  const resolveGiteaAssetConcurrency = (valueArg: number | undefined): number => {
1971
1983
  const value = valueArg ?? defaultGiteaAssetConcurrency;
1972
1984
  if (
@@ -1990,7 +2002,11 @@ const executeGiteaAssets = async (
1990
2002
  );
1991
2003
  const writer = createReleaseJournalWriter(journalArg);
1992
2004
  let releaseId = journalArg.giteaAssets.release.id;
1993
- let releaseIsDraft = true;
2005
+ const observed: {
2006
+ /** The release as Gitea last reported it; never the state this run intended. */
2007
+ release: IGiteaRelease | null;
2008
+ createdHere: boolean;
2009
+ } = { release: null, createdHere: false };
1994
2010
  printReleasePhase("Gitea release");
1995
2011
  await executeGiteaTarget(
1996
2012
  options,
@@ -2002,40 +2018,67 @@ const executeGiteaAssets = async (
2002
2018
  if (releaseId !== null && release.id !== releaseId)
2003
2019
  throw new GiteaAssetConflictError("Gitea release identity changed.");
2004
2020
  releaseId = release.id;
2005
- releaseIsDraft = release.draft;
2021
+ observed.release = release;
2006
2022
  return true;
2007
2023
  },
2008
2024
  async () => {
2009
- await client.createRelease();
2025
+ observed.createdHere = true;
2026
+ await client.createRelease((elapsedMs) =>
2027
+ printReleasePhase(
2028
+ `Gitea release: waiting for Gitea to process the pushed tag ${journalArg.release.tag} (${formatReleaseDuration(elapsedMs)})`,
2029
+ ),
2030
+ );
2010
2031
  },
2011
2032
  (target) => {
2012
2033
  if ("id" in target) target.id = releaseId;
2013
2034
  },
2014
2035
  );
2015
2036
  const verifiedReleaseId = releaseId;
2016
- if (verifiedReleaseId === null)
2037
+ const release = observed.release;
2038
+ if (verifiedReleaseId === null || release === null)
2017
2039
  throw new Error("Gitea release has no verified identity.");
2040
+ // The draft this run created must read back as a draft; Gitea refusing it
2041
+ // must not turn into a public upload.
2042
+ if (observed.createdHere && !release.draft)
2043
+ throw new GiteaReleaseStateChangedError(
2044
+ `Gitea release ${journalArg.release.tag} was created public instead of as a draft; no attachment was uploaded. A resume continues it as a published release, one attachment at a time in manifest order.`,
2045
+ );
2046
+ const releaseIsDraft = release.draft;
2018
2047
  const journal = requireGiteaJournal(writer.current());
2019
2048
  const expected = journal.giteaAssets.assets.map((entry) => entry.asset);
2020
2049
  const directory = options.store.getReleaseDirectory(journal.release.version);
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;
2050
+ // Order and concurrency follow the state Gitea reported. A draft stays
2051
+ // invisible until every attachment verified, so no attachment is public
2052
+ // before the notices and sources beside it, and every upload first checks
2053
+ // that the release is still a draft. Pipelined (concurrency 2), the next
2054
+ // attachment uploads while the previous one is verified, largest first, so
2055
+ // the last verification is a short one; one at a time, attachments go in
2056
+ // manifest order. A published release (one an older gitzone created, or a
2057
+ // draft published before its set was complete) shows every attachment the
2058
+ // moment it lands, so its missing attachments upload one at a time in
2059
+ // manifest order, sources and notices first.
2033
2060
  const effectiveConcurrency = releaseIsDraft ? concurrency : 1;
2061
+ const order =
2062
+ effectiveConcurrency > 1
2063
+ ? expected
2064
+ .map((asset, index) => ({ asset, index }))
2065
+ .sort((a, b) => b.asset.size - a.asset.size || a.index - b.index)
2066
+ .map(({ asset }) => asset)
2067
+ : expected;
2034
2068
  printReleasePhase(
2035
2069
  releaseIsDraft
2036
- ? `Gitea assets: ${expected.length} attachments into the draft, ${effectiveConcurrency} at a time, largest first`
2070
+ ? effectiveConcurrency === 1
2071
+ ? `Gitea assets: ${expected.length} attachments into the draft, one at a time in manifest order`
2072
+ : `Gitea assets: ${expected.length} attachments into the draft, largest first, each uploading while the previous one is verified`
2037
2073
  : `Gitea assets: ${expected.length} attachments into the already published release, one at a time in manifest order`,
2038
2074
  );
2075
+ // Two transfers in the same direction share the link and each takes longer
2076
+ // than alone, long enough for a large upload to outlast the reverse proxy in
2077
+ // front of Gitea (HTTP 504). So uploads never overlap and neither do
2078
+ // verification downloads, whatever the concurrency; only an upload and
2079
+ // another attachment's verification download may run at the same time.
2080
+ const uploadSlot = createExclusiveSlot();
2081
+ const downloadSlot = createExclusiveSlot();
2039
2082
  await runBoundedInOrder(
2040
2083
  order,
2041
2084
  effectiveConcurrency,
@@ -2046,20 +2089,29 @@ const executeGiteaAssets = async (
2046
2089
  writer,
2047
2090
  { kind: "gitea-asset", name: asset.name },
2048
2091
  async (verifyBytes) => {
2049
- const inspection = await client.inspectAssets(
2050
- verifiedReleaseId,
2051
- expected,
2052
- verifyBytes ? [asset] : [],
2092
+ const inspection = await downloadSlot(() =>
2093
+ client.inspectAssets(
2094
+ verifiedReleaseId,
2095
+ expected,
2096
+ verifyBytes ? [asset] : [],
2097
+ ),
2053
2098
  );
2054
2099
  return inspection.present.some((entry) => entry.name === asset.name);
2055
2100
  },
2056
2101
  async () => {
2057
- startedAt = Date.now();
2058
- printReleasePhase(
2059
- `Gitea asset ${asset.name}: uploading ${asset.size} bytes`,
2060
- );
2061
2102
  await validateGiteaAssets(directory, [asset]);
2062
- await client.uploadAsset(verifiedReleaseId, asset, directory);
2103
+ await uploadSlot(async () => {
2104
+ startedAt = Date.now();
2105
+ printReleasePhase(
2106
+ `Gitea asset ${asset.name}: uploading ${asset.size} bytes`,
2107
+ );
2108
+ await client.uploadAsset(
2109
+ verifiedReleaseId,
2110
+ asset,
2111
+ directory,
2112
+ releaseIsDraft,
2113
+ );
2114
+ });
2063
2115
  },
2064
2116
  );
2065
2117
  if (startedAt !== undefined)
@@ -2081,8 +2133,12 @@ const executeGiteaAssets = async (
2081
2133
  // only verified. Setting `draft: false` is idempotent and observable, so
2082
2134
  // it needs no journal attempt: a resume after a crash finds the release
2083
2135
  // published, verifies it and continues.
2084
- printReleasePhase("Gitea release: publishing the verified draft");
2085
- await client.publishRelease(verifiedReleaseId, expected);
2136
+ printReleasePhase(
2137
+ releaseIsDraft
2138
+ ? "Gitea release: publishing the verified draft"
2139
+ : "Gitea release: verifying the already published release",
2140
+ );
2141
+ await client.publishRelease(verifiedReleaseId, expected, releaseIsDraft);
2086
2142
  } catch (error) {
2087
2143
  if (error instanceof GiteaAssetConflictError) {
2088
2144
  await writer.write((current) =>
@@ -57,6 +57,13 @@ export interface IGiteaAssetsClientOptions
57
57
  commit: string;
58
58
  token: string;
59
59
  fetchImplementation?: typeof fetch;
60
+ /**
61
+ * Longest wait for Gitea to process the pushed release tag before the draft
62
+ * is created; default 15 minutes.
63
+ */
64
+ tagProcessingTimeoutMs?: number;
65
+ /** Pause between two checks whether Gitea processed the tag; default 5 s. */
66
+ tagProcessingPollIntervalMs?: number;
60
67
  }
61
68
 
62
69
  export interface IGiteaAssetPreflight {
@@ -68,17 +75,31 @@ export interface IGiteaAssetPreflight {
68
75
  export interface IGiteaAssetsClient {
69
76
  preflight(): Promise<IGiteaAssetPreflight>;
70
77
  inspectRelease(): Promise<IGiteaRelease | null>;
71
- createRelease(): Promise<IGiteaRelease>;
78
+ /**
79
+ * Waits until Gitea has processed the pushed tag, then creates the draft.
80
+ * Gitea processes a tag push asynchronously and publishes any draft it then
81
+ * finds for the tag, so a draft created earlier would turn public on its own.
82
+ * `onWaiting` is called when the wait starts and about once a minute after.
83
+ */
84
+ createRelease(
85
+ onWaiting?: (elapsedMs: number) => void,
86
+ ): Promise<IGiteaRelease>;
72
87
  /** Downloads and hashes only the present attachments named in `verify`. */
73
88
  inspectAssets(
74
89
  releaseId: number,
75
90
  expected: IGiteaFrozenAsset[],
76
91
  verify?: readonly IGiteaFrozenAsset[],
77
92
  ): Promise<IGiteaAssetInspection>;
93
+ /**
94
+ * Uploads one attachment, only while the release is still a draft exactly
95
+ * when `expectedDraft` says so; otherwise a GiteaReleaseStateChangedError
96
+ * stops before any byte is sent.
97
+ */
78
98
  uploadAsset(
79
99
  releaseId: number,
80
100
  asset: IGiteaFrozenAsset,
81
101
  artifactDirectory: string,
102
+ expectedDraft: boolean,
82
103
  ): Promise<void>;
83
104
  /**
84
105
  * The complete set, nothing extra, every frozen identity unchanged. Downloads
@@ -96,9 +117,13 @@ export interface IGiteaAssetsClient {
96
117
  * listed attachment must be the one this client hashed (id and UUID; the id
97
118
  * alone on a server without UUIDs), otherwise a destination conflict leaves
98
119
  * the release a draft. An already published release is only verified.
120
+ * `expectedDraft` is the state the attachments were uploaded for: a draft
121
+ * that something else published meanwhile stops with a
122
+ * GiteaReleaseStateChangedError instead of passing as already published.
99
123
  */
100
124
  publishRelease(
101
125
  releaseId: number,
102
126
  expected: IGiteaFrozenAsset[],
127
+ expectedDraft: boolean,
103
128
  ): Promise<void>;
104
129
  }