Add rc tag

pull/5/head
Varun Sharma 4 years ago
parent 9b5fe1fd8f
commit 5cdf6d60b5

@ -4,8 +4,10 @@ inputs:
source-tag: source-tag:
description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3' description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3'
required: true required: true
slack-webhook: rc:
description: 'Slack Webhook URL to post a message' description: 'Update rc tag'
required: false
default: ''
token: token:
description: 'Token to get an authenticated Octokit' description: 'Token to get an authenticated Octokit'
default: ${{ github.token }} default: ${{ github.token }}

51
dist/index.js vendored

@ -26,15 +26,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.postMessageToSlack = exports.updateTag = exports.validateIfReleaseIsPublished = void 0; exports.updateTag = exports.validateIfReleaseIsPublished = void 0;
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438); const github_1 = __nccwpck_require__(5438);
const http_client_1 = __nccwpck_require__(9925);
async function findTag(tag, octokitClient) { async function findTag(tag, octokitClient) {
try { try {
const { data: foundTag } = await octokitClient.git.getRef({ const { data: foundTag } = await octokitClient.git.getRef({
...github_1.context.repo, ...github_1.context.repo,
ref: `tags/${tag}` ref: `tags/${tag}`,
}); });
return foundTag; return foundTag;
} }
@ -84,7 +83,7 @@ async function updateTag(sourceTag, targetTag, octokitClient) {
...github_1.context.repo, ...github_1.context.repo,
ref: refName, ref: refName,
sha: sourceTagSHA, sha: sourceTagSHA,
force: true force: true,
}); });
} }
else { else {
@ -92,17 +91,11 @@ async function updateTag(sourceTag, targetTag, octokitClient) {
await octokitClient.git.createRef({ await octokitClient.git.createRef({
...github_1.context.repo, ...github_1.context.repo,
ref: `refs/${refName}`, ref: `refs/${refName}`,
sha: sourceTagSHA sha: sourceTagSHA,
}); });
} }
} }
exports.updateTag = updateTag; exports.updateTag = updateTag;
async function postMessageToSlack(slackWebhook, message) {
const jsonData = { text: message };
const http = new http_client_1.HttpClient();
await http.postJson(slackWebhook, jsonData);
}
exports.postMessageToSlack = postMessageToSlack;
/***/ }), /***/ }),
@ -134,34 +127,30 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438)); const github = __importStar(__nccwpck_require__(5438));
const github_1 = __nccwpck_require__(5438);
const api_utils_1 = __nccwpck_require__(2430); const api_utils_1 = __nccwpck_require__(2430);
const version_utils_1 = __nccwpck_require__(1534); const version_utils_1 = __nccwpck_require__(1534);
async function run() { async function run() {
try { try {
const token = core.getInput('token'); const token = core.getInput("token");
const octokitClient = github.getOctokit(token); const octokitClient = github.getOctokit(token);
const sourceTagName = core.getInput('source-tag'); const sourceTagName = core.getInput("source-tag");
version_utils_1.validateSemverVersionFromTag(sourceTagName); const rc = core.getInput("rc");
await api_utils_1.validateIfReleaseIsPublished(sourceTagName, octokitClient); if (rc !== "") {
const majorTag = version_utils_1.getMajorTagFromFullTag(sourceTagName); await api_utils_1.updateTag(sourceTagName, "rc", octokitClient);
await api_utils_1.updateTag(sourceTagName, majorTag, octokitClient); core.setOutput("major-tag", "rc");
core.setOutput('major-tag', majorTag); core.info(`'rc' tag now points to the '${sourceTagName}' tag`);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`); }
const slackMessage = `The ${majorTag} tag has been successfully updated for the ${github_1.context.repo.repo} action to include changes from the ${sourceTagName}`; else {
await reportStatusToSlack(slackMessage); version_utils_1.validateSemverVersionFromTag(sourceTagName);
await api_utils_1.validateIfReleaseIsPublished(sourceTagName, octokitClient);
const majorTag = version_utils_1.getMajorTagFromFullTag(sourceTagName);
await api_utils_1.updateTag(sourceTagName, majorTag, octokitClient);
core.setOutput("major-tag", majorTag);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`);
}
} }
catch (error) { catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
const slackMessage = `Failed to update a major tag for the ${github_1.context.repo.repo} action`;
await reportStatusToSlack(slackMessage);
}
}
;
async function reportStatusToSlack(message) {
const slackWebhook = core.getInput('slack-webhook');
if (slackWebhook) {
await api_utils_1.postMessageToSlack(slackWebhook, message);
} }
} }
run(); run();

