Skip to content

Commit

Permalink
chore: refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdevpereira committed Oct 4, 2022
1 parent 519bca6 commit b0d0d7a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 104 deletions.
90 changes: 37 additions & 53 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12061,42 +12061,27 @@ var src_default = shellac;
;// CONCATENATED MODULE: ./src/Cloudflare.js



const COVERAGE_OUTPUT_FOLDER = './coverage';

class Cloudflare {
constructor(config) {
this.token = config.token;
this.apiToken = config.apiToken;
this.accountId = config.accountId;
this.projectName = config.projectName;
this.baseUrl = config.baseCloudflareDeploymentUrl;
this.baseUrl = config.baseUrl;
}

async publish(commitSha) {

core_default().startGroup('Uploading to Cloudflare Pages...');

await src_default`
$ export CLOUDFLARE_API_TOKEN="${this.token}"
$ export CLOUDFLARE_API_TOKEN="${this.apiToken}"
$ export CLOUDFLARE_ACCOUNT_ID="${this.accountId}"

$$ npx wrangler@2 pages publish "${COVERAGE_OUTPUT_FOLDER}" --project-name="${this.projectName}" --branch="${commitSha}"
`;

core_default().endGroup();

return `https://${commitSha}.${this.baseUrl}`;
}
}
;// CONCATENATED MODULE: ./src/config/cloudflare.js


/* harmony default export */ const config_cloudflare = ({
cloudflareProjectName: core_default().getInput('cloudflareProjectName', { required: true }),
cloudflareApiToken: core_default().getInput('cloudflareApiToken', { required: true }),
cloudflareAccountId: core_default().getInput('cloudflareAccountId', { required: true }),
baseCloudflareDeploymentUrl: core_default().getInput('baseCloudflareDeploymentUrl')
});
// EXTERNAL MODULE: external "fs"
var external_fs_ = __nccwpck_require__(7147);
var external_fs_default = /*#__PURE__*/__nccwpck_require__.n(external_fs_);
Expand All @@ -12106,7 +12091,6 @@ var exec = __nccwpck_require__(1514);




const SUPPORTED_TEST_FRAMEWORKS = ['jest'];
const Framework_COVERAGE_OUTPUT_FOLDER = './coverage';

Expand All @@ -12133,8 +12117,6 @@ class Framework {
* in this repository 😉).
**/
async runTests() {
core_default().startGroup('Running Jest Tests...');

const JEST_PATH = './node_modules/jest/bin/jest.js';
const JEST_FLAGS = '--no-cache --detectOpenHandles --coverage --json';
const RESULT_OUTPUT_FILE = `${Framework_COVERAGE_OUTPUT_FOLDER}/test-results.json`;
Expand All @@ -12151,24 +12133,9 @@ class Framework {
external_fs_default().writeFileSync(RESULT_OUTPUT_FILE, results);
this.testResults = JSON.parse(results);

core_default().endGroup();

return this.testResults;
}
}
;// CONCATENATED MODULE: ./src/config/framework.js


/* harmony default export */ const framework = ({
framework: core_default().getInput('framework')
});
;// CONCATENATED MODULE: ./src/config/github.js


