Merge pull request #1 from actions/v-mazhuk/implement-publish-action

Implement the "publish-action" action
This commit is contained in:
MaksimZhukov 2021-05-26 13:19:10 +03:00 committed by GitHub
commit a2705dedd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 12507 additions and 0 deletions

View File

@ -0,0 +1,34 @@
name: Release new action version
on:
release:
types: [released]
workflow_dispatch:
inputs:
TAG_NAME:
description: 'Tag name that the major tag will point to'
required: true
env:
TAG_NAME: ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }}
defaults:
run:
shell: pwsh
jobs:
update_tag:
name: Update the major tag to include the ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }} changes
# Remember to configure the releaseNewActionVersion environment with required approvers in the repository settings
environment:
name: releaseNewActionVersion
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v2
- name: Update the ${{ env.TAG_NAME }} tag
id: update-major-tag
uses: ./
with:
source-tag: ${{ env.TAG_NAME }}
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}

27
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,27 @@
name: Tests
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set Node.JS
uses: actions/setup-node@v2
with:
node-version: 12.x
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Run tests
run: npm run test

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
lib/

22
LICENSE Normal file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2018 GitHub, Inc. and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1 +1,26 @@
# publish-action # publish-action
**Please note: this action is for internal usage only, issues are disabled and contributing PRs will not be reviewed. We also do not recommend this action for public or production usage while it is still in development.**
This action adds reliability to the new action versions publishing and handles the following cases:
- Update a major tag (v1, for example) to point to the latest release (v1.2.1, for example).
- Create a major tag from the latest released tag if a major tag doesn't exist
## Status
Alpha. Action is under development and internal testing.
## Usage
This action can be triggered automatically when a release is created or manually using a `workflow_dispatch` event. The actual major tag update will require manual approval.
See [release-new-action-version.yml](./.github/workflows/release-new-action-version.yml) for usage example.
See [action.yml](action.yml) for a complete description of input and output fields.
Read more about action versioning notation in [action-versioning.md](https://github.com/actions/toolkit/blob/main/docs/action-versioning.md).
To roll back a release in case of customer impact, start the workflow manually and specify the previous stable tag.
## Conributions
We don't accept contributions until the action is ready for production.
## License
The scripts and documentation in this project are released under the [MIT License](LICENSE).

View File

@ -0,0 +1,39 @@
import * as github from "@actions/github";
import * as apiUtils from "../src/api-utils";
const prereleaseData = require("./data/pre-release.json");
const releaseData = require("./data/release.json");
const token = "faketoken";
const octokitClient = github.getOctokit(token);
let getReleaseSpy: jest.SpyInstance;
process.env.GITHUB_REPOSITORY = "test/repository";
describe("validateIfReleaseIsPublished", () => {
beforeEach(() => {
getReleaseSpy = jest.spyOn(octokitClient.repos, "getReleaseByTag");
});
it("throw if release is marked as pre-release", async () => {
getReleaseSpy.mockReturnValue(prereleaseData);
expect.assertions(1);
await expect(apiUtils.validateIfReleaseIsPublished("v1.0.0", octokitClient)).rejects.toThrowError(
"The 'v1.0.0' release is marked as pre-release. Updating tags for pre-release is not supported"
);
});
it("validate that release is published", async () => {
getReleaseSpy.mockReturnValue(releaseData);
expect.assertions(1);
await expect(apiUtils.validateIfReleaseIsPublished("v1.1.0", octokitClient)).resolves.not.toThrow();
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
});

View File

@ -0,0 +1,14 @@
{
"data": {
"id": 1,
"node_id": "MDc6UmVsZWFzZTE=",
"tag_name": "v1.0.0",
"target_commitish": "main",
"name": "v1.0.0",
"body": "Description of the release",
"draft": false,
"prerelease": true,
"created_at": "2013-02-27T19:35:32Z",
"published_at": "2013-02-27T19:35:32Z"
}
}

View File

@ -0,0 +1,12 @@
{
"options": {},
"loose": false,
"includePrerelease": false,
"raw": "v1.0.0-beta.1+20130313144700",
"major": 1,
"minor": 0,
"patch": 0,
"prerelease": [ "beta", 1 ],
"build": [ "20130313144700" ],
"version": "1.0.0-beta.1"
}

View File

@ -0,0 +1,12 @@
{
"options": {},
"loose": false,
"includePrerelease": false,
"raw": "v1.0.0-beta.1",
"major": 1,
"minor": 0,
"patch": 0,
"prerelease": [ "beta", 1 ],
"build": [],
"version": "1.0.0-beta.1"
}

View File

@ -0,0 +1,14 @@
{
"data": {
"id": 1,
"node_id": "MDc6UmVsZWFzZTE=",
"tag_name": "v1.1.0",
"target_commitish": "main",
"name": "v1.1.0",
"body": "Description of the release",
"draft": false,
"prerelease": false,
"created_at": "2013-02-27T19:35:32Z",
"published_at": "2013-02-27T19:35:32Z"
}
}

View File

@ -0,0 +1,12 @@
{
"options": {},
"loose": false,
"includePrerelease": false,
"raw": "1.0.0+20130313144700",
"major": 1,
"minor": 0,
"patch": 0,
"prerelease": [],
"build": [ "20130313144700" ],
"version": "1.0.0"
}

View File

@ -0,0 +1,12 @@
{
"options": {},
"loose": false,
"includePrerelease": false,
"raw": "v1.0.0",
"major": 1,
"minor": 0,
"patch": 0,
"prerelease": [],
"build": [],
"version": "1.0.0"
}

View File

@ -0,0 +1,69 @@
import * as versionUtils from "../src/version-utils";
describe("isStableSemverVersion", () => {
it("validate if a version is stable", () => {
const semverVersion = require("./data/stable-semver.json");
expect(versionUtils.isStableSemverVersion(semverVersion)).toBeTruthy();
});
it("validate if a version with build metadata is stable", () => {
const semverVersion = require("./data/stable-build-semver.json");
expect(versionUtils.isStableSemverVersion(semverVersion)).toBeTruthy();
});
it("validate if a pre-release version is not stable", () => {
const semverVersion = require("./data/prerelease-semver.json");
expect(versionUtils.isStableSemverVersion(semverVersion)).toBeFalsy();
});
it("validate if a pre-release version with build metadata is not stable", () => {
const semverVersion = require("./data/prerelease-build-semver.json");
expect(versionUtils.isStableSemverVersion(semverVersion)).toBeFalsy();
});
});
describe("validateSemverVersionFromTag", () => {
it("validate a tag containing an valid semantic version", () => {
expect(() => versionUtils.validateSemverVersionFromTag("1.0.0")).not.toThrow();
});
it("validate a tag containing an valid semantic version with 'v' prefix", () => {
expect(() => versionUtils.validateSemverVersionFromTag("v1.0.0")).not.toThrow();
});
it("validate a tag containing an valid semantic version with build metadata", () => {
expect(() => versionUtils.validateSemverVersionFromTag("v1.0.0+20130313144700")).not.toThrow();
});
it("throw when a tag contains an invalid semantic version", () => {
expect(() => versionUtils.validateSemverVersionFromTag("1.0.0invalid")).toThrowError(
"The '1.0.0invalid' doesn't satisfy semantic versioning specification"
);
});
it("throw when a tag contains an valid unstable semantic version", () => {
expect(() => versionUtils.validateSemverVersionFromTag("v1.0.0-beta.1")).toThrowError(
"It is not allowed to specify pre-release version to update the major tag"
);
});
it("throw when a tag contains an valid unstable semantic version with build metadata", () => {
expect(() => versionUtils.validateSemverVersionFromTag("v1.0.0-beta.1+20130313144700")).toThrowError(
"It is not allowed to specify pre-release version to update the major tag"
);
});
});
describe("getMajorTagFromFullTag", () => {
describe("get a valid major tag from full tag", () => {
it.each([
["1.0.0", "1"],
["v1.0.0", "v1"],
["v1.0.0-beta.1", "v1"],
["v1.0.0+20130313144700", "v1"],
] as [string, string][])("%s -> %s", (sourceTag: string, expectedMajorTag: string) => {
const resultantMajorTag = versionUtils.getMajorTagFromFullTag(sourceTag);
expect(resultantMajorTag).toBe(expectedMajorTag);
});
});
});

17
action.yml Normal file
View File

@ -0,0 +1,17 @@
name: 'Publish action versions'
description: 'Move the major version tag to point to a specified ref'
inputs:
source-tag:
description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3'
required: true
slack-webhook:
description: 'Slack Webhook URL to post a message'
token:
description: 'Token to get an authenticated Octokit'
default: ${{ github.token }}
outputs:
major-tag:
description: 'The major version tag that has been updated (created). Examples: v1, 1'
runs:
using: 'node12'
main: 'dist/index.js'

6862
dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

11
jest.config.js Normal file
View File

@ -0,0 +1,11 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}

5086
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

39
package.json Normal file
View File

@ -0,0 +1,39 @@
{
"name": "publish-action",
"version": "0.1.0",
"description": "Update the major version tag (v1, v2, etc.) to point to the specified tag",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/actions/publish-action.git"
},
"keywords": [
"actions",
"publish"
],
"author": "GitHub",
"license": "MIT",
"bugs": {
"url": "https://github.com/actions/publish-action/issues"
},
"homepage": "https://github.com/actions/publish-action#readme",
"dependencies": {
"@actions/core": "^1.2.7",
"@actions/github": "^4.0.0"
},
"devDependencies": {
"@actions/http-client": "^1.0.11",
"@types/jest": "^26.0.23",
"@types/semver": "^7.3.6",
"@vercel/ncc": "^0.28.5",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"semver": "^7.3.5",
"ts-jest": "^26.5.6",
"typescript": "^4.2.4"
}
}

114
src/api-utils.ts Normal file
View File

@ -0,0 +1,114 @@
import * as core from '@actions/core';
import { context } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';
import { HttpClient } from '@actions/http-client';
interface GitRef {
ref: string;
node_id: string;
url: string;
object: {
type: string;
sha: string;
url: string;
};
}
async function findTag(
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}`
);
}
}
}
async function getTagSHA(
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`
);
}
return foundTag.object.sha;
}
export async function validateIfReleaseIsPublished(
tag: string,
octokitClient: InstanceType<typeof GitHub>
): Promise<void> {
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}`
);
}
}
}
export async function updateTag(
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}`;
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.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);
}

