Skip to content

Commit

Permalink
[sc-22046] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RMEngels committed Dec 12, 2022
1 parent 889bb08 commit ece077e
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 123 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`
2 changes: 1 addition & 1 deletion config/ActionConfiguration.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
157 changes: 73 additions & 84 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ var CommentFormatter = /** @class */ (function () {
classCoverage += '```\n\n';
return classCoverage;
};
CommentFormatter.CommentIdentifier = "coverage:";
return CommentFormatter;
}());
exports.CommentFormatter = CommentFormatter;
Expand Down Expand Up @@ -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();
Expand All @@ -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*/];
}
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -575,7 +608,7 @@ exports.DynamoDBAdapter = DynamoDBAdapter;

/***/ }),

/***/ 99723:
/***/ 87299:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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();


/***/ }),
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/CommentFormatter.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 3 additions & 5 deletions src/Actions/CommentWriter.ts
Original file line number Diff line number Diff line change
@@ -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<typeof GitHub>;
private commentFormatter: CommentFormatter;


public constructor(
private readonly pullRequest: number,
private readonly token: string,
Expand Down Expand Up @@ -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,
Expand All @@ -51,9 +50,8 @@ export class CommentWriter {
}

private async create(
comment: string,
comment: string
): Promise<void> {
// @ts-ignore
await this.octokit.rest.issues.createComment({
owner: this.owner,
repo: this.repo,
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/CoverageReader.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/Actions/CoverageWriter.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/AdapterFactory.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 18 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<ExecutionStatus> {
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<ExecutionStatus> {
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();

0 comments on commit ece077e

Please sign in to comment.