Skip to content

Commit

Permalink
conditional update (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
mangs authored Mar 29, 2024
1 parent 26b9fee commit 3986539
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ etc...

## Action Inputs

| Name | Required | Default Value | Descripition |
| ------------------ | -------- | ------------------ | ------------------------------------------------------------------------------------------------ |
| `changelog_path` | N | `"./CHANGELOG.md"` | Path to the target changelog file |
| `github_token` | Y | N/A | GitHub authentication token used to authenticate on behalf of GitHub Actions |
| `packagejson_path` | N | `"./package.json"` | Path to the target `package.json` file |
| `tag_override` | N | `undefined` | String to enforce an exact tag version; overrides default behavior |
| `tag_prefix` | N | `"v"` | Prefix to create a tag by combining this and the target version number; this is default behavior |
| Name | Required | Default Value | Descripition |
| ------------------------------ | -------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `changelog_path` | N | `"./CHANGELOG.md"` | Path to the target changelog file |
| `github_token` | Y | N/A | GitHub authentication token used to authenticate on behalf of GitHub Actions |
| `packagejson_path` | N | `"./package.json"` | Path to the target `package.json` file |
| `should_auto_update_major_tag` | N | `true` | Auto-update the major tag version of your repository when a new release gets published or create it if it doesn't exist. For example, when publishing version 2.3.4, major tag v2 will get updated to HEAD. |
| `tag_override` | N | `undefined` | String to enforce an exact tag version; overrides default behavior |
| `tag_prefix` | N | `"v"` | Prefix to create a tag by combining this and the target version number; this is default behavior |
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ inputs:
default: ./package.json
description: Path to the target package.json file
required: false
should_auto_update_major_tag:
default: true
description: Auto-update the major tag version of your repository when a new release gets published or create it if it doesn't exist. For example, when publishing version 2.3.4, major tag v2 will get updated to HEAD.
required: false
tag_override:
description: String to enforce an exact tag version; overrides default behavior
required: false
Expand Down
40 changes: 18 additions & 22 deletions scripts/publishReleaseNotes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import major from '../vendor/semver-v7.6.0-functions-major.vendor.mjs';

// Local Variables
const changelogPath = process.env.INPUT_CHANGELOG_PATH ?? './CHANGELOG.md';
const githubSha = process.env.GITHUB_SHA;
const githubToken = process.env.INPUT_GITHUB_TOKEN;
const packageJsonPath = process.env.INPUT_PACKAGEJSON_PATH ?? './package.json';
const shouldUpdateMajorTag = process.env.INPUT_SHOULD_AUTO_UPDATE_MAJOR_TAG === 'true';
const tagOverride = process.env.INPUT_TAG_OVERRIDE;
const tagPrefix = process.env.INPUT_TAG_PREFIX ?? 'v';
const urlRegex = /\[(?<linkText>[^\]]*)\]\([^)]+\)/g; // eslint-disable-line unicorn/better-regex -- stuck in a lint error loop, this is as good as it can get
Expand Down Expand Up @@ -56,11 +56,9 @@ for (const token of markdownTokens) {
}
}
if (!isCapturing) {
console.error(
throw new Error(
`No match for version "${targetVersion}" found in changelog file "${changelogPath}"`,
);
// eslint-disable-next-line n/no-process-exit -- stack trace not useful here
process.exit(1);
}

// Store the tokens' combined markdown
Expand All @@ -77,27 +75,24 @@ const commitHash = process.env.GITHUB_SHA;
// const isDraft = false;
// const isPrerelease = false;

// Authenticate on Github, then create a release note
if (process.env.CI !== 'true') {
// eslint-disable-next-line n/no-process-exit -- don't make any requests to GitHub when doing local testing
process.exit();
}

// Create a release
// Create a GitHub release
try {
const releaseData = JSON.stringify({
body: combinedMarkdown,
// draft: isDraft,
name: versionMatchHeadingText,
owner: repoOwner,
// prerelease: isPrerelease,
repo: repoName,
tag_name: tagName,
target_commitish: commitHash,
});

const response = await fetch(`https://api.github.com/repos/${repoOwner}/${repoName}/releases`, {
body: releaseData,
body: JSON.stringify({
body: combinedMarkdown,
// draft: isDraft,
name: versionMatchHeadingText,
owner: repoOwner,
// prerelease: isPrerelease,
repo: repoName,
tag_name: tagName,
target_commitish: commitHash,
}),
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${githubToken}`,
Expand All @@ -117,17 +112,18 @@ try {
}

// Update the latest major tag version to the latest commit SHA
if (!shouldUpdateMajorTag) {
// eslint-disable-next-line n/no-process-exit -- don't continue because remaining functionality has been disabled
process.exit(0);
}
try {
const majorTag = tagOverride ? tagOverride.split('.')[0] : `${tagPrefix}${major(tagName)}`;
const ref = `refs/tags/${majorTag}`; // eslint-disable-line unicorn/prevent-abbreviations -- required name

console.log('REF', ref);

const tagData = {
owner: repoOwner,
ref,
repo: repoName,
sha: githubSha,
sha: process.env.GITHUB_SHA,
};

const updateResponse = await fetch(
Expand Down

0 comments on commit 3986539

Please sign in to comment.