semantic-release 25.0.3 → 25.0.4
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.
- package/README.md +7 -0
- package/docs/extending/plugins-list.md +6 -0
- package/docs/recipes/ci-configurations/github-actions.md +0 -7
- package/index.js +37 -265
- package/lib/definitions/constants.js +9 -8
- package/package.json +17 -17
- package/lib/branches/expand.js +0 -18
- package/lib/branches/get-tags.js +0 -39
- package/lib/branches/index.js +0 -71
- package/lib/branches/normalize.js +0 -104
- package/lib/definitions/branches.js +0 -22
- package/lib/definitions/errors.js +0 -298
- package/lib/definitions/plugins.js +0 -108
- package/lib/get-commits.js +0 -31
- package/lib/get-config.js +0 -98
- package/lib/get-error.js +0 -7
- package/lib/get-git-auth-url.js +0 -126
- package/lib/get-last-release.js +0 -48
- package/lib/get-logger.js +0 -19
- package/lib/get-next-version.js +0 -35
- package/lib/get-release-to-add.js +0 -59
- package/lib/git.js +0 -381
- package/lib/plugins/index.js +0 -103
- package/lib/plugins/normalize.js +0 -69
- package/lib/plugins/pipeline.js +0 -59
- package/lib/plugins/utils.js +0 -81
- package/lib/utils.js +0 -101
- package/lib/verify.js +0 -43
package/lib/get-git-auth-url.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { format, parse } from "node:url";
|
|
2
|
-
import { isNil } from "lodash-es";
|
|
3
|
-
import hostedGitInfo from "hosted-git-info";
|
|
4
|
-
import debugAuthUrl from "debug";
|
|
5
|
-
import { verifyAuth } from "./git.js";
|
|
6
|
-
|
|
7
|
-
const debug = debugAuthUrl("semantic-release:get-git-auth-url");
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Machinery to format a repository URL with the given credentials
|
|
11
|
-
*
|
|
12
|
-
* @param {String} protocol URL protocol (which should not be present in repositoryUrl)
|
|
13
|
-
* @param {String} repositoryUrl User-given repository URL
|
|
14
|
-
* @param {String} gitCredentials The basic auth part of the URL
|
|
15
|
-
*
|
|
16
|
-
* @return {String} The formatted Git repository URL.
|
|
17
|
-
*/
|
|
18
|
-
function formatAuthUrl(protocol, repositoryUrl, gitCredentials) {
|
|
19
|
-
const [match, auth, host, basePort, path] =
|
|
20
|
-
/^(?!.+:\/\/)(?:(?<auth>.*)@)?(?<host>.*?):(?<port>\d+)?:?\/?(?<path>.*)$/.exec(repositoryUrl) || [];
|
|
21
|
-
const { port, hostname, ...parsed } = parse(
|
|
22
|
-
match ? `ssh://${auth ? `${auth}@` : ""}${host}${basePort ? `:${basePort}` : ""}/${path}` : repositoryUrl
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
return format({
|
|
26
|
-
...parsed,
|
|
27
|
-
auth: gitCredentials,
|
|
28
|
-
host: `${hostname}${protocol === "ssh:" ? "" : port ? `:${port}` : ""}`,
|
|
29
|
-
protocol: protocol && /http[^s]/.test(protocol) ? "http" : "https",
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Verify authUrl by calling git.verifyAuth, but don't throw on failure
|
|
35
|
-
*
|
|
36
|
-
* @param {Object} context semantic-release context.
|
|
37
|
-
* @param {String} authUrl Repository URL to verify
|
|
38
|
-
*
|
|
39
|
-
* @return {String} The authUrl as is if the connection was successful, null otherwise
|
|
40
|
-
*/
|
|
41
|
-
async function ensureValidAuthUrl({ cwd, env, branch }, authUrl) {
|
|
42
|
-
try {
|
|
43
|
-
await verifyAuth(authUrl, branch.name, { cwd, env });
|
|
44
|
-
return authUrl;
|
|
45
|
-
} catch (error) {
|
|
46
|
-
debug(error);
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Determine the git repository URL to use to push, either:
|
|
53
|
-
* - The `repositoryUrl` as is if allowed to push
|
|
54
|
-
* - The `repositoryUrl` converted to `https` or `http` with Basic Authentication
|
|
55
|
-
*
|
|
56
|
-
* In addition, expand shortcut URLs (`owner/repo` => `https://github.com/owner/repo.git`) and transform `git+https` / `git+http` URLs to `https` / `http`.
|
|
57
|
-
*
|
|
58
|
-
* @param {Object} context semantic-release context.
|
|
59
|
-
*
|
|
60
|
-
* @return {String} The formatted Git repository URL.
|
|
61
|
-
*/
|
|
62
|
-
export default async (context) => {
|
|
63
|
-
const { cwd, env, branch } = context;
|
|
64
|
-
const GIT_TOKENS = {
|
|
65
|
-
GIT_CREDENTIALS: undefined,
|
|
66
|
-
GH_TOKEN: undefined,
|
|
67
|
-
// GitHub Actions require the "x-access-token:" prefix for git access
|
|
68
|
-
// https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation
|
|
69
|
-
GITHUB_TOKEN: isNil(env.GITHUB_ACTION) ? undefined : "x-access-token:",
|
|
70
|
-
GL_TOKEN: "gitlab-ci-token:",
|
|
71
|
-
GITLAB_TOKEN: "gitlab-ci-token:",
|
|
72
|
-
BB_TOKEN: "x-token-auth:",
|
|
73
|
-
BITBUCKET_TOKEN: "x-token-auth:",
|
|
74
|
-
BB_TOKEN_BASIC_AUTH: "",
|
|
75
|
-
BITBUCKET_TOKEN_BASIC_AUTH: "",
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
let { repositoryUrl } = context.options;
|
|
79
|
-
const info = hostedGitInfo.fromUrl(repositoryUrl, { noGitPlus: true });
|
|
80
|
-
const { protocol, ...parsed } = parse(repositoryUrl);
|
|
81
|
-
|
|
82
|
-
if (info && info.getDefaultRepresentation() === "shortcut") {
|
|
83
|
-
// Expand shorthand URLs (such as `owner/repo` or `gitlab:owner/repo`)
|
|
84
|
-
repositoryUrl = info.https();
|
|
85
|
-
} else if (protocol && protocol.includes("http")) {
|
|
86
|
-
// Replace `git+https` and `git+http` with `https` or `http`
|
|
87
|
-
repositoryUrl = format({ ...parsed, protocol: protocol.includes("https") ? "https" : "http", href: null });
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Test if push is allowed without transforming the URL (e.g. is ssh keys are set up)
|
|
91
|
-
try {
|
|
92
|
-
debug("Verifying ssh auth by attempting to push to %s", repositoryUrl);
|
|
93
|
-
await verifyAuth(repositoryUrl, branch.name, { cwd, env });
|
|
94
|
-
} catch {
|
|
95
|
-
debug("SSH key auth failed, falling back to https.");
|
|
96
|
-
const envVars = Object.keys(GIT_TOKENS).filter((envVar) => !isNil(env[envVar]));
|
|
97
|
-
|
|
98
|
-
// Skip verification if there is no ambiguity on which env var to use for authentication
|
|
99
|
-
if (envVars.length === 1) {
|
|
100
|
-
const gitCredentials = `${GIT_TOKENS[envVars[0]] || ""}${env[envVars[0]]}`;
|
|
101
|
-
return formatAuthUrl(protocol, repositoryUrl, gitCredentials);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (envVars.length > 1) {
|
|
105
|
-
debug(`Found ${envVars.length} credentials in environment, trying all of them`);
|
|
106
|
-
|
|
107
|
-
const candidateRepositoryUrls = [];
|
|
108
|
-
for (const envVar of envVars) {
|
|
109
|
-
const gitCredentials = `${GIT_TOKENS[envVar] || ""}${env[envVar]}`;
|
|
110
|
-
const authUrl = formatAuthUrl(protocol, repositoryUrl, gitCredentials);
|
|
111
|
-
candidateRepositoryUrls.push(ensureValidAuthUrl(context, authUrl));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const validRepositoryUrls = await Promise.all(candidateRepositoryUrls);
|
|
115
|
-
const chosenAuthUrlIndex = validRepositoryUrls.findIndex((url) => url !== null);
|
|
116
|
-
if (chosenAuthUrlIndex > -1) {
|
|
117
|
-
debug(`Using "${envVars[chosenAuthUrlIndex]}" to authenticate`);
|
|
118
|
-
return validRepositoryUrls[chosenAuthUrlIndex];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Log completion of this process
|
|
124
|
-
debug("SSH key auth successful.");
|
|
125
|
-
return repositoryUrl;
|
|
126
|
-
};
|
package/lib/get-last-release.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { isUndefined } from "lodash-es";
|
|
2
|
-
import semver from "semver";
|
|
3
|
-
import { isSameChannel, makeTag } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Last release.
|
|
7
|
-
*
|
|
8
|
-
* @typedef {Object} LastRelease
|
|
9
|
-
* @property {string} version The version number of the last release.
|
|
10
|
-
* @property {string} gitHead The Git reference used to make the last release.
|
|
11
|
-
* @property {string} gitTag The git tag associated with the last release.
|
|
12
|
-
* @property {string} channel The channel on which of the last release was published.
|
|
13
|
-
* @property {string} name The name of the last release.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Determine the Git tag and version of the last tagged release.
|
|
18
|
-
*
|
|
19
|
-
* - Filter out the branch tags that are not valid semantic version
|
|
20
|
-
* - Sort the versions
|
|
21
|
-
* - Retrieve the highest version
|
|
22
|
-
*
|
|
23
|
-
* @param {Object} context semantic-release context.
|
|
24
|
-
* @param {Object} params Function parameters.
|
|
25
|
-
* @param {Object} params.before Find only releases with version number lower than this version.
|
|
26
|
-
*
|
|
27
|
-
* @return {LastRelease} The last tagged release or empty object if none is found.
|
|
28
|
-
*/
|
|
29
|
-
export default ({ branch, options: { tagFormat } }, { before } = {}) => {
|
|
30
|
-
const [{ version, gitTag, channels } = {}] = branch.tags
|
|
31
|
-
.filter(
|
|
32
|
-
(tag) =>
|
|
33
|
-
((branch.type === "prerelease" &&
|
|
34
|
-
tag.channels.some((channel) => isSameChannel(branch.channel, channel)) &&
|
|
35
|
-
semver
|
|
36
|
-
.parse(tag.version)
|
|
37
|
-
.prerelease.includes(branch.prerelease === true ? branch.name : branch.prerelease)) ||
|
|
38
|
-
!semver.prerelease(tag.version)) &&
|
|
39
|
-
(isUndefined(before) || semver.lt(tag.version, before))
|
|
40
|
-
)
|
|
41
|
-
.sort((a, b) => semver.rcompare(a.version, b.version));
|
|
42
|
-
|
|
43
|
-
if (gitTag) {
|
|
44
|
-
return { version, gitTag, channels, gitHead: gitTag, name: makeTag(tagFormat, version) };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return {};
|
|
48
|
-
};
|
package/lib/get-logger.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import signale from "signale";
|
|
2
|
-
import figures from "figures";
|
|
3
|
-
|
|
4
|
-
const { Signale } = signale;
|
|
5
|
-
|
|
6
|
-
export default ({ stdout, stderr }) =>
|
|
7
|
-
new Signale({
|
|
8
|
-
config: { displayTimestamp: true, underlineMessage: false, displayLabel: false },
|
|
9
|
-
disabled: false,
|
|
10
|
-
interactive: false,
|
|
11
|
-
scope: "semantic-release",
|
|
12
|
-
stream: [stdout],
|
|
13
|
-
types: {
|
|
14
|
-
error: { badge: figures.cross, color: "red", label: "", stream: [stderr] },
|
|
15
|
-
log: { badge: figures.info, color: "magenta", label: "", stream: [stdout] },
|
|
16
|
-
success: { badge: figures.tick, color: "green", label: "", stream: [stdout] },
|
|
17
|
-
warn: { badge: figures.warning, color: "yellow", label: "", stream: [stderr] },
|
|
18
|
-
},
|
|
19
|
-
});
|
package/lib/get-next-version.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import semver from "semver";
|
|
2
|
-
import { FIRST_RELEASE, FIRSTPRERELEASE } from "./definitions/constants.js";
|
|
3
|
-
import { getLatestVersion, highest, isSameChannel, tagsToVersions } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
export default ({ branch, nextRelease: { type, channel }, lastRelease, logger }) => {
|
|
6
|
-
let version;
|
|
7
|
-
if (lastRelease.version) {
|
|
8
|
-
const { major, minor, patch } = semver.parse(lastRelease.version);
|
|
9
|
-
|
|
10
|
-
if (branch.type === "prerelease") {
|
|
11
|
-
if (
|
|
12
|
-
semver.prerelease(lastRelease.version) &&
|
|
13
|
-
lastRelease.channels.some((lastReleaseChannel) => isSameChannel(lastReleaseChannel, channel))
|
|
14
|
-
) {
|
|
15
|
-
version = highest(
|
|
16
|
-
semver.inc(lastRelease.version, "prerelease"),
|
|
17
|
-
`${semver.inc(getLatestVersion(tagsToVersions(branch.tags), { withPrerelease: true }), type)}-${
|
|
18
|
-
branch.prerelease
|
|
19
|
-
}.${FIRSTPRERELEASE}`
|
|
20
|
-
);
|
|
21
|
-
} else {
|
|
22
|
-
version = `${semver.inc(`${major}.${minor}.${patch}`, type)}-${branch.prerelease}.${FIRSTPRERELEASE}`;
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
version = semver.inc(lastRelease.version, type);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
logger.log("The next release version is %s", version);
|
|
29
|
-
} else {
|
|
30
|
-
version = branch.type === "prerelease" ? `${FIRST_RELEASE}-${branch.prerelease}.${FIRSTPRERELEASE}` : FIRST_RELEASE;
|
|
31
|
-
logger.log(`There is no previous release, the next release version is ${version}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return version;
|
|
35
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { intersection, uniqBy } from "lodash-es";
|
|
2
|
-
import semver from "semver";
|
|
3
|
-
import getLastRelease from "./get-last-release.js";
|
|
4
|
-
import { getLowerBound, getVersionDiff, makeTag } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Find releases that have been merged from a higher branch but not added on the channel of the current branch.
|
|
8
|
-
*
|
|
9
|
-
* @param {Object} context semantic-release context.
|
|
10
|
-
*
|
|
11
|
-
* @return {Array<Object>} Last release and next release to be added on the channel of the current branch.
|
|
12
|
-
*/
|
|
13
|
-
export default (context) => {
|
|
14
|
-
const {
|
|
15
|
-
branch,
|
|
16
|
-
branches,
|
|
17
|
-
options: { tagFormat },
|
|
18
|
-
} = context;
|
|
19
|
-
|
|
20
|
-
const higherChannels = branches
|
|
21
|
-
// Consider only releases of higher branches
|
|
22
|
-
.slice(branches.findIndex(({ name }) => name === branch.name) + 1)
|
|
23
|
-
// Exclude prerelease branches
|
|
24
|
-
.filter(({ type }) => type !== "prerelease")
|
|
25
|
-
.map(({ channel }) => channel || null);
|
|
26
|
-
|
|
27
|
-
const versiontoAdd = uniqBy(
|
|
28
|
-
branch.tags.filter(
|
|
29
|
-
({ channels, version }) =>
|
|
30
|
-
!channels.includes(branch.channel || null) &&
|
|
31
|
-
intersection(channels, higherChannels).length > 0 &&
|
|
32
|
-
(branch.type !== "maintenance" || semver.gte(version, getLowerBound(branch.mergeRange)))
|
|
33
|
-
),
|
|
34
|
-
"version"
|
|
35
|
-
).sort((a, b) => semver.compare(b.version, a.version))[0];
|
|
36
|
-
|
|
37
|
-
if (versiontoAdd) {
|
|
38
|
-
const { version, gitTag, channels } = versiontoAdd;
|
|
39
|
-
const lastRelease = getLastRelease(context, { before: version });
|
|
40
|
-
if (semver.gt(getLastRelease(context).version, version)) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const type = lastRelease.version ? getVersionDiff(lastRelease.version, version) : "major";
|
|
45
|
-
const name = makeTag(tagFormat, version);
|
|
46
|
-
return {
|
|
47
|
-
lastRelease,
|
|
48
|
-
currentRelease: { type, version, channels, gitTag, name, gitHead: gitTag },
|
|
49
|
-
nextRelease: {
|
|
50
|
-
type,
|
|
51
|
-
version,
|
|
52
|
-
channel: branch.channel || null,
|
|
53
|
-
gitTag: makeTag(tagFormat, version),
|
|
54
|
-
name,
|
|
55
|
-
gitHead: gitTag,
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
};
|
package/lib/git.js
DELETED
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
import gitLogParser from "git-log-parser";
|
|
2
|
-
import getStream from "get-stream";
|
|
3
|
-
import { execa } from "execa";
|
|
4
|
-
import debugGit from "debug";
|
|
5
|
-
import { merge } from "lodash-es";
|
|
6
|
-
import { GIT_NOTE_REF } from "./definitions/constants.js";
|
|
7
|
-
import { extractGitLogTags } from "./utils.js";
|
|
8
|
-
|
|
9
|
-
const debug = debugGit("semantic-release:git");
|
|
10
|
-
|
|
11
|
-
Object.assign(gitLogParser.fields, { hash: "H", message: "B", gitTags: "d", committerDate: { key: "ci", type: Date } });
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Get the commit sha for a given tag.
|
|
15
|
-
*
|
|
16
|
-
* @param {String} tagName Tag name for which to retrieve the commit sha.
|
|
17
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
18
|
-
*
|
|
19
|
-
* @return {String} The commit sha of the tag in parameter or `null`.
|
|
20
|
-
*/
|
|
21
|
-
export async function getTagHead(tagName, execaOptions) {
|
|
22
|
-
return (await execa("git", ["rev-list", "-1", tagName], execaOptions)).stdout;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Get all the tags for a given branch.
|
|
27
|
-
*
|
|
28
|
-
* @param {String} branch The branch for which to retrieve the tags.
|
|
29
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
30
|
-
*
|
|
31
|
-
* @return {Array<String>} List of git tags.
|
|
32
|
-
* @throws {Error} If the `git` command fails.
|
|
33
|
-
*/
|
|
34
|
-
export async function getTags(branch, execaOptions) {
|
|
35
|
-
return (await execa("git", ["tag", "--merged", branch], execaOptions)).stdout
|
|
36
|
-
.split("\n")
|
|
37
|
-
.map((tag) => tag.trim())
|
|
38
|
-
.filter(Boolean);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Retrieve a range of commits.
|
|
43
|
-
*
|
|
44
|
-
* @param {String} from to includes all commits made after this sha (does not include this sha).
|
|
45
|
-
* @param {String} to to includes all commits made before this sha (also include this sha).
|
|
46
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
47
|
-
* @return {Promise<Array<Object>>} The list of commits between `from` and `to`.
|
|
48
|
-
*/
|
|
49
|
-
export async function getCommits(from, to, execaOptions) {
|
|
50
|
-
return (
|
|
51
|
-
await getStream.array(
|
|
52
|
-
gitLogParser.parse(
|
|
53
|
-
{ _: `${from ? from + ".." : ""}${to}` },
|
|
54
|
-
{ cwd: execaOptions.cwd, env: { ...process.env, ...execaOptions.env } }
|
|
55
|
-
)
|
|
56
|
-
)
|
|
57
|
-
).map(({ message, gitTags, ...commit }) => ({ ...commit, message: message.trim(), gitTags: gitTags.trim() }));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Get all the repository branches.
|
|
62
|
-
*
|
|
63
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
64
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
65
|
-
*
|
|
66
|
-
* @return {Array<String>} List of git branches.
|
|
67
|
-
* @throws {Error} If the `git` command fails.
|
|
68
|
-
*/
|
|
69
|
-
export async function getBranches(repositoryUrl, execaOptions) {
|
|
70
|
-
return (await execa("git", ["ls-remote", "--heads", repositoryUrl], execaOptions)).stdout
|
|
71
|
-
.split("\n")
|
|
72
|
-
.filter(Boolean)
|
|
73
|
-
.map((branch) => branch.match(/^.+refs\/heads\/(?<branch>.+)$/)[1]);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Verify if the `ref` exits
|
|
78
|
-
*
|
|
79
|
-
* @param {String} ref The reference to verify.
|
|
80
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
81
|
-
*
|
|
82
|
-
* @return {Boolean} `true` if the reference exists, falsy otherwise.
|
|
83
|
-
*/
|
|
84
|
-
export async function isRefExists(ref, execaOptions) {
|
|
85
|
-
try {
|
|
86
|
-
return (await execa("git", ["rev-parse", "--verify", ref], execaOptions)).exitCode === 0;
|
|
87
|
-
} catch (error) {
|
|
88
|
-
debug(error);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Fetch all the tags from a branch. Unshallow if necessary.
|
|
94
|
-
* This will update the local branch from the latest on the remote if:
|
|
95
|
-
* - The branch is not the one that triggered the CI
|
|
96
|
-
* - The CI created a detached head
|
|
97
|
-
*
|
|
98
|
-
* Otherwise it just calls `git fetch` without specifying the `refspec` option to avoid overwritting the head commit set by the CI.
|
|
99
|
-
*
|
|
100
|
-
* The goal is to retrieve the informations on all the release branches without "disturbing" the CI, leaving the trigger branch or the detached head intact.
|
|
101
|
-
*
|
|
102
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
103
|
-
* @param {String} branch The repository branch to fetch.
|
|
104
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
105
|
-
*/
|
|
106
|
-
export async function fetch(repositoryUrl, branch, ciBranch, execaOptions) {
|
|
107
|
-
const isDetachedHead =
|
|
108
|
-
(await execa("git", ["rev-parse", "--abbrev-ref", "HEAD"], { ...execaOptions, reject: false })).stdout === "HEAD";
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
await execa(
|
|
112
|
-
"git",
|
|
113
|
-
[
|
|
114
|
-
"fetch",
|
|
115
|
-
"--unshallow",
|
|
116
|
-
"--tags",
|
|
117
|
-
...(branch === ciBranch && !isDetachedHead
|
|
118
|
-
? [repositoryUrl]
|
|
119
|
-
: ["--update-head-ok", repositoryUrl, `+refs/heads/${branch}:refs/heads/${branch}`]),
|
|
120
|
-
],
|
|
121
|
-
execaOptions
|
|
122
|
-
);
|
|
123
|
-
} catch {
|
|
124
|
-
await execa(
|
|
125
|
-
"git",
|
|
126
|
-
[
|
|
127
|
-
"fetch",
|
|
128
|
-
"--tags",
|
|
129
|
-
...(branch === ciBranch && !isDetachedHead
|
|
130
|
-
? [repositoryUrl]
|
|
131
|
-
: ["--update-head-ok", repositoryUrl, `+refs/heads/${branch}:refs/heads/${branch}`]),
|
|
132
|
-
],
|
|
133
|
-
execaOptions
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Unshallow the git repository if necessary and fetch all the notes.
|
|
140
|
-
*
|
|
141
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
142
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
143
|
-
*/
|
|
144
|
-
export async function fetchNotes(repositoryUrl, execaOptions) {
|
|
145
|
-
try {
|
|
146
|
-
await execa("git", ["fetch", "--unshallow", repositoryUrl, `+refs/notes/*:refs/notes/*`], execaOptions);
|
|
147
|
-
} catch {
|
|
148
|
-
await execa("git", ["fetch", repositoryUrl, `+refs/notes/*:refs/notes/*`], {
|
|
149
|
-
...execaOptions,
|
|
150
|
-
reject: false,
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Get the HEAD sha.
|
|
157
|
-
*
|
|
158
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
159
|
-
*
|
|
160
|
-
* @return {String} the sha of the HEAD commit.
|
|
161
|
-
*/
|
|
162
|
-
export async function getGitHead(execaOptions) {
|
|
163
|
-
return (await execa("git", ["rev-parse", "HEAD"], execaOptions)).stdout;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Get the repository remote URL.
|
|
168
|
-
*
|
|
169
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
170
|
-
*
|
|
171
|
-
* @return {string} The value of the remote git URL.
|
|
172
|
-
*/
|
|
173
|
-
export async function repoUrl(execaOptions) {
|
|
174
|
-
try {
|
|
175
|
-
return (await execa("git", ["config", "--get", "remote.origin.url"], execaOptions)).stdout;
|
|
176
|
-
} catch (error) {
|
|
177
|
-
debug(error);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Test if the current working directory is a Git repository.
|
|
183
|
-
*
|
|
184
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
185
|
-
*
|
|
186
|
-
* @return {Boolean} `true` if the current working directory is in a git repository, falsy otherwise.
|
|
187
|
-
*/
|
|
188
|
-
export async function isGitRepo(execaOptions) {
|
|
189
|
-
try {
|
|
190
|
-
return (await execa("git", ["rev-parse", "--git-dir"], execaOptions)).exitCode === 0;
|
|
191
|
-
} catch (error) {
|
|
192
|
-
debug(error);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Verify the write access authorization to remote repository with push dry-run.
|
|
198
|
-
*
|
|
199
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
200
|
-
* @param {String} branch The repository branch for which to verify write access.
|
|
201
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
202
|
-
*
|
|
203
|
-
* @throws {Error} if not authorized to push.
|
|
204
|
-
*/
|
|
205
|
-
export async function verifyAuth(repositoryUrl, branch, execaOptions) {
|
|
206
|
-
try {
|
|
207
|
-
await execa("git", ["push", "--dry-run", "--no-verify", repositoryUrl, `HEAD:${branch}`], execaOptions);
|
|
208
|
-
} catch (error) {
|
|
209
|
-
debug(error);
|
|
210
|
-
throw error;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Tag the commit head on the local repository.
|
|
216
|
-
*
|
|
217
|
-
* @param {String} tagName The name of the tag.
|
|
218
|
-
* @param {String} ref The Git reference to tag.
|
|
219
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
220
|
-
*
|
|
221
|
-
* @throws {Error} if the tag creation failed.
|
|
222
|
-
*/
|
|
223
|
-
export async function tag(tagName, ref, execaOptions) {
|
|
224
|
-
await execa("git", ["tag", tagName, ref], execaOptions);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Push to the remote repository.
|
|
229
|
-
*
|
|
230
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
231
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
232
|
-
*
|
|
233
|
-
* @throws {Error} if the push failed.
|
|
234
|
-
*/
|
|
235
|
-
export async function push(repositoryUrl, execaOptions) {
|
|
236
|
-
await execa("git", ["push", "--tags", repositoryUrl], execaOptions);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Push notes to the remote repository.
|
|
241
|
-
*
|
|
242
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
243
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
244
|
-
*
|
|
245
|
-
* @throws {Error} if the push failed.
|
|
246
|
-
*/
|
|
247
|
-
export async function pushNotes(repositoryUrl, ref, execaOptions) {
|
|
248
|
-
await execa("git", ["push", repositoryUrl, `refs/notes/${GIT_NOTE_REF}-${ref}`], execaOptions);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Verify a tag name is a valid Git reference.
|
|
253
|
-
*
|
|
254
|
-
* @param {String} tagName the tag name to verify.
|
|
255
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
256
|
-
*
|
|
257
|
-
* @return {Boolean} `true` if valid, falsy otherwise.
|
|
258
|
-
*/
|
|
259
|
-
export async function verifyTagName(tagName, execaOptions) {
|
|
260
|
-
try {
|
|
261
|
-
return (await execa("git", ["check-ref-format", `refs/tags/${tagName}`], execaOptions)).exitCode === 0;
|
|
262
|
-
} catch (error) {
|
|
263
|
-
debug(error);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Verify a branch name is a valid Git reference.
|
|
269
|
-
*
|
|
270
|
-
* @param {String} branch the branch name to verify.
|
|
271
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
272
|
-
*
|
|
273
|
-
* @return {Boolean} `true` if valid, falsy otherwise.
|
|
274
|
-
*/
|
|
275
|
-
export async function verifyBranchName(branch, execaOptions) {
|
|
276
|
-
try {
|
|
277
|
-
return (await execa("git", ["check-ref-format", `refs/heads/${branch}`], execaOptions)).exitCode === 0;
|
|
278
|
-
} catch (error) {
|
|
279
|
-
debug(error);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Verify the local branch is up to date with the remote one.
|
|
285
|
-
*
|
|
286
|
-
* @param {String} repositoryUrl The remote repository URL.
|
|
287
|
-
* @param {String} branch The repository branch for which to verify status.
|
|
288
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
289
|
-
*
|
|
290
|
-
* @return {Boolean} `true` is the HEAD of the current local branch is the same as the HEAD of the remote branch, falsy otherwise.
|
|
291
|
-
*/
|
|
292
|
-
export async function isBranchUpToDate(repositoryUrl, branch, execaOptions) {
|
|
293
|
-
return (
|
|
294
|
-
(await getGitHead(execaOptions)) ===
|
|
295
|
-
(await execa("git", ["ls-remote", "--heads", repositoryUrl, branch], execaOptions)).stdout.match(/^(?<ref>\w+)?/)[1]
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Retrieves a map of Git tags to their associated notes from the repository.
|
|
301
|
-
*
|
|
302
|
-
* Executes a `git log` command to list tags and their notes, then parses the output into a Map
|
|
303
|
-
* where each key is a tag name (e.g., "v24.2.3") and the value is the parsed JSON note object.
|
|
304
|
-
*
|
|
305
|
-
* @async
|
|
306
|
-
* @param {import('execa').Options} execaOptions - Options to pass to `execa`
|
|
307
|
-
* @returns {Promise<Map<string, Object>>} A promise that resolves to a Map of tag names to their notes.
|
|
308
|
-
*/
|
|
309
|
-
export async function getTagsNotes(execaOptions) {
|
|
310
|
-
/**
|
|
311
|
-
* git log --tags="*" --decorate-refs="refs/tags/*" --no-walk --format="%d%x09%N" --notes="refs/notes/semantic-release*"
|
|
312
|
-
*
|
|
313
|
-
* (tag: v1.2.3)
|
|
314
|
-
* (tag: v2.0.0) {"channels":[null]}
|
|
315
|
-
* (tag: v3.0.0, tag: 5833/merge) {"channels":[null]}
|
|
316
|
-
* ...
|
|
317
|
-
*/
|
|
318
|
-
const { stdout } = await execa(
|
|
319
|
-
"git",
|
|
320
|
-
[
|
|
321
|
-
"log",
|
|
322
|
-
"--tags=*",
|
|
323
|
-
"--decorate-refs=refs/tags/*", // This filters the refs shown in the %d format specifier to only include tags matching refs/tags/*.
|
|
324
|
-
"--no-walk", // This ensures that only the commits directly pointed to by the tags are shown, not their historical parents.
|
|
325
|
-
"--format=%d%x09%N", // <refName><tab><notes> eg. (tag: v24.2.3) {"channels":[null]}
|
|
326
|
-
`--notes=refs/notes/${GIT_NOTE_REF}*`, // handles both patterns for notes - `semantic-release` (old) and `semantic-release-<version>` (current)
|
|
327
|
-
],
|
|
328
|
-
execaOptions
|
|
329
|
-
);
|
|
330
|
-
|
|
331
|
-
// drop empty lines
|
|
332
|
-
const lines = stdout.split("\n").filter((line) => line.trim() !== "");
|
|
333
|
-
|
|
334
|
-
// parse and create a map of tags to notes
|
|
335
|
-
const tagNotesMap = new Map();
|
|
336
|
-
|
|
337
|
-
for (const line of lines) {
|
|
338
|
-
const [tagPart, notePart] = line.trim().split("\t"); // tab separator is defined in the git command above (%x09)
|
|
339
|
-
const tags = extractGitLogTags(tagPart);
|
|
340
|
-
if (tags.length === 0) {
|
|
341
|
-
debug(`Cannot parse tags from line: ${line}`);
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
try {
|
|
346
|
-
const parsed = JSON.parse(notePart);
|
|
347
|
-
tags.forEach((tag) => {
|
|
348
|
-
tagNotesMap.set(tag, parsed);
|
|
349
|
-
});
|
|
350
|
-
} catch (error) {
|
|
351
|
-
debug(error);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
return tagNotesMap;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Add JSON note to a given reference.
|
|
360
|
-
*
|
|
361
|
-
* @param {Object} note The object to save in the reference note.
|
|
362
|
-
* @param {String} ref The Git reference to add the note to.
|
|
363
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
364
|
-
*/
|
|
365
|
-
export async function addNote(note, ref, execaOptions) {
|
|
366
|
-
await execa(
|
|
367
|
-
"git",
|
|
368
|
-
["notes", "--ref", `${GIT_NOTE_REF}-${ref}`, "add", "-f", "-m", JSON.stringify(note), ref],
|
|
369
|
-
execaOptions
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Get the reference of a tag
|
|
375
|
-
*
|
|
376
|
-
* @param {String} tag The tag name to get the reference of.
|
|
377
|
-
* @param {Object} [execaOpts] Options to pass to `execa`.
|
|
378
|
-
**/
|
|
379
|
-
export async function getTagRef(tag, execaOptions) {
|
|
380
|
-
return (await execa("git", ["show-ref", tag, "--hash"], execaOptions)).stdout;
|
|
381
|
-
}
|