Skip to content

Commit

Permalink
chore: 🚧 Keep state
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 5, 2024
1 parent dea62ae commit 6bf68d3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
run: yarn install

- name: 'Run Code Limit action'
uses: getcodelimit/codelimit-action@v1
uses: getcodelimit/codelimit-action@main
41 changes: 24 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49039,14 +49039,13 @@ function generateMarkdownReport(clBinary) {
}
function updateReportsBranch(octokit, markdownReport) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const owner = (0, github_2.getRepoOwner)(github_1.context);
const repo = (0, github_2.getRepoName)(github_1.context);
if (!owner || !repo) {
console.error("Could not determine repository owner or name");
const branch = (0, github_2.getSourceBranch)();
if (!owner || !repo || !branch) {
console.error("Could not determine repository owner, name, or branch");
process.exit(1);
}
const branch = (0, github_2.getSourceBranch)();
yield (0, github_2.createBranchIfNotExists)(octokit, owner, repo, "_codelimit_reports");
const reportContent = (0, codelimit_1.getReportContent)();
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/badge.svg`, (0, codelimit_1.getBadgeContent)(reportContent));
Expand All @@ -49055,19 +49054,27 @@ function updateReportsBranch(octokit, markdownReport) {
}
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/codelimit.md`, markdownReport);
if ((0, github_2.isPullRequest)()) {
const prNumber = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
if (prNumber) {
const actionStateFile = yield (0, github_2.getFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/action.json`);
if (actionStateFile) {
const actionState = JSON.parse(actionStateFile.content);
const commentId = actionState.commentId;
yield (0, github_2.updateComment)(octokit, owner, repo, prNumber, markdownReport, commentId);
} else {
const commentId = yield (0, github_2.createPRComment)(octokit, owner, repo, prNumber, markdownReport);
const actionState = { commentId };
const actionStateJson = JSON.stringify(actionState);
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/action.json`, actionStateJson);
}
yield updatePullRequestComment(octokit, owner, repo, branch, markdownReport);
}
});
}
function updatePullRequestComment(octokit, owner, repo, branchName, markdownReport) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const prNumber = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
if (prNumber) {
const actionStateFile = yield (0, github_2.getFile)(octokit, owner, repo, "_codelimit_reports", `${branchName}/action.json`);
if (actionStateFile) {
const actionState = JSON.parse(actionStateFile.content);
const commentId = actionState.commentId;
console.log(`Updating existing comment with ID: ${commentId}`);
yield (0, github_2.updateComment)(octokit, owner, repo, prNumber, markdownReport, commentId);
} else {
console.log("State file not found, creating new comment");
const commentId = yield (0, github_2.createPRComment)(octokit, owner, repo, prNumber, markdownReport);
const actionState = { commentId };
const actionStateJson = JSON.stringify(actionState);
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branchName}/action.json`, actionStateJson);
}
}
});
Expand Down
38 changes: 22 additions & 16 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ async function generateMarkdownReport(clBinary: string) {
async function updateReportsBranch(octokit: Octokit, markdownReport: string) {
const owner = getRepoOwner(context);
const repo = getRepoName(context);
if (!owner || !repo) {
console.error('Could not determine repository owner or name');
const branch = getSourceBranch();
if (!owner || !repo || !branch) {
console.error('Could not determine repository owner, name, or branch');
process.exit(1);
}
const branch = getSourceBranch();
await createBranchIfNotExists(octokit, owner, repo, '_codelimit_reports');
const reportContent = getReportContent();
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/badge.svg`, getBadgeContent(reportContent));
Expand All @@ -43,19 +43,25 @@ async function updateReportsBranch(octokit: Octokit, markdownReport: string) {
}
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, markdownReport);
if (isPullRequest()) {
const prNumber = context.payload.pull_request?.number;
if (prNumber) {
const actionStateFile = await getFile(octokit, owner, repo, '_codelimit_reports', `${branch}/action.json`);
if (actionStateFile) {
const actionState = JSON.parse(actionStateFile.content) as ActionState;
const commentId = actionState.commentId;
await updateComment(octokit, owner, repo, prNumber, markdownReport, commentId);
} else {
const commentId = await createPRComment(octokit, owner, repo, prNumber, markdownReport);
const actionState: ActionState = {commentId: commentId};
const actionStateJson = JSON.stringify(actionState);
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/action.json`, actionStateJson);
}
await updatePullRequestComment(octokit, owner, repo, branch, markdownReport);
}
}

async function updatePullRequestComment(octokit: Octokit, owner: string, repo: string, branchName: string, markdownReport: string) {
const prNumber = context.payload.pull_request?.number;
if (prNumber) {
const actionStateFile = await getFile(octokit, owner, repo, '_codelimit_reports', `${branchName}/action.json`);
if (actionStateFile) {
const actionState = JSON.parse(actionStateFile.content) as ActionState;
const commentId = actionState.commentId;
console.log(`Updating existing comment with ID: ${commentId}`);
await updateComment(octokit, owner, repo, prNumber, markdownReport, commentId);
} else {
console.log('State file not found, creating new comment');
const commentId = await createPRComment(octokit, owner, repo, prNumber, markdownReport);
const actionState: ActionState = {commentId: commentId};
const actionStateJson = JSON.stringify(actionState);
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branchName}/action.json`, actionStateJson);
}
}
}
Expand Down

0 comments on commit 6bf68d3

Please sign in to comment.