Skip to content

Commit

Permalink
chore: refactor action logic
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdevpereira committed Oct 18, 2022
1 parent 1068c03 commit ccb0323
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 367 deletions.
26 changes: 11 additions & 15 deletions src/Action.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const github = require('@actions/github');

const Cloudflare = require('./Cloudflare');
const Commit = require('./Commit');
const Repository = require('./Repository');

class Action {
constructor(context, config) {
this.config = config;
this.github = github.getOctokit(config.github.token);

this.repository = new Repository(
context.payload.repository.name,
context.payload.repository.owner.login,
config
);
this.commit = new Commit(context.sha, this.repository);
this.repository = new Repository(context.payload.repository.name, context.payload.repository.owner.login, this.github, config);
this.commit = new Commit(context.sha, this.repository, this.github);

this.testResults = null;
this.coverageReportUrl = null;
Expand All @@ -20,32 +19,29 @@ class Action {
async runTests() {
this.testResults = await this.repository.testFramework.runTests();

return this;
return this.testResults;
}

async saveTestResults() {
await this.repository.addCommitComment(
this.commit.hash,
JSON.stringify(this.testResults)
);
await this.commit.addComment(JSON.stringify(this.testResults));
}

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

return this;
return this.coverageReportUrl;
}

async commentOnAvailablePullRequests() {
const pullRequests = await this.repository.getPullRequests();

for (const pullRequest of pullRequests) {
await this.repository.commentPullRequest(pullRequest, this.coverageReportUrl);
}
const comment = await pullRequest.buildComment(this.coverageReportUrl);

return this;
await pullRequest.addComment(comment);
}
}
}

Expand Down
52 changes: 51 additions & 1 deletion src/Commit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const core = require('@actions/core');

class Commit {
constructor(hash, repository) {
constructor(hash, repository, github) {
this.hash = hash;
this.repository = repository;
this.github = github;
}

/**
Expand All @@ -11,6 +14,53 @@ class Commit {
shortHash() {
return this.hash.slice(0, 7);
}

async getComment() {
core.info('Retrieving comments for commit `' + this.hash + '`...');

const comments = await this.github.rest.repos.listCommentsForCommit({
owner: this.repository.owner,
repo: this.repository.name,
commit_sha: this.hash
});

if (!comments.data.length) {
core.info('Commit `' + this.hash + '` doesnt have comments yet!');
return null;
}

core.info('Found comments for commit `' + this.hash + '`!');
return comments.data[0];
}

async addComment(comment) {
const commitComment = await this.getComment();

if (commitComment) {
core.info('Updating existing commit comment `' + commitComment.id + '`...');

await this.github.rest.repos.updateCommitComment({
owner: this.repository.owner,
repo: this.repository.name,
comment_id: commitComment.id,
body: comment
});

core.info('Comment `' + commitComment.id + '` updated!');
}
else {
core.info('Creating a new commit comment...');

await this.github.rest.repos.createCommitComment({
owner: this.repository.owner,
repo: this.repository.name,
commit_sha: this.hash,
body: comment
});

core.info('Added comment to commit `' + this.hash + '`!');
}
}
}

module.exports = Commit;
Loading

0 comments on commit ccb0323

Please sign in to comment.