/* harmony default export */ const config_github = ({
token: core_default().getInput('githubToken', { required: true }),
branch: core_default().getInput('branchName', { required: true })
});
// EXTERNAL MODULE: ./node_modules/@actions/http-client/lib/index.js
var lib = __nccwpck_require__(6255);
;// CONCATENATED MODULE: ./src/utils/getReports.js
Expand Down Expand Up @@ -12363,20 +12330,18 @@ function CalculateTimeTaken(startedAt, endedAt) {







/**
* Represents a Github repository
*/
class Repository {
constructor(name, owner) {
constructor(name, owner, config) {
this.name = name;
this.owner = owner;
this.branch = config_github.branch;
this.github = github_default().getOctokit(config_github.token);
this.testFramework = new Framework(framework.framework);
this.config = config;

this.branch = this.config.github.branch;
this.github = github_default().getOctokit(this.config.github.token);
this.testFramework = new Framework(this.config.testing.framework);
}

async getPullRequests() {
Expand Down Expand Up @@ -12406,8 +12371,6 @@ class Repository {
}

async commentPullRequest(pullRequest, testResults, fullReportUrl) {
core_default().startGroup('Comment on Pull Request or Commit...');

const { data: comments } = await this.github.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
Expand All @@ -12418,7 +12381,7 @@ class Repository {
const headResult = await GetReport({ reportUrl: `${fullReportUrl}/coverage-summary.json` });
const baseResult = await GetReport(
{
reportUrl: `https://${pullRequest.baseBranchShortSha}.${config_cloudflare.baseCloudflareDeploymentUrl}/coverage-summary.json`,
reportUrl: `https://${pullRequest.baseBranchShortSha}.${this.config.cloudflare.baseUrl}/coverage-summary.json`,
retryCount: 0,
ignoreErrors: true
}
Expand Down Expand Up @@ -12453,19 +12416,18 @@ class Repository {
body: commentBody
});
}

core_default().endGroup();
}
}
;// CONCATENATED MODULE: ./src/index.js





class GithubAction {
constructor(context) {
constructor(context, config) {
this.context = context;
this.config = config;

this.repository = new Repository(context.repo.repo, context.repo.owner);
this.commit = new Commit(context.sha, this.repository);

Expand All @@ -12480,7 +12442,7 @@ class GithubAction {
}

async publishToCloudflare() {
const cloudflare = new Cloudflare(config_cloudflare);
const cloudflare = new Cloudflare(this.config.cloudflare);
this.coverageReportUrl = await cloudflare.publish(this.commit.shortHash());

return this;
Expand All @@ -12504,11 +12466,33 @@ class GithubAction {
// 🚀 Execute Github Action
(async () => {
try {
const Action = new GithubAction((github_default()).context);
const Action = new GithubAction((github_default()).context, {
testing: {
framework: core_default().getInput('framework')
},
github: {
token: core_default().getInput('githubToken', { required: true }),
branch: core_default().getInput('branchName', { required: true })
},
cloudflare: {
projectName: core_default().getInput('cloudflareProjectName', { required: true }),
apiToken: core_default().getInput('cloudflareApiToken', { required: true }),
accountId: core_default().getInput('cloudflareAccountId', { required: true }),
baseUrl: core_default().getInput('baseCloudflareDeploymentUrl')
}
});

core_default().startGroup('Running Jest Tests...');
await Action.runTests();
core_default().endGroup();

core_default().startGroup('Uploading to Cloudflare Pages...');
await Action.publishToCloudflare();
core_default().endGroup();

core_default().startGroup('Comment on available Pull Requests...');
await Action.commentOnAvailablePullRequests();
core_default().endGroup();

} catch (error) {
core_default().setFailed(error.message);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ import GithubAction from './src/index';
// 🚀 Execute Github Action
(async () => {
try {
const Action = new GithubAction(github.context);
const Action = new GithubAction(github.context, {
testing: {
framework: core.getInput('framework')
},
github: {
token: core.getInput('githubToken', { required: true }),
branch: core.getInput('branchName', { required: true })
},
cloudflare: {
projectName: core.getInput('cloudflareProjectName', { required: true }),
apiToken: core.getInput('cloudflareApiToken', { required: true }),
accountId: core.getInput('cloudflareAccountId', { required: true }),
baseUrl: core.getInput('baseCloudflareDeploymentUrl')
}
});

core.startGroup('Running Jest Tests...');
await Action.runTests();
core.endGroup();

core.startGroup('Uploading to Cloudflare Pages...');
await Action.publishToCloudflare();
core.endGroup();

core.startGroup('Comment on available Pull Requests...');
await Action.commentOnAvailablePullRequests();
core.endGroup();

} catch (error) {
core.setFailed(error.message);
Expand Down
12 changes: 3 additions & 9 deletions src/Cloudflare.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import core from '@actions/core';
import shellac from 'shellac';

const COVERAGE_OUTPUT_FOLDER = './coverage';

export default class Cloudflare {
constructor(config) {
this.token = config.token;
this.apiToken = config.apiToken;
this.accountId = config.accountId;
this.projectName = config.projectName;
this.baseUrl = config.baseCloudflareDeploymentUrl;
this.baseUrl = config.baseUrl;
}

async publish(commitSha) {

core.startGroup('Uploading to Cloudflare Pages...');

await shellac`
$ export CLOUDFLARE_API_TOKEN="${this.token}"
$ export CLOUDFLARE_API_TOKEN="${this.apiToken}"
$ export CLOUDFLARE_ACCOUNT_ID="${this.accountId}"
$$ npx wrangler@2 pages publish "${COVERAGE_OUTPUT_FOLDER}" --project-name="${this.projectName}" --branch="${commitSha}"
`;

core.endGroup();

return `https://${commitSha}.${this.baseUrl}`;
}
}
5 changes: 0 additions & 5 deletions src/Framework.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs';
import core from '@actions/core';
import { exec } from '@actions/exec';

const SUPPORTED_TEST_FRAMEWORKS = ['jest'];
Expand Down Expand Up @@ -28,8 +27,6 @@ export default class Framework {
* in this repository 😉).
**/
async runTests() {
core.startGroup('Running Jest Tests...');

const JEST_PATH = './node_modules/jest/bin/jest.js';
const JEST_FLAGS = '--no-cache --detectOpenHandles --coverage --json';
const RESULT_OUTPUT_FILE = `${COVERAGE_OUTPUT_FOLDER}/test-results.json`;
Expand All @@ -46,8 +43,6 @@ export default class Framework {
fs.writeFileSync(RESULT_OUTPUT_FILE, results);
this.testResults = JSON.parse(results);

core.endGroup();

return this.testResults;
}
}
20 changes: 7 additions & 13 deletions src/Repository.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import core from '@actions/core';
import github from '@actions/github';
import Framework from './Framework';
import frameworkConfig from './config/framework';
import githubConfig from './config/github';
import cloudflareConfig from './config/cloudflare';
import GetReport, { TotalPercentagesAverage } from './utils/getReports';
import { BuildCommentBody } from './utils/buildComment';

/**
* Represents a Github repository
*/
export default class Repository {
constructor(name, owner) {
constructor(name, owner, config) {
this.name = name;
this.owner = owner;
this.branch = githubConfig.branch;
this.github = github.getOctokit(githubConfig.token);
this.testFramework = new Framework(frameworkConfig.framework);
this.config = config;

this.branch = this.config.github.branch;
this.github = github.getOctokit(this.config.github.token);
this.testFramework = new Framework(this.config.testing.framework);
}

async getPullRequests() {
Expand Down Expand Up @@ -46,8 +44,6 @@ export default class Repository {
}

async commentPullRequest(pullRequest, testResults, fullReportUrl) {
core.startGroup('Comment on Pull Request or Commit...');

const { data: comments } = await this.github.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
Expand All @@ -58,7 +54,7 @@ export default class Repository {
const headResult = await GetReport({ reportUrl: `${fullReportUrl}/coverage-summary.json` });
const baseResult = await GetReport(
{
reportUrl: `https://${pullRequest.baseBranchShortSha}.${cloudflareConfig.baseCloudflareDeploymentUrl}/coverage-summary.json`,
reportUrl: `https://${pullRequest.baseBranchShortSha}.${this.config.cloudflare.baseUrl}/coverage-summary.json`,
retryCount: 0,
ignoreErrors: true
}
Expand Down Expand Up @@ -93,7 +89,5 @@ export default class Repository {
body: commentBody
});
}

core.endGroup();
}
}
8 changes: 0 additions & 8 deletions src/config/cloudflare.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/config/framework.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/config/github.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Commit from './Commit';
import Cloudflare from './Cloudflare';
import cloudflareConfig from './config/cloudflare';
import Repository from './Repository';

export default class GithubAction {
constructor(context) {
constructor(context, config) {
this.context = context;
this.config = config;

this.repository = new Repository(context.repo.repo, context.repo.owner);
this.commit = new Commit(context.sha, this.repository);

Expand All @@ -20,7 +21,7 @@ export default class GithubAction {
}

async publishToCloudflare() {
const cloudflare = new Cloudflare(cloudflareConfig);
const cloudflare = new Cloudflare(this.config.cloudflare);
this.coverageReportUrl = await cloudflare.publish(this.commit.shortHash());

return this;
Expand Down

0 comments on commit b0d0d7a

Please sign in to comment.