Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Config file support #193

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ To do this, please follow this process:
1. Fork the repo.
2. Create a PR in **your own repo**.
3. The "Lint PR title preview (current branch)" workflow will run the new version and will help you validate the change.
4. Create a PR to this repo with the changes. In this case the preview workflow will fail, but we'll know that it works since you tested it in the fork. Please include a include a link to a workflow where you tested the current state of this pull request.
4. Create a PR to this repo with the changes. In this case the preview workflow will fail, but we'll know that it works since you tested it in the fork. Please include a link to a workflow where you tested the current state of this pull request.
5. Don't run `npm run build` to update the `dist` folder as it will be generated on CI during the build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

## Configuration

The action works without configuration, however you can provide options for customization.
The action works without configuration, however you can provide options for customization. You can either use inputs to pass the configuration, or add a `.github/semantic.json` file to your repository. The action inputs will override the configuration file. Lists will be appended.

The following terminology helps to understand the configuration options:

Expand Down
90 changes: 49 additions & 41 deletions src/parseConfig.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,100 @@
const {readFileSync} = require('fs');
const ConfigParser = require('./ConfigParser');

module.exports = function parseConfig() {
let types;
let config = {};
try {
config = JSON.parse(
readFileSync('.github/semantic.json', {encoding: 'utf8'})
);
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(
'The semantic.json file is not valid JSON. Please fix the syntax errors: ' +
error.message
);
}
// skipping if the file doesn't exist
}

if (process.env.INPUT_TYPES) {
types = ConfigParser.parseEnum(process.env.INPUT_TYPES);
config.types = [
...config.types,
...ConfigParser.parseEnum(process.env.INPUT_TYPES)
];
}

let scopes;
if (process.env.INPUT_SCOPES) {
scopes = ConfigParser.parseEnum(process.env.INPUT_SCOPES);
config.scopes = [
...config.scopes,
...ConfigParser.parseEnum(process.env.INPUT_SCOPES)
];
}

let requireScope;
if (process.env.INPUT_REQUIRESCOPE) {
requireScope = ConfigParser.parseBoolean(process.env.INPUT_REQUIRESCOPE);
config.requireScope = ConfigParser.parseBoolean(
process.env.INPUT_REQUIRESCOPE
);
}

let disallowScopes;
if (process.env.INPUT_DISALLOWSCOPES) {
disallowScopes = ConfigParser.parseEnum(process.env.INPUT_DISALLOWSCOPES);
config.disallowScopes = [
...config.disallowScopes,
...ConfigParser.parseEnum(process.env.INPUT_DISALLOWSCOPES)
];
}

let subjectPattern;
if (process.env.INPUT_SUBJECTPATTERN) {
subjectPattern = ConfigParser.parseString(process.env.INPUT_SUBJECTPATTERN);
config.subjectPattern = ConfigParser.parseString(
process.env.INPUT_SUBJECTPATTERN
);
}

let subjectPatternError;
if (process.env.INPUT_SUBJECTPATTERNERROR) {
subjectPatternError = ConfigParser.parseString(
config.subjectPatternError = ConfigParser.parseString(
process.env.INPUT_SUBJECTPATTERNERROR
);
}

let headerPattern;
if (process.env.INPUT_HEADERPATTERN) {
headerPattern = ConfigParser.parseString(process.env.INPUT_HEADERPATTERN);
config.headerPattern = ConfigParser.parseString(
process.env.INPUT_HEADERPATTERN
);
}

let headerPatternCorrespondence;
if (process.env.INPUT_HEADERPATTERNCORRESPONDENCE) {
headerPatternCorrespondence = ConfigParser.parseString(
config.headerPatternCorrespondence = ConfigParser.parseString(
process.env.INPUT_HEADERPATTERNCORRESPONDENCE
);
}

let wip;
if (process.env.INPUT_WIP) {
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
config.wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
}

let validateSingleCommit;
if (process.env.INPUT_VALIDATESINGLECOMMIT) {
validateSingleCommit = ConfigParser.parseBoolean(
config.validateSingleCommit = ConfigParser.parseBoolean(
process.env.INPUT_VALIDATESINGLECOMMIT
);
}

let validateSingleCommitMatchesPrTitle;
if (process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE) {
validateSingleCommitMatchesPrTitle = ConfigParser.parseBoolean(
config.validateSingleCommitMatchesPrTitle = ConfigParser.parseBoolean(
process.env.INPUT_VALIDATESINGLECOMMITMATCHESPRTITLE
);
}

let githubBaseUrl;
if (process.env.INPUT_GITHUBBASEURL) {
githubBaseUrl = ConfigParser.parseString(process.env.INPUT_GITHUBBASEURL);
config.githubBaseUrl = ConfigParser.parseString(
process.env.INPUT_GITHUBBASEURL
);
}

let ignoreLabels;
if (process.env.INPUT_IGNORELABELS) {
ignoreLabels = ConfigParser.parseEnum(process.env.INPUT_IGNORELABELS);
config.ignoreLabels = ConfigParser.parseEnum(
process.env.INPUT_IGNORELABELS
);
}

return {
types,
scopes,
requireScope,
disallowScopes,
wip,
subjectPattern,
subjectPatternError,
headerPattern,
headerPatternCorrespondence,
validateSingleCommit,
validateSingleCommitMatchesPrTitle,
githubBaseUrl,
ignoreLabels
};
return config;
};