5721
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{ {
"name": "publish-action", "name": "publish-action",
"version": "0.1.0", "version": "0.2.0",
"description": "Update the major version tag (v1, v2, etc.) to point to the specified tag", "description": "Update the major version tag (v1, v2, etc.) to point to the specified tag",
"main": "lib/main.js", "main": "lib/main.js",
"scripts": { "scripts": {
@ -23,8 +23,8 @@
"homepage": "https://github.com/actions/publish-action#readme", "homepage": "https://github.com/actions/publish-action#readme",
"dependencies": { "dependencies": {
"@actions/core": "^1.2.7", "@actions/core": "^1.2.7",
"@actions/http-client": "^1.0.11",
"@actions/github": "^4.0.0", "@actions/github": "^4.0.0",
"@actions/http-client": "^1.0.11",
"semver": "^7.3.5" "semver": "^7.3.5"
}, },
"devDependencies": { "devDependencies": {

@ -1,114 +1,105 @@
import * as core from '@actions/core'; import * as core from "@actions/core";
import { context } from '@actions/github'; import { context } from "@actions/github";
import { GitHub } from '@actions/github/lib/utils'; import { GitHub } from "@actions/github/lib/utils";
import { HttpClient } from '@actions/http-client';
interface GitRef { interface GitRef {
ref: string; ref: string;
node_id: string; node_id: string;
url: string;
object: {
type: string;
sha: string;
url: string; url: string;
object: { };
type: string;
sha: string;
url: string;
};
} }
async function findTag( async function findTag(
tag: string, tag: string,
octokitClient: InstanceType<typeof GitHub> octokitClient: InstanceType<typeof GitHub>
): Promise<GitRef | null> { ): Promise<GitRef | null> {
try { try {
const { data: foundTag } = await octokitClient.git.getRef({ const { data: foundTag } = await octokitClient.git.getRef({
...context.repo, ...context.repo,
ref: `tags/${tag}` ref: `tags/${tag}`,
}); });
return foundTag; return foundTag;
} catch (err) { } catch (err) {
if (err.status === 404) { if (err.status === 404) {
return null; return null;
} else { } else {
throw new Error( throw new Error(
`Retrieving refs failed with the following error: ${err}` `Retrieving refs failed with the following error: ${err}`
); );
}
} }
}
} }
async function getTagSHA( async function getTagSHA(
tag: string, tag: string,
octokitClient: InstanceType<typeof GitHub> octokitClient: InstanceType<typeof GitHub>
): Promise<string> { ): Promise<string> {
const foundTag = await findTag(tag, octokitClient); const foundTag = await findTag(tag, octokitClient);
if (!foundTag) { if (!foundTag) {
throw new Error( throw new Error(`The '${tag}' tag does not exist in the remote repository`);
`The '${tag}' tag does not exist in the remote repository` }
);
}
return foundTag.object.sha; return foundTag.object.sha;
} }
export async function validateIfReleaseIsPublished( export async function validateIfReleaseIsPublished(
tag: string, tag: string,
octokitClient: InstanceType<typeof GitHub> octokitClient: InstanceType<typeof GitHub>
): Promise<void> { ): Promise<void> {
try { try {
const { data: foundRelease } = await octokitClient.repos.getReleaseByTag({ const { data: foundRelease } = await octokitClient.repos.getReleaseByTag({
...context.repo, ...context.repo,
tag, tag,
}); });
if (foundRelease.prerelease) { if (foundRelease.prerelease) {
throw new Error( throw new Error(
`The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported` `The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported`
); );
} }
} catch (err) { } catch (err) {
if (err.status === 404) { if (err.status === 404) {
throw new Error( throw new Error(`No GitHub release found for the ${tag} tag`);
`No GitHub release found for the ${tag} tag` } else {
); throw new Error(
} else { `Retrieving releases failed with the following error: ${err}`
throw new Error( );
`Retrieving releases failed with the following error: ${err}`
);
}
} }
}
} }
export async function updateTag( export async function updateTag(
sourceTag: string, sourceTag: string,
targetTag: string, targetTag: string,
octokitClient: InstanceType<typeof GitHub> octokitClient: InstanceType<typeof GitHub>
): Promise<void> { ): Promise<void> {
const sourceTagSHA = await getTagSHA(sourceTag, octokitClient); const sourceTagSHA = await getTagSHA(sourceTag, octokitClient);
const foundTargetTag = await findTag(targetTag, octokitClient); const foundTargetTag = await findTag(targetTag, octokitClient);
const refName = `tags/${targetTag}`; const refName = `tags/${targetTag}`;
if (foundTargetTag) { if (foundTargetTag) {
core.info(`Updating the '${targetTag}' tag to point to the '${sourceTag}' tag`); core.info(
`Updating the '${targetTag}' tag to point to the '${sourceTag}' tag`
);
await octokitClient.git.updateRef({ await octokitClient.git.updateRef({
...context.repo, ...context.repo,
ref: refName, ref: refName,
sha: sourceTagSHA, sha: sourceTagSHA,
force: true force: true,
}); });
} else { } else {
core.info(`Creating the '${targetTag}' tag from the '${sourceTag}' tag`); core.info(`Creating the '${targetTag}' tag from the '${sourceTag}' tag`);
await octokitClient.git.createRef({ await octokitClient.git.createRef({
...context.repo, ...context.repo,
ref: `refs/${refName}`, ref: `refs/${refName}`,
sha: sourceTagSHA sha: sourceTagSHA,
}); });
} }
} }
export async function postMessageToSlack(slackWebhook: string, message: string): Promise<void> {
const jsonData = {text: message}
const http = new HttpClient();
await http.postJson(slackWebhook, jsonData);
}

@ -1,40 +1,39 @@
import * as core from '@actions/core'; import * as core from "@actions/core";
import * as github from '@actions/github'; import * as github from "@actions/github";
import { context } from '@actions/github'; import { updateTag, validateIfReleaseIsPublished } from "./api-utils";
import { updateTag, validateIfReleaseIsPublished, postMessageToSlack } from './api-utils'; import {
import { validateSemverVersionFromTag, getMajorTagFromFullTag } from './version-utils'; validateSemverVersionFromTag,
getMajorTagFromFullTag,
} from "./version-utils";
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
const token = core.getInput('token'); const token = core.getInput("token");
const octokitClient = github.getOctokit(token); const octokitClient = github.getOctokit(token);
const sourceTagName = core.getInput('source-tag'); const sourceTagName = core.getInput("source-tag");
const rc = core.getInput("rc");
validateSemverVersionFromTag(sourceTagName); if (rc !== "") {
await updateTag(sourceTagName, "rc", octokitClient);
await validateIfReleaseIsPublished(sourceTagName, octokitClient); core.setOutput("major-tag", "rc");
core.info(`'rc' tag now points to the '${sourceTagName}' tag`);
} else {
validateSemverVersionFromTag(sourceTagName);
const majorTag = getMajorTagFromFullTag(sourceTagName); await validateIfReleaseIsPublished(sourceTagName, octokitClient);
await updateTag(sourceTagName, majorTag, octokitClient);
core.setOutput('major-tag', majorTag); const majorTag = getMajorTagFromFullTag(sourceTagName);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`); await updateTag(sourceTagName, majorTag, octokitClient);
const slackMessage = `The ${majorTag} tag has been successfully updated for the ${context.repo.repo} action to include changes from the ${sourceTagName}`; core.setOutput("major-tag", majorTag);
await reportStatusToSlack(slackMessage); core.info(
} catch (error) { `The '${majorTag}' major tag now points to the '${sourceTagName}' tag`
core.setFailed(error.message); );
const slackMessage = `Failed to update a major tag for the ${context.repo.repo} action`;
await reportStatusToSlack(slackMessage);
}
};
async function reportStatusToSlack(message: string): Promise<void> {
const slackWebhook = core.getInput('slack-webhook');
if (slackWebhook) {
await postMessageToSlack(slackWebhook, message);
} }
} catch (error) {
core.setFailed(error.message);
}
} }
run(); run();

Loading…
Cancel
Save