feat: config file as option (#42)

Fix https://github.com/lampajr/backporting/issues/37

This enhancement required a huge refactoring where all arguments
defaults have been centralized in one single place ArgsParser#parse
This commit is contained in:
Andrea Lamparelli 2023-07-05 22:11:23 +02:00 committed by GitHub
parent a88adeec35
commit 5ead31f606
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 1465 additions and 219 deletions

View file

@ -1,8 +1,9 @@
import ArgsParser from "@bp/service/args/args-parser";
import { Args } from "@bp/service/args/args.types";
import { getInput } from "@actions/core";
import { readConfigFile } from "@bp/service/args/args-utils";
export default class GHAArgsParser implements ArgsParser {
export default class GHAArgsParser extends ArgsParser {
/**
* Return the input only if it is not a blank or null string, otherwise returns undefined
@ -14,39 +15,43 @@ export default class GHAArgsParser implements ArgsParser {
return value !== "" ? value : undefined;
}
public getOrDefault(key: string, defaultValue: string): string {
const value = getInput(key);
return value !== "" ? value : defaultValue;
}
public getAsCommaSeparatedList(key: string): string[] {
public getAsCommaSeparatedList(key: string): string[] | undefined {
// trim the value
const value: string = (getInput(key) ?? "").trim();
return value !== "" ? value.replace(/\s/g, "").split(",") : [];
return value !== "" ? value.replace(/\s/g, "").split(",") : undefined;
}
public getAsBooleanOrDefault(key: string, defaultValue: boolean): boolean {
private getAsBooleanOrDefault(key: string): boolean | undefined {
const value = getInput(key).trim();
return value !== "" ? value.toLowerCase() === "true" : defaultValue;
return value !== "" ? value.toLowerCase() === "true" : undefined;
}
parse(): Args {
return {
dryRun: this.getAsBooleanOrDefault("dry-run", false),
auth: getInput("auth"),
pullRequest: getInput("pull-request"),
targetBranch: getInput("target-branch"),
folder: this.getOrUndefined("folder"),
gitUser: this.getOrDefault("git-user", "GitHub"),
gitEmail: this.getOrDefault("git-email", "noreply@github.com"),
title: this.getOrUndefined("title"),
body: this.getOrUndefined("body"),
bodyPrefix: this.getOrUndefined("body-prefix"),
bpBranchName: this.getOrUndefined("bp-branch-name"),
reviewers: this.getAsCommaSeparatedList("reviewers"),
assignees: this.getAsCommaSeparatedList("assignees"),
inheritReviewers: !this.getAsBooleanOrDefault("no-inherit-reviewers", false),
};
readArgs(): Args {
const configFile = this.getOrUndefined("config-file");
let args: Args;
if (configFile) {
args = readConfigFile(configFile);
} else {
args = {
dryRun: this.getAsBooleanOrDefault("dry-run"),
auth: this.getOrUndefined("auth"),
pullRequest: getInput("pull-request"),
targetBranch: getInput("target-branch"),
folder: this.getOrUndefined("folder"),
gitUser: this.getOrUndefined("git-user"),
gitEmail: this.getOrUndefined("git-email"),
title: this.getOrUndefined("title"),
body: this.getOrUndefined("body"),
bodyPrefix: this.getOrUndefined("body-prefix"),
bpBranchName: this.getOrUndefined("bp-branch-name"),
reviewers: this.getAsCommaSeparatedList("reviewers"),
assignees: this.getAsCommaSeparatedList("assignees"),
inheritReviewers: !this.getAsBooleanOrDefault("no-inherit-reviewers"),
};
}
return args;
}
}