Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 86 additions & 56 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 87 additions & 54 deletions src/github/github_comment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import { warning } from '@actions/core';
import { context, getOctokit } from '@actions/github';
import dedent from 'ts-dedent';
import type { ResultResponse } from '../stably/api/agent-api';
import type { PlaywrightResultResponse } from '../stably/api/playwright-api';
import { getSuiteRunDashboardUrl } from '../stably/url';

const isPermissionError = (error: unknown): boolean => {
if (error instanceof Error) {
const message = error.message.toLowerCase();
return (
message.includes('resource not accessible by integration') ||
message.includes('permission') ||
message.includes('403')
);
}
return false;
};

const handleCommentError = (error: unknown, action: string): void => {
if (isPermissionError(error)) {
warning(
`Skipping GitHub comment (${action}): insufficient permissions. ` +
`To enable comments, add 'contents: write' and 'pull-requests: write' to your workflow permissions. ` +
`See: https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs`
);
} else {
throw error;
}
};

export async function upsertGitHubComment(
testSuiteId: string,
githubToken: string,
Expand Down Expand Up @@ -85,34 +110,38 @@ export async function upsertGitHubComment(
)?.id;

// Create or update commit/PR comment
if (context.payload.pull_request) {
if (existingCommentId) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
body,
issue_number: context.payload.pull_request.number
});
}
} else if (commitSha) {
if (existingCommentId) {
await octokit.rest.repos.updateCommitComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.repos.createCommitComment({
...context.repo,
body,
commit_sha: commitSha
});
try {
if (context.payload.pull_request) {
if (existingCommentId) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
body,
issue_number: context.payload.pull_request.number
});
}
} else if (commitSha) {
if (existingCommentId) {
await octokit.rest.repos.updateCommitComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.repos.createCommitComment({
...context.repo,
body,
commit_sha: commitSha
});
}
}
} catch (error) {
handleCommentError(error, 'upsertGitHubComment');
}
}

Expand Down Expand Up @@ -214,33 +243,37 @@ export async function upsertGitHubCommentV2(
)?.id;

// Create or update commit/PR comment
if (context.payload.pull_request) {
if (existingCommentId) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
body,
issue_number: context.payload.pull_request.number
});
}
} else if (commitSha) {
if (existingCommentId) {
await octokit.rest.repos.updateCommitComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.repos.createCommitComment({
...context.repo,
body,
commit_sha: commitSha
});
try {
if (context.payload.pull_request) {
if (existingCommentId) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
body,
issue_number: context.payload.pull_request.number
});
}
} else if (commitSha) {
if (existingCommentId) {
await octokit.rest.repos.updateCommitComment({
...context.repo,
comment_id: existingCommentId,
body
});
} else {
await octokit.rest.repos.createCommitComment({
...context.repo,
body,
commit_sha: commitSha
});
}
}
} catch (error) {
handleCommentError(error, 'upsertGitHubCommentV2');
}
}
Loading