Add rc tag
parent
9b5fe1fd8f
commit
5cdf6d60b5
File diff suppressed because it is too large
Load Diff
@ -1,114 +1,105 @@
|
||||
import * as core from '@actions/core';
|
||||
import { context } from '@actions/github';
|
||||
import { GitHub } from '@actions/github/lib/utils';
|
||||
import { HttpClient } from '@actions/http-client';
|
||||
import * as core from "@actions/core";
|
||||
import { context } from "@actions/github";
|
||||
import { GitHub } from "@actions/github/lib/utils";
|
||||
|
||||
interface GitRef {
|
||||
ref: string;
|
||||
node_id: string;
|
||||
ref: string;
|
||||
node_id: string;
|
||||
url: string;
|
||||
object: {
|
||||
type: string;
|
||||
sha: string;
|
||||
url: string;
|
||||
object: {
|
||||
type: string;
|
||||
sha: string;
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
async function findTag(
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
): Promise<GitRef | null> {
|
||||
try {
|
||||
const { data: foundTag } = await octokitClient.git.getRef({
|
||||
...context.repo,
|
||||
ref: `tags/${tag}`
|
||||
});
|
||||
|
||||
return foundTag;
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
return null;
|
||||
} else {
|
||||
throw new Error(
|
||||
`Retrieving refs failed with the following error: ${err}`
|
||||
);
|
||||
}
|
||||
try {
|
||||
const { data: foundTag } = await octokitClient.git.getRef({
|
||||
...context.repo,
|
||||
ref: `tags/${tag}`,
|
||||
});
|
||||
|
||||
return foundTag;
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
return null;
|
||||
} else {
|
||||
throw new Error(
|
||||
`Retrieving refs failed with the following error: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getTagSHA(
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
): Promise<string> {
|
||||
const foundTag = await findTag(tag, octokitClient);
|
||||
if (!foundTag) {
|
||||
throw new Error(
|
||||
`The '${tag}' tag does not exist in the remote repository`
|
||||
);
|
||||
}
|
||||
const foundTag = await findTag(tag, octokitClient);
|
||||
if (!foundTag) {
|
||||
throw new Error(`The '${tag}' tag does not exist in the remote repository`);
|
||||
}
|
||||
|
||||
return foundTag.object.sha;
|
||||
return foundTag.object.sha;
|
||||
}
|
||||
|
||||
export async function validateIfReleaseIsPublished(
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
tag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { data: foundRelease } = await octokitClient.repos.getReleaseByTag({
|
||||
...context.repo,
|
||||
tag,
|
||||
});
|
||||
try {
|
||||
const { data: foundRelease } = await octokitClient.repos.getReleaseByTag({
|
||||
...context.repo,
|
||||
tag,
|
||||
});
|
||||
|
||||
if (foundRelease.prerelease) {
|
||||
throw new Error(
|
||||
`The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
throw new Error(
|
||||
`No GitHub release found for the ${tag} tag`
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Retrieving releases failed with the following error: ${err}`
|
||||
);
|
||||
}
|
||||
if (foundRelease.prerelease) {
|
||||
throw new Error(
|
||||
`The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
throw new Error(`No GitHub release found for the ${tag} tag`);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Retrieving releases failed with the following error: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateTag(
|
||||
sourceTag: string,
|
||||
targetTag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
sourceTag: string,
|
||||
targetTag: string,
|
||||
octokitClient: InstanceType<typeof GitHub>
|
||||
): Promise<void> {
|
||||
const sourceTagSHA = await getTagSHA(sourceTag, octokitClient);
|
||||
const foundTargetTag = await findTag(targetTag, octokitClient);
|
||||
const refName = `tags/${targetTag}`;
|
||||
const sourceTagSHA = await getTagSHA(sourceTag, octokitClient);
|
||||
const foundTargetTag = await findTag(targetTag, octokitClient);
|
||||
const refName = `tags/${targetTag}`;
|
||||
|
||||
if (foundTargetTag) {
|
||||
core.info(`Updating the '${targetTag}' tag to point to the '${sourceTag}' tag`);
|
||||
if (foundTargetTag) {
|
||||
core.info(
|
||||
`Updating the '${targetTag}' tag to point to the '${sourceTag}' tag`
|
||||
);
|
||||
|
||||
await octokitClient.git.updateRef({
|
||||
...context.repo,
|
||||
ref: refName,
|
||||
sha: sourceTagSHA,
|
||||
force: true
|
||||
});
|
||||
} else {
|
||||
core.info(`Creating the '${targetTag}' tag from the '${sourceTag}' tag`);
|
||||
await octokitClient.git.updateRef({
|
||||
...context.repo,
|
||||
ref: refName,
|
||||
sha: sourceTagSHA,
|
||||
force: true,
|
||||
});
|
||||
} else {
|
||||
core.info(`Creating the '${targetTag}' tag from the '${sourceTag}' tag`);
|
||||
|
||||
await octokitClient.git.createRef({
|
||||
...context.repo,
|
||||
ref: `refs/${refName}`,
|
||||
sha: sourceTagSHA
|
||||
});
|
||||
}
|
||||
await octokitClient.git.createRef({
|
||||
...context.repo,
|
||||
ref: `refs/${refName}`,
|
||||
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 github from '@actions/github';
|
||||
import { context } from '@actions/github';
|
||||
import { updateTag, validateIfReleaseIsPublished, postMessageToSlack } from './api-utils';
|
||||
import { validateSemverVersionFromTag, getMajorTagFromFullTag } from './version-utils';
|
||||
import * as core from "@actions/core";
|
||||
import * as github from "@actions/github";
|
||||
import { updateTag, validateIfReleaseIsPublished } from "./api-utils";
|
||||
import {
|
||||
validateSemverVersionFromTag,
|
||||
getMajorTagFromFullTag,
|
||||
} from "./version-utils";
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const token = core.getInput('token');
|
||||
const octokitClient = github.getOctokit(token);
|
||||
const sourceTagName = core.getInput('source-tag');
|
||||
try {
|
||||
const token = core.getInput("token");
|
||||
const octokitClient = github.getOctokit(token);
|
||||
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 updateTag(sourceTagName, majorTag, octokitClient);
|
||||
await validateIfReleaseIsPublished(sourceTagName, octokitClient);
|
||||
|
||||
core.setOutput('major-tag', majorTag);
|
||||
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`);
|
||||
const majorTag = getMajorTagFromFullTag(sourceTagName);
|
||||
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}`;
|
||||
await reportStatusToSlack(slackMessage);
|
||||
} catch (error) {
|
||||
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);
|
||||
core.setOutput("major-tag", majorTag);
|
||||
core.info(
|
||||
`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
run();
|
||||
|
||||
Loading…
Reference in New Issue