From ece077e1f2af1648e1a1087f6a51cfed836ed34d Mon Sep 17 00:00:00 2001 From: Ruben Engels <16747379+RMEngels@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:10:30 +0100 Subject: [PATCH] [sc-22046] refactor --- README.md | 13 +- config/ActionConfiguration.ts | 2 +- dist/index.js | 157 ++++++++---------- src/Actions/CommentFormatter.ts | 4 +- src/Actions/CommentWriter.ts | 8 +- src/Actions/CoverageReader.ts | 2 +- src/Actions/CoverageWriter.ts | 2 +- src/Factory/AdapterFactory.ts | 6 +- src/{Adapters => StorageAdapters}/Adapter.ts | 0 .../DynamoDBAdapter.ts | 0 .../JsonblobAdapter.ts | 0 src/index.ts | 44 ++--- 12 files changed, 115 insertions(+), 123 deletions(-) rename src/{Adapters => StorageAdapters}/Adapter.ts (100%) rename src/{Adapters => StorageAdapters}/DynamoDBAdapter.ts (100%) rename src/{Adapters => StorageAdapters}/JsonblobAdapter.ts (100%) diff --git a/README.md b/README.md index a26ff94..4d28a31 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,17 @@ To build the dist file run: ``` This coverts the TS files to JS and bundles them into the `dist/index.js` file +### Secrets +To function with the current adapters you need to input env variables (presumably with secrets). these are +#### JsonblobAdapter +- `JSONBLOB_ID` + +#### DynamoDBAdapter +- `AWS_REGION` +- `AWS_SECRET_ACCESS_KEY` +- `AWS_ACCESS_KEY_ID` +- `COVERAGE_STORAGE_ID` + ### Storage adapters To each their own; If you want to extend the functionality of this action with your own storage method you kan do it in the form of a new `StorageAdapter` Currently the available storage adapters are: @@ -16,4 +27,4 @@ Currently the available storage adapters are: A new adapter must implement the `Adapter` interface to insure compatibility with the current structure. All credentials must be gotten through the environment. -Dont forget to append to the `AdapterType` and `adapterMap` +Don't forget to append to the `AdapterType` and `adapterMap` diff --git a/config/ActionConfiguration.ts b/config/ActionConfiguration.ts index cbed459..1d99cad 100644 --- a/config/ActionConfiguration.ts +++ b/config/ActionConfiguration.ts @@ -1,5 +1,5 @@ import { getInput, setFailed } from "@actions/core"; -import { Adapter, Coverage } from "../src/Adapters/Adapter"; +import { Adapter, Coverage } from "../src/StorageAdapters/Adapter"; import { AdapterFactory } from "../src/Factory/AdapterFactory"; export type ActionType = "read" | "save"; diff --git a/dist/index.js b/dist/index.js index 457edc0..9f45fe5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -145,6 +145,7 @@ var CommentFormatter = /** @class */ (function () { classCoverage += '```\n\n'; return classCoverage; }; + CommentFormatter.CommentIdentifier = "coverage:"; return CommentFormatter; }()); exports.CommentFormatter = CommentFormatter; @@ -229,7 +230,7 @@ var CommentWriter = /** @class */ (function () { case 2: if (!(_i < _b.length)) return [3 /*break*/, 5]; comment = _b[_i]; - if (!((_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes("coverage:"))) return [3 /*break*/, 4]; + if (!((_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(CommentFormatter_1.CommentFormatter.CommentIdentifier))) return [3 /*break*/, 4]; return [4 /*yield*/, this.update(commentBody, comment.id)]; case 3: _c.sent(); @@ -249,16 +250,13 @@ var CommentWriter = /** @class */ (function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { - case 0: - // @ts-ignore - return [4 /*yield*/, this.octokit.rest.issues.createComment({ + case 0: return [4 /*yield*/, this.octokit.rest.issues.createComment({ owner: this.owner, repo: this.repo, issue_number: this.pullRequest, body: comment })]; case 1: - // @ts-ignore _a.sent(); return [2 /*return*/]; } @@ -449,7 +447,42 @@ exports.CoverageWriter = CoverageWriter; /***/ }), -/***/ 84553: +/***/ 66638: +/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { + +"use strict"; + +exports.__esModule = true; +exports.AdapterFactory = void 0; +var core_1 = __nccwpck_require__(42186); +var DynamoDBAdapter_1 = __nccwpck_require__(18827); +var JsonblobAdapter_1 = __nccwpck_require__(87299); +var AdapterFactory = /** @class */ (function () { + function AdapterFactory() { + } + AdapterFactory.createAdapter = function (adapterType) { + if (!this.isValidAdapterType(adapterType)) { + var error = new Error("".concat(adapterType, " is not a valid adapter type, valid adapter types are Jsonblob and DynamoDB")); + (0, core_1.setFailed)(error); + throw error; + } + return new this.adapterMap[adapterType](); + }; + AdapterFactory.isValidAdapterType = function (adapterType) { + return ["DynamoDB", "Jsonblob"].includes(adapterType); + }; + AdapterFactory.adapterMap = { + Jsonblob: JsonblobAdapter_1.JsonblobAdapter, + DynamoDB: DynamoDBAdapter_1.DynamoDBAdapter + }; + return AdapterFactory; +}()); +exports.AdapterFactory = AdapterFactory; + + +/***/ }), + +/***/ 18827: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -575,7 +608,7 @@ exports.DynamoDBAdapter = DynamoDBAdapter; /***/ }), -/***/ 99723: +/***/ 87299: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -650,41 +683,6 @@ var JsonblobAdapter = /** @class */ (function () { exports.JsonblobAdapter = JsonblobAdapter; -/***/ }), - -/***/ 66638: -/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { - -"use strict"; - -exports.__esModule = true; -exports.AdapterFactory = void 0; -var core_1 = __nccwpck_require__(42186); -var DynamoDBAdapter_1 = __nccwpck_require__(84553); -var JsonblobAdapter_1 = __nccwpck_require__(99723); -var AdapterFactory = /** @class */ (function () { - function AdapterFactory() { - } - AdapterFactory.createAdapter = function (adapterType) { - if (!this.isValidAdapterType(adapterType)) { - var error = new Error("".concat(adapterType, " is not a valid adapter type, valid adapter types are Jsonblob and DynamoDB")); - (0, core_1.setFailed)(error); - throw error; - } - return new this.adapterMap[adapterType](); - }; - AdapterFactory.isValidAdapterType = function (adapterType) { - return ["DynamoDB", "Jsonblob"].includes(adapterType); - }; - AdapterFactory.adapterMap = { - Jsonblob: JsonblobAdapter_1.JsonblobAdapter, - DynamoDB: DynamoDBAdapter_1.DynamoDBAdapter - }; - return AdapterFactory; -}()); -exports.AdapterFactory = AdapterFactory; - - /***/ }), /***/ 19957: @@ -734,50 +732,41 @@ var ActionConfiguration_1 = __nccwpck_require__(24167); var Action_1 = __nccwpck_require__(67025); var CoverageReader_1 = __nccwpck_require__(54414); var CoverageWriter_1 = __nccwpck_require__(86260); -var CoverageActions = /** @class */ (function () { - function CoverageActions(config) { - this.config = config; - this.bootstrap(); - } - CoverageActions.prototype.bootstrap = function () { - return __awaiter(this, void 0, void 0, function () { - var executionStatus; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, this.run()]; - case 1: - executionStatus = _a.sent(); - if (executionStatus === Action_1.ExecutionStatus.Failed) { - (0, core_1.setFailed)('The coverage actions have failed'); - return [2 /*return*/]; - } - (0, core_1.info)('The coverage actions have succeeded'); - return [2 /*return*/]; - } - }); - }); - }; - CoverageActions.prototype.run = function () { - return __awaiter(this, void 0, void 0, function () { - var coverageReader, coverageWriter; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - if (!(this.config.actionType === "read")) return [3 /*break*/, 2]; - coverageReader = new CoverageReader_1.CoverageReader(this.config); - return [4 /*yield*/, coverageReader.execute()]; - case 1: return [2 /*return*/, _a.sent()]; - case 2: - coverageWriter = new CoverageWriter_1.CoverageWriter(this.config); - return [4 /*yield*/, coverageWriter.execute()]; - case 3: return [2 /*return*/, _a.sent()]; +var main = function () { return __awaiter(void 0, void 0, void 0, function () { + var executionStatus; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, run(ActionConfiguration_1.ActionConfiguration.loadConfiguration())]; + case 1: + executionStatus = _a.sent(); + if (executionStatus === Action_1.ExecutionStatus.Failed) { + (0, core_1.setFailed)('The coverage actions have failed'); + return [2 /*return*/]; } - }); + (0, core_1.info)('The coverage actions have succeeded'); + return [2 /*return*/]; + } + }); +}); }; +function run(config) { + return __awaiter(this, void 0, void 0, function () { + var coverageReader, coverageWriter; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!(config.actionType === "read")) return [3 /*break*/, 2]; + coverageReader = new CoverageReader_1.CoverageReader(config); + return [4 /*yield*/, coverageReader.execute()]; + case 1: return [2 /*return*/, _a.sent()]; + case 2: + coverageWriter = new CoverageWriter_1.CoverageWriter(config); + return [4 /*yield*/, coverageWriter.execute()]; + case 3: return [2 /*return*/, _a.sent()]; + } }); - }; - return CoverageActions; -}()); -exports["default"] = new CoverageActions(ActionConfiguration_1.ActionConfiguration.loadConfiguration()); + }); +} +main(); /***/ }), diff --git a/src/Actions/CommentFormatter.ts b/src/Actions/CommentFormatter.ts index 08253d1..49399b1 100644 --- a/src/Actions/CommentFormatter.ts +++ b/src/Actions/CommentFormatter.ts @@ -1,6 +1,8 @@ -import { Coverage, CoverageDiff } from "../Adapters/Adapter"; +import { Coverage, CoverageDiff } from "../StorageAdapters/Adapter"; export class CommentFormatter { + + public static readonly CommentIdentifier: string = "coverage:"; public constructor( private readonly coverageDiff: CoverageDiff, diff --git a/src/Actions/CommentWriter.ts b/src/Actions/CommentWriter.ts index 788b3da..27a3661 100644 --- a/src/Actions/CommentWriter.ts +++ b/src/Actions/CommentWriter.ts @@ -1,13 +1,12 @@ import { getOctokit } from "@actions/github"; import { GitHub } from "@actions/github/lib/utils"; -import { Coverage, CoverageDiff } from "../Adapters/Adapter"; +import { Coverage, CoverageDiff } from "../StorageAdapters/Adapter"; import { CommentFormatter } from "./CommentFormatter"; export class CommentWriter { private octokit: InstanceType; private commentFormatter: CommentFormatter; - public constructor( private readonly pullRequest: number, private readonly token: string, @@ -36,7 +35,7 @@ export class CommentWriter { }); for (const comment of Object.values(comments.data)) { - if (comment.body?.includes("coverage:")) { + if (comment.body?.includes(CommentFormatter.CommentIdentifier)) { await this.update( commentBody, comment.id, @@ -51,9 +50,8 @@ export class CommentWriter { } private async create( - comment: string, + comment: string ): Promise { - // @ts-ignore await this.octokit.rest.issues.createComment({ owner: this.owner, repo: this.repo, diff --git a/src/Actions/CoverageReader.ts b/src/Actions/CoverageReader.ts index 2c1105b..9da7d43 100644 --- a/src/Actions/CoverageReader.ts +++ b/src/Actions/CoverageReader.ts @@ -1,5 +1,5 @@ import { ActionConfiguration } from "../../config/ActionConfiguration"; -import { Coverage, CoverageDiff } from "../Adapters/Adapter"; +import { Coverage, CoverageDiff } from "../StorageAdapters/Adapter"; import { CommentWriter } from "./CommentWriter"; import { Action, ExecutionStatus } from "./Action"; diff --git a/src/Actions/CoverageWriter.ts b/src/Actions/CoverageWriter.ts index dcbc939..bb0e450 100644 --- a/src/Actions/CoverageWriter.ts +++ b/src/Actions/CoverageWriter.ts @@ -1,5 +1,5 @@ import { ActionConfiguration } from "../../config/ActionConfiguration"; -import { Coverage } from "../Adapters/Adapter"; +import { Coverage } from "../StorageAdapters/Adapter"; import { Action, ExecutionStatus } from "./Action"; export class CoverageWriter implements Action { diff --git a/src/Factory/AdapterFactory.ts b/src/Factory/AdapterFactory.ts index 81d33eb..6e167bb 100644 --- a/src/Factory/AdapterFactory.ts +++ b/src/Factory/AdapterFactory.ts @@ -1,7 +1,7 @@ import { setFailed } from "@actions/core"; -import { Adapter } from "../Adapters/Adapter"; -import { DynamoDBAdapter } from "../Adapters/DynamoDBAdapter"; -import { JsonblobAdapter } from "../Adapters/JsonblobAdapter"; +import { Adapter } from "../StorageAdapters/Adapter"; +import { DynamoDBAdapter } from "../StorageAdapters/DynamoDBAdapter"; +import { JsonblobAdapter } from "../StorageAdapters/JsonblobAdapter"; export type AdapterType = "Jsonblob" | "DynamoDB"; diff --git a/src/Adapters/Adapter.ts b/src/StorageAdapters/Adapter.ts similarity index 100% rename from src/Adapters/Adapter.ts rename to src/StorageAdapters/Adapter.ts diff --git a/src/Adapters/DynamoDBAdapter.ts b/src/StorageAdapters/DynamoDBAdapter.ts similarity index 100% rename from src/Adapters/DynamoDBAdapter.ts rename to src/StorageAdapters/DynamoDBAdapter.ts diff --git a/src/Adapters/JsonblobAdapter.ts b/src/StorageAdapters/JsonblobAdapter.ts similarity index 100% rename from src/Adapters/JsonblobAdapter.ts rename to src/StorageAdapters/JsonblobAdapter.ts diff --git a/src/index.ts b/src/index.ts index b25a643..e8bfe7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,36 +4,28 @@ import { ExecutionStatus } from "./Actions/Action"; import { CoverageReader } from "./Actions/CoverageReader"; import { CoverageWriter } from "./Actions/CoverageWriter"; -class CoverageActions { - constructor( - private readonly config: ActionConfiguration - ) { - this.bootstrap(); - } - - private async bootstrap(): Promise { - const executionStatus = await this.run(); +const main = async () => { + const executionStatus = await run( + ActionConfiguration.loadConfiguration(), + ); - if (executionStatus === ExecutionStatus.Failed) { - setFailed('The coverage actions have failed'); - return; - } - - info('The coverage actions have succeeded'); + if (executionStatus === ExecutionStatus.Failed) { + setFailed('The coverage actions have failed'); return; } - private async run(): Promise { - if (this.config.actionType === "read") { - const coverageReader = new CoverageReader(this.config); - return await coverageReader.execute(); - } + info('The coverage actions have succeeded'); + return; +} - const coverageWriter = new CoverageWriter(this.config); - return await coverageWriter.execute(); - } +async function run(config: ActionConfiguration): Promise { + if (config.actionType === "read") { + const coverageReader = new CoverageReader(config); + return await coverageReader.execute(); +} + + const coverageWriter = new CoverageWriter(config); + return await coverageWriter.execute(); } -export default new CoverageActions( - ActionConfiguration.loadConfiguration(), -); +main(); \ No newline at end of file