Skip to content

Commit 97b3989

Browse files
committed
Calculate major and minor semver tags, e.g. for tagging containers
0 parents  commit 97b3989

20 files changed

+18662
-0
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
lib/
3+
node_modules/

.eslintrc.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"extends": "plugin:jest/recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 12,
9+
"sourceType": "module"
10+
},
11+
"plugins": ["jest"],
12+
"rules": {}
13+
}

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/** -diff linguist-generated=true

.github/dependabot.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: npm
4+
# Look for `package.json` and `lock` files in the `root` directory
5+
directory: /
6+
schedule:
7+
interval: weekly

.github/workflows/test.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: build-test
2+
on:
3+
pull_request:
4+
push:
5+
6+
jobs:
7+
build: # make sure build/ci work properly, and that dist was updated
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- run: npm install
12+
- run: npm run lint
13+
- run: npm run all
14+
- run: |
15+
git diff --exit-code
16+
if [ -n "$(git status --porcelain)" ]; then
17+
exit 1
18+
fi

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/
2+
node_modules/

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
lib/
3+
node_modules/

.prettierrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

LICENSE

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

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# major-minor-tag-calculator GitHub Action
2+
3+
[![build-test](https://github.com/manics/action-major-minor-tag-calculator/workflows/build-test/badge.svg)](https://github.com/manics/action-major-minor-tag-calculator/actions)
4+
5+
Calculate major and minor semver tags, e.g. for tagging containers.
6+
7+
This will calculate major and minor prefix tags for when you want to provide users with a way to obtain the latest `major` or `major.minor` version.
8+
9+
Pre-releases are ignored.
10+
11+
## Examples
12+
13+
Current and latest tag: `1.2.3`, returned tags: `[1.2.3, 1.2.0, 1.0.0, latest]`.
14+
15+
Current tag `1.2.3` but the repository already contains a more recent tag `2.0.0`: return only `[1.2.3]`.
16+
17+
## Required input parameters
18+
19+
- `githubToken`: The GitHub token, required so this action can comment on pull requests.
20+
21+
## Optional input parameters
22+
23+
- `binderUrl`: Optionally specify an alternative BinderHub instead of mybinder.org.
24+
The URL `<binderUrl>/badge_logo.svg` must exist, and will be used as the badge for linking.
25+
26+
## Example
27+
28+
```yaml
29+
name: binder-badge
30+
on:
31+
pull_request_target:
32+
33+
jobs:
34+
badge:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: manics/action-binderbadge@main
38+
with:
39+
githubToken: ${{ secrets.GITHUB_TOKEN }}
40+
```
41+
42+
## Developer notes
43+
44+
Install the dependencies:
45+
46+
```bash
47+
$ npm install
48+
```
49+
50+
Build the typescript, run the formatter and linter:
51+
52+
```bash
53+
$ npm run build && npm run format && npm run lint
54+
```
55+
56+
Package the code for distribution (uses [ncc](https://github.com/zeit/ncc)):
57+
58+
```bash
59+
$ npm run package
60+
```
61+
62+
Run the tests :heavy_check_mark:
63+
64+
```bash
65+
$ npm test
66+
```
67+
68+
The tests use [nock](https://github.com/nock/nock) to mock GitHub API responses, no real requests are made so manual testing is still required.
69+
70+
Shortcut:
71+
72+
```bash
73+
$ npm run all
74+
```
75+
76+
Actions are run from GitHub repos so you must checkin the packed `dist` folder:
77+
78+
```bash
79+
$ npm run all
80+
$ git add dist
81+
$ git commit
82+
$ git push origin main
83+
```

__tests__/main.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const { calculateTags } = require("../index");
2+
const nock = require("nock");
3+
4+
test("No tags", async () => {
5+
nock("https://api.github.com").get("/repos/owner/repo/tags").reply(200, []);
6+
const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/0.0.1");
7+
expect(tags).toEqual(new Set(["0.0.1", "0.0", "0", "latest"]));
8+
});
9+
10+
test("Is the latest tag", async () => {
11+
nock("https://api.github.com")
12+
.get("/repos/owner/repo/tags")
13+
.reply(200, [
14+
{
15+
name: "1.0.0",
16+
},
17+
]);
18+
const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/2.0.0");
19+
expect(tags).toEqual(new Set(["2.0.0", "2.0", "2", "latest"]));
20+
});
21+
22+
test("Not the latest major tag", async () => {
23+
nock("https://api.github.com")
24+
.get("/repos/owner/repo/tags")
25+
.reply(200, [
26+
{
27+
name: "1.0.0",
28+
name: "2.0.0",
29+
},
30+
]);
31+
const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/1.1.0");
32+
expect(tags).toEqual(new Set(["1.1.0"]));
33+
});
34+
35+
test("Not the latest minor tag", async () => {
36+
nock("https://api.github.com")
37+
.get("/repos/owner/repo/tags")
38+
.reply(200, [
39+
{
40+
name: "1.0.0",
41+
name: "2.0.0",
42+
name: "2.1.0",
43+
},
44+
]);
45+
const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/2.0.1");
46+
expect(tags).toEqual(new Set(["2.0.1"]));
47+
});
48+
49+
test("Ignore existing pre-releases", async () => {
50+
nock("https://api.github.com")
51+
.get("/repos/owner/repo/tags")
52+
.reply(200, [
53+
{
54+
name: "1.0.0",
55+
name: "2.0.0-rc1",
56+
},
57+
]);
58+
const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/1.1.0");
59+
expect(tags).toEqual(new Set(["1.1.0", "1.1", "1", "latest"]));
60+
});
61+
62+
test("Pre-release tag", async () => {
63+
nock("https://api.github.com")
64+
.get("/repos/owner/repo/tags")
65+
.reply(200, [
66+
{
67+
name: "1.0.0",
68+
},
69+
]);
70+
const tags = await calculateTags(
71+
"TOKEN",
72+
"owner",
73+
"repo",
74+
"refs/tags/2.0.0-rc1"
75+
);
76+
expect(tags).toEqual(new Set(["2.0.0-rc1"]));
77+
});
78+
79+
test("Not a tag", async () => {
80+
await expect(
81+
calculateTags("TOKEN", "owner", "repo", "refs/heads/main")
82+
).rejects.toEqual(new Error("Not a tag: refs/heads/main"));
83+
});
84+
85+
test("Invalid semver tag", async () => {
86+
await expect(
87+
calculateTags("TOKEN", "owner", "repo", "refs/tags/v1")
88+
).rejects.toEqual(new Error("Invalid semver tag: v1"));
89+
});

action.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: major-minor-tag-calculator
2+
description: Calculate major and minor semver tags, e.g. for tagging containers
3+
author: Simon Li
4+
inputs:
5+
githubToken:
6+
required: true
7+
description: GitHub token
8+
runs:
9+
using: node12
10+
main: dist/index.js

0 commit comments

Comments
 (0)