Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
daun committed Jul 7, 2024
2 parents 118957e + b9734f6 commit cd29c73
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.5.0] - 2024-07-07

- Support pull request comment as workflow trigger

## [3.4.0] - 2024-07-02

- Add ability to display custom information by @fungairino
Expand Down Expand Up @@ -61,6 +65,8 @@

- Initial release

[3.5.0]: https://github.com/daun/playwright-report-summary/releases/tag/v3.5.0
[3.4.0]: https://github.com/daun/playwright-report-summary/releases/tag/v3.4.0
[3.3.0]: https://github.com/daun/playwright-report-summary/releases/tag/v3.3.0
[3.2.0]: https://github.com/daun/playwright-report-summary/releases/tag/v3.2.0
[3.1.0]: https://github.com/daun/playwright-report-summary/releases/tag/v3.1.0
Expand Down
55 changes: 34 additions & 21 deletions dist/index.js

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

56 changes: 37 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function report(): Promise<void> {
const cwd = process.cwd()

const { workflow, eventName, repo, payload } = context
const { owner, number: pull_number } = context.issue || {}
const { owner, number: issueNumber } = context.issue || {}

const token = getInput('github-token')
const reportFile = getInput('report-file', { required: true })
Expand All @@ -54,21 +54,41 @@ export async function report(): Promise<void> {

let ref: string = context.ref
let sha: string = context.sha
let pr: number | null = null

const octokit = getOctokit(token)

if (eventName === 'push') {
ref = payload.ref
sha = payload.after
console.log(`Commit pushed onto ${ref} (${sha})`)
} else if (eventName === 'pull_request' || eventName === 'pull_request_target') {
ref = payload.pull_request?.base?.ref
sha = payload.pull_request?.head?.sha
console.log(`PR #${pull_number} targeting ${ref} (${sha})`)
} else if (eventName === 'workflow_dispatch') {
console.log(`Workflow dispatched on ${ref} (${sha})`)
} else {
console.warn(`Unsupported event type: ${eventName}`)
switch (eventName) {
case 'push':
ref = payload.ref
sha = payload.after
console.log(`Commit pushed onto ${ref} (${sha})`)
break

case 'pull_request':
case 'pull_request_target':
ref = payload.pull_request?.base?.ref
sha = payload.pull_request?.head?.sha
pr = issueNumber
console.log(`PR #${pr} targeting ${ref} (${sha})`)
break

case 'issue_comment':
if (payload.issue?.pull_request) {
pr = issueNumber
console.log(`Comment on PR #${pr} targeting ${ref} (${sha})`)
} else {
console.log(`Comment on issue #${issueNumber}`)
}
break

case 'workflow_dispatch':
console.log(`Workflow dispatched on ${ref} (${sha})`)
break

default:
console.warn(`Unsupported event type: ${eventName}`)
break
}

const reportPath = path.resolve(cwd, reportFile)
Expand All @@ -94,14 +114,12 @@ export async function report(): Promise<void> {
const body = `${prefix}\n\n${summary}`
let commentId = null

const hasPR = eventName === 'pull_request' || eventName === 'pull_request_target'

if (!hasPR) {
if (!pr) {
console.log('No PR associated with this action run. Not posting a check or comment.')
} else {
startGroup(`Commenting test report on PR`)
try {
const comments = await getIssueComments(octokit, { ...repo, issue_number: pull_number })
const comments = await getIssueComments(octokit, { ...repo, issue_number: pr })
const existingComment = comments.findLast((c) => c.body?.includes(prefix))
commentId = existingComment?.id || null
} catch (error: unknown) {
Expand All @@ -122,7 +140,7 @@ export async function report(): Promise<void> {
if (!commentId) {
console.log('Creating new comment')
try {
const newComment = await createIssueComment(octokit, { ...repo, issue_number: pull_number, body })
const newComment = await createIssueComment(octokit, { ...repo, issue_number: pr, body })
commentId = newComment.id
console.log(`Created new comment #${commentId}`)
} catch (error: unknown) {
Expand All @@ -145,7 +163,7 @@ export async function report(): Promise<void> {
endGroup()
}

if (!commentId && hasPR) {
if (!commentId && pr) {
const intro = `Unable to comment on your PR — this can happen for PR's originating from a fork without write permissions. You can copy the test results directly into a comment using the markdown summary below:`
warning(`${intro}\n\n${body}`, { title: 'Unable to comment on PR' })
}
Expand Down

0 comments on commit cd29c73

Please sign in to comment.