Skip to content

Commit 485c833

Browse files
author
Moritz Schmitz von Hülst
committed
add prettier and warning for invalid release types
1 parent 2744b29 commit 485c833

11 files changed

+198
-85
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ jobs:
1313
- uses: actions/checkout@v2
1414
- run: npm ci
1515
- run: npm run test
16+
- run: npm run check
1617
- run: npm run build

.prettierignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.github/
2+
/.vscode/
3+
/docs/
4+
/*.json
5+
/*.js
6+
/*.yml
7+
/*.md

.prettierrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"parser": "typescript"
4+
}

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"main": "lib/index.js",
77
"scripts": {
88
"build": "tsc",
9-
"test": "jest --testTimeout 10000"
9+
"test": "jest --testTimeout 10000",
10+
"check": "prettier --check ."
1011
},
1112
"repository": {
1213
"type": "git",
@@ -33,6 +34,7 @@
3334
"jest": "^26.6.3",
3435
"jest-circus": "^26.6.3",
3536
"js-yaml": "^3.14.0",
37+
"prettier": "2.1.2",
3638
"ts-jest": "^26.4.4",
3739
"typescript": "^3.8.3"
3840
}

src/github.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { context, GitHub } from "@actions/github";
2-
import * as core from "@actions/core";
3-
import { Octokit } from "@octokit/rest";
1+
import { context, GitHub } from '@actions/github';
2+
import * as core from '@actions/core';
3+
import { Octokit } from '@octokit/rest';
44

55
let octokitSingleton;
66

77
export function getOctokitSingleton() {
88
if (octokitSingleton) {
99
return octokitSingleton;
1010
}
11-
const githubToken = core.getInput("github_token");
11+
const githubToken = core.getInput('github_token');
1212
octokitSingleton = new GitHub(githubToken);
1313
return octokitSingleton;
1414
}
@@ -30,7 +30,7 @@ export async function compareCommits(sha: string) {
3030
const commits = await octokit.repos.compareCommits({
3131
...context.repo,
3232
base: sha,
33-
head: "HEAD",
33+
head: 'HEAD',
3434
});
3535

3636
return commits.data.commits;
@@ -52,7 +52,7 @@ export async function createTag(
5252
tag: newTag,
5353
message: newTag,
5454
object: GITHUB_SHA,
55-
type: "commit",
55+
type: 'commit',
5656
});
5757
}
5858

src/main.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export default async () => {
5050
.some((branch) => currentBranch.match(branch));
5151
const isPrerelease = !isReleaseBranch && isPreReleaseBranch;
5252

53-
const identifier = appendToPreReleaseTag ? appendToPreReleaseTag : currentBranch;
53+
const identifier = appendToPreReleaseTag
54+
? appendToPreReleaseTag
55+
: currentBranch;
56+
5457
const validTags = await getValidTags();
5558
const latestTag = getLatestTag(validTags);
56-
const latestPrereleaseTag = getLatestPrereleaseTag(
57-
validTags,
58-
identifier,
59-
);
59+
const latestPrereleaseTag = getLatestPrereleaseTag(validTags, identifier);
6060

6161
const commits = await getCommits(latestTag.commit.sha);
6262

@@ -72,7 +72,7 @@ export default async () => {
7272
previousTag = parse(
7373
gte(latestTag.name, latestPrereleaseTag.name)
7474
? latestTag.name
75-
: latestPrereleaseTag.name,
75+
: latestPrereleaseTag.name
7676
);
7777
}
7878

@@ -86,29 +86,27 @@ export default async () => {
8686

8787
let bump = await analyzeCommits(
8888
{ releaseRules: mappedReleaseRules },
89-
{ commits, logger: { log: console.info.bind(console) } },
89+
{ commits, logger: { log: console.info.bind(console) } }
9090
);
9191

9292
if (!bump && defaultBump === 'false') {
93-
core.debug('No commit specifies the version bump. Skipping the tag creation.');
93+
core.debug(
94+
'No commit specifies the version bump. Skipping the tag creation.'
95+
);
9496
return;
9597
}
9698

9799
// If somebody uses custom release rules on a prerelease branch they might create a 'preprepatch' bump.
98100
const preReg = /^pre/;
99101
if (isPrerelease && preReg.test(bump)) {
100-
bump = bump.replace(preReg,'');
102+
bump = bump.replace(preReg, '');
101103
}
102104

103105
const releaseType: ReleaseType = isPrerelease
104106
? `pre${bump || defaultBump}`
105107
: bump || defaultBump;
106108

107-
const incrementedVersion = inc(
108-
previousTag,
109-
releaseType,
110-
identifier,
111-
);
109+
const incrementedVersion = inc(previousTag, releaseType, identifier);
112110

113111
if (!incrementedVersion) {
114112
core.setFailed('Could not increment version.');
@@ -140,14 +138,14 @@ export default async () => {
140138
},
141139
lastRelease: { gitTag: latestTag.name },
142140
nextRelease: { gitTag: newTag, version: newVersion },
143-
},
141+
}
144142
);
145143
core.info(`Changelog is ${changelog}.`);
146144
core.setOutput('changelog', changelog);
147145

148146
if (!isReleaseBranch && !isPreReleaseBranch) {
149147
core.info(
150-
'This branch is neither a release nor a pre-release branch. Skipping the tag creation.',
148+
'This branch is neither a release nor a pre-release branch. Skipping the tag creation.'
151149
);
152150
return;
153151
}
@@ -166,4 +164,4 @@ export default async () => {
166164
} catch (error) {
167165
core.setFailed(error.message);
168166
}
169-
}
167+
};

src/utils.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as core from "@actions/core";
2-
import { Octokit } from "@octokit/rest";
3-
import { prerelease, rcompare, valid } from "semver";
4-
import DEFAULT_RELEASE_TYPES from "@semantic-release/commit-analyzer/lib/default-release-types.js";
5-
import { compareCommits, listTags } from "./github";
1+
import * as core from '@actions/core';
2+
import { Octokit } from '@octokit/rest';
3+
import { prerelease, rcompare, valid } from 'semver';
4+
import DEFAULT_RELEASE_TYPES from '@semantic-release/commit-analyzer/lib/default-release-types.js';
5+
import { compareCommits, listTags } from './github';
66

77
export async function getValidTags() {
88
const tags = await listTags();
@@ -34,15 +34,15 @@ export async function getCommits(sha: string) {
3434
}
3535

3636
export function getBranchFromRef(ref: string) {
37-
return ref.replace("refs/heads/", "");
37+
return ref.replace('refs/heads/', '');
3838
}
3939

4040
export function getLatestTag(tags: Octokit.ReposListTagsResponseItem[]) {
4141
return (
4242
tags.find((tag) => !prerelease(tag.name)) || {
43-
name: "0.0.0",
43+
name: '0.0.0',
4444
commit: {
45-
sha: "HEAD",
45+
sha: 'HEAD',
4646
},
4747
}
4848
);
@@ -57,12 +57,10 @@ export function getLatestPrereleaseTag(
5757
.find((tag) => tag.name.match(identifier));
5858
}
5959

60-
export function mapCustomReleaseRules(
61-
customReleaseTypes: string
62-
) {
60+
export function mapCustomReleaseRules(customReleaseTypes: string) {
6361
return customReleaseTypes
6462
.split(',')
65-
.map(part => {
63+
.map((part) => {
6664
const custom = part.split(':');
6765
if (custom.length !== 2) {
6866
core.warning(`${part} is not a valid custom release definition.`);
@@ -71,8 +69,14 @@ export function mapCustomReleaseRules(
7169
const [keyword, release] = custom;
7270
return {
7371
type: keyword,
74-
release
72+
release,
7573
};
7674
})
77-
.filter(customRelease => DEFAULT_RELEASE_TYPES.includes(customRelease?.release));
75+
.filter((customRelease) => {
76+
if (DEFAULT_RELEASE_TYPES.includes(customRelease?.release)) {
77+
return true;
78+
}
79+
core.warning(`${customRelease} is not a valid release type.`);
80+
return false;
81+
});
7882
}

tests/helper.test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ export function setInput(key: string, value: string) {
1515
}
1616

1717
export function setInputs(map: Object) {
18-
Object.keys(map).forEach(key => setInput(key, map[key]));
18+
Object.keys(map).forEach((key) => setInput(key, map[key]));
1919
}
2020

2121
export function loadDefaultInputs() {
2222
const actionYaml = fs.readFileSync(path.join(process.cwd(), 'action.yml'));
2323
const actionJson = yaml.safeLoad(actionYaml);
24-
const defaultInputs = Object
25-
.keys(actionJson.inputs)
26-
.filter(key => actionJson.inputs[key].default)
27-
.reduce((obj, key) => ({ ...obj, [key]: actionJson.inputs[key].default }), {});
24+
const defaultInputs = Object.keys(actionJson.inputs)
25+
.filter((key) => actionJson.inputs[key].default)
26+
.reduce(
27+
(obj, key) => ({ ...obj, [key]: actionJson.inputs[key].default }),
28+
{}
29+
);
2830
setInputs(defaultInputs);
2931
}
3032

0 commit comments

Comments
 (0)