40
src/main.ts Normal file
View File

@ -0,0 +1,40 @@
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';
async function run(): Promise<void> {
try {
const token = core.getInput('token');
const octokitClient = github.getOctokit(token);
const sourceTagName = core.getInput('source-tag');
validateSemverVersionFromTag(sourceTagName);
await validateIfReleaseIsPublished(sourceTagName, octokitClient);
const majorTag = getMajorTagFromFullTag(sourceTagName);
await updateTag(sourceTagName, majorTag, octokitClient);
core.setOutput('major-tag', majorTag);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`);
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);
}
}
run();

25
src/version-utils.ts Normal file
View File

@ -0,0 +1,25 @@
import semverParse from 'semver/functions/parse';
import SemVer from 'semver/classes/semver';
export function isStableSemverVersion(version: SemVer): boolean {
return version.prerelease.length === 0
}
export function getMajorTagFromFullTag(fullTag: string): string {
return fullTag.split('.')[0];
}
export function validateSemverVersionFromTag(tag: string): void {
const semverVersion = semverParse(tag);
if (!semverVersion) {
throw new Error(
`The '${tag}' doesn't satisfy semantic versioning specification`
);
}
if (!isStableSemverVersion(semverVersion)) {
throw new Error(
'It is not allowed to specify pre-release version to update the major tag'
);
}
}

19
tsconfig.json Normal file
View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"strictFunctionTypes": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true
},
"include": ["./src/**/*.ts"],
"exclude": ["node_modules"]
}