-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handling of the configuration file. Closes #5 This adds the field to the action file to look for the configuration file so we can set a different field, and defaults to `.github/review.yml`. Created the basic type for the configuration file. This adds the: - rules array - no rule types yet - they accept an array of include and exclude conditions. - the `preventReviewRequests` - has been renamed from `prevent-review-requests` to `preventReviewRequests` Created tests to evaluate all this types and validations. Also created JOI validations for the configuration object. I have discovered that it still won't be enough in the case that a regex is invalid, so I added #16 to have visibility of it. ## Miscelanious ### Created the PullRequestApi class This class will use the default github secret generated by the action. We will have a second class to handle the teams. It is mocked in the tests. ### Created the Runner class This will be the main class that will combine all the other classes and APIs (basically the `core.ts` file but with proper abstractions).
- Loading branch information
Showing
18 changed files
with
471 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/node_modules | ||
/dist | ||
.env | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
!.vscode/settings.json | ||
.idea | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint", | ||
"editor.insertSpaces": true, | ||
"editor.tabSize": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { preset: "ts-jest", testEnvironment: "node", testMatch: [__dirname + "/src/**/test/**/*.ts"] }; | ||
module.exports = { preset: "ts-jest", testEnvironment: "node", testMatch: [__dirname + "/src/**/test/**/*.test.ts"] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface Rule { | ||
name: string; | ||
condition: { include: string[]; exclude?: string[] }; | ||
} | ||
|
||
export interface ConfigurationFile { | ||
rules: Rule[]; | ||
preventReviewRequests: { | ||
teams?: string[]; | ||
users: string[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Joi from "joi"; | ||
|
||
import { ConfigurationFile, Rule } from "./types"; | ||
|
||
const ruleSchema = Joi.object<Rule>().keys({ | ||
name: Joi.string().required(), | ||
condition: Joi.object<Rule["condition"]>().keys({ | ||
include: Joi.array().items(Joi.string()).required(), | ||
exclude: Joi.array().items(Joi.string()).optional().allow(null), | ||
}), | ||
}); | ||
|
||
export const schema = Joi.object<ConfigurationFile>().keys({ | ||
rules: Joi.array<ConfigurationFile["rules"]>().items(ruleSchema).required(), | ||
preventReviewRequests: Joi.object<ConfigurationFile["preventReviewRequests"]>() | ||
.keys({ | ||
users: Joi.array().items(Joi.string()).optional().allow(null), | ||
teams: Joi.array().items(Joi.string()).optional().allow(null), | ||
}) | ||
.optional() | ||
.allow(null), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { PullRequest } from "@octokit/webhooks-types"; | ||
|
||
import { ActionLogger, GitHubClient } from "./types"; | ||
|
||
/** API class that uses the default token to access the data from the pull request and the repository */ | ||
export class PullRequestApi { | ||
constructor( | ||
private readonly api: GitHubClient, | ||
private readonly pr: PullRequest, | ||
private readonly logger: ActionLogger, | ||
) {} | ||
|
||
async getConfigFile(configFilePath: string): Promise<string> { | ||
const { data } = await this.api.rest.repos.getContent({ | ||
owner: this.pr.base.repo.owner.login, | ||
repo: this.pr.base.repo.name, | ||
path: configFilePath, | ||
}); | ||
|
||
if (!("content" in data)) { | ||
throw new Error(`${configFilePath} has no content`); | ||
} | ||
|
||
this.logger.debug(`Content is ${data.content}`); | ||
|
||
const decryptedFile = Buffer.from(data.content, "base64").toString("utf-8"); | ||
|
||
this.logger.debug(`File content is ${decryptedFile}`); | ||
|
||
return decryptedFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { GitHub } from "@actions/github/lib/utils"; | ||
|
||
export interface ActionLogger { | ||
debug(message: string): void; | ||
info(message: string): void; | ||
warn(message: string | Error): void; | ||
error(message: string | Error): void; | ||
} | ||
|
||
export type GitHubClient = InstanceType<typeof GitHub>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { validate } from "@eng-automation/js"; | ||
import { parse } from "yaml"; | ||
|
||
import { Inputs } from "."; | ||
import { ConfigurationFile } from "./file/types"; | ||
import { schema } from "./file/validator"; | ||
import { PullRequestApi } from "./github/pullRequest"; | ||
import { ActionLogger } from "./github/types"; | ||
|
||
/** Action in charge of running the GitHub action */ | ||
export class ActionRunner { | ||
constructor(private readonly prApi: PullRequestApi, private readonly logger: ActionLogger) {} | ||
|
||
/** | ||
* Fetches the configuration file, parses it and validates it. | ||
* If the config is invalid or not found, an error will be thrown. | ||
*/ | ||
async getConfigFile(configLocation: string): Promise<ConfigurationFile> { | ||
const content = await this.prApi.getConfigFile(configLocation); | ||
this.logger.debug(content); | ||
const config: unknown = parse(content); | ||
|
||
this.logger.info(`Obtained config at ${configLocation}`); | ||
|
||
return validate<ConfigurationFile>(config, schema, { message: "Configuration file is invalid" }); | ||
} | ||
|
||
async runAction(inputs: Omit<Inputs, "repoToken">): Promise<boolean> { | ||
const config = await this.getConfigFile(inputs.configLocation); | ||
|
||
return config !== null; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ActionLogger } from "../github/types"; | ||
|
||
export class TestLogger implements ActionLogger { | ||
logHistory: string[] = []; | ||
|
||
debug(message: string): void { | ||
this.logHistory.push(message); | ||
} | ||
info(message: string): void { | ||
this.logHistory.push(message); | ||
} | ||
warn(arg: string | Error): void { | ||
this.logHistory.push(typeof arg === "string" ? arg : arg.message); | ||
} | ||
error(arg: string | Error): void { | ||
this.logHistory.push(typeof arg === "string" ? arg : arg.message); | ||
} | ||
} |
Oops, something went wrong.