Skip to content

Commit

Permalink
Implement the "publish-action" action
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimZhukov committed May 21, 2021
1 parent d90d23d commit d8eb8e5
Show file tree
Hide file tree
Showing 22 changed files with 12,475 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/release-new-action-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
environment:
name: releaseNewActionVersion
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Update the ${{ env.TAG_NAME }} tag
id: update-major-tag
uses: ./
with:
source-tag: ${{ env.TAG_NAME }}

- name: Send slack message
if: failure()
run: |
curl `
-X POST `
-H 'Content-type: application/json' `
--data '{\"text\":\"Failed to update a major tag for the ${{ github.repository }} action\"}' `
${{ secrets.SLACK }}
- name: Send slack message
if: success()
run: |
curl `
-X POST `
-H 'Content-type: application/json' `
--data '{\"text\":\"The ${{ steps.update-major-tag.outputs.major-tag }} tag has been successfully updated for the ${{ github.repository }} action to include changes from the ${{ env.TAG_NAME }}\"}' `
${{ secrets.SLACK }}
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# publish-action

**Please note: this action is for internal usage only, we don't track issues or accept any contributions. We also do not recommend it for public or production usage.**

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
Action can be triggered on release creation or manually. 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).
39 changes: 39 additions & 0 deletions __tests__/api-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 14 additions & 0 deletions __tests__/data/pre-release.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
12 changes: 12 additions & 0 deletions __tests__/data/prerelease-build-semver.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions __tests__/data/prerelease-semver.json
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 14 additions & 0 deletions __tests__/data/release.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
12 changes: 12 additions & 0 deletions __tests__/data/stable-build-semver.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions __tests__/data/stable-semver.json
Original file line number Diff line number Diff line change
@@ -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"
}
69 changes: 69 additions & 0 deletions __tests__/version-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'Publish action versions'
description: 'Move the major version tag to point to the specified ref'
inputs:
source-tag:
description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3'
required: true
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'
Loading

0 comments on commit d8eb8e5

Please sign in to comment.