Skip to content

Commit

Permalink
Merge pull request #206 from daun/fix/head-sha
Browse files Browse the repository at this point in the history
Fix head sha when triggered via pull request comment
  • Loading branch information
daun authored Jul 7, 2024
2 parents cd29c73 + a658df9 commit 8d17a56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
20 changes: 14 additions & 6 deletions dist/index.js

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

10 changes: 10 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ type IssueComment = components['schemas']['issue-comment']
type PullRequestReview = components['schemas']['pull-request-review']
type Octokit = ReturnType<typeof getOctokit>

export async function getPullRequestInfo(
octokit: Octokit,
params: { owner: string; repo: string; pull_number: number }
): Promise<{ ref: string; sha: string }> {
const { data: pr } = await octokit.rest.pulls.get(params)
const { ref } = pr.base
const { sha } = pr.head
return { ref, sha }
}

export async function getIssueComments(
octokit: Octokit,
params: { owner: string; repo: string; issue_number: number }
Expand Down
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import {
import { context, getOctokit } from '@actions/github'
import { fileExists, readFile } from './fs'
import { parseReport, renderReportSummary } from './report'
import { getIssueComments, createIssueComment, updateIssueComment, createPullRequestReview } from './github'
import {
getIssueComments,
createIssueComment,
updateIssueComment,
createPullRequestReview,
getPullRequestInfo
} from './github'

/**
* The main function for the action.
Expand All @@ -34,8 +40,13 @@ export async function run(): Promise<void> {
export async function report(): Promise<void> {
const cwd = process.cwd()

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

const token = getInput('github-token')
const reportFile = getInput('report-file', { required: true })
Expand Down Expand Up @@ -76,6 +87,7 @@ export async function report(): Promise<void> {
case 'issue_comment':
if (payload.issue?.pull_request) {
pr = issueNumber
;({ ref, sha } = await getPullRequestInfo(octokit, { owner, repo, pull_number: pr }))
console.log(`Comment on PR #${pr} targeting ${ref} (${sha})`)
} else {
console.log(`Comment on issue #${issueNumber}`)
Expand Down Expand Up @@ -119,7 +131,7 @@ export async function report(): Promise<void> {
} else {
startGroup(`Commenting test report on PR`)
try {
const comments = await getIssueComments(octokit, { ...repo, issue_number: pr })
const comments = await getIssueComments(octokit, { owner, repo, issue_number: pr })
const existingComment = comments.findLast((c) => c.body?.includes(prefix))
commentId = existingComment?.id || null
} catch (error: unknown) {
Expand All @@ -129,7 +141,7 @@ export async function report(): Promise<void> {
if (commentId) {
console.log(`Found previous comment #${commentId}`)
try {
await updateIssueComment(octokit, { ...repo, comment_id: commentId, body })
await updateIssueComment(octokit, { owner, repo, comment_id: commentId, body })
console.log(`Updated previous comment #${commentId}`)
} catch (error: unknown) {
console.error(`Error updating previous comment: ${(error as Error).message}`)
Expand All @@ -140,7 +152,7 @@ export async function report(): Promise<void> {
if (!commentId) {
console.log('Creating new comment')
try {
const newComment = await createIssueComment(octokit, { ...repo, issue_number: pr, body })
const newComment = await createIssueComment(octokit, { owner, repo, issue_number: pr, body })
commentId = newComment.id
console.log(`Created new comment #${commentId}`)
} catch (error: unknown) {
Expand Down

0 comments on commit 8d17a56

Please sign in to comment.