Skip to content
Draft
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
51 changes: 51 additions & 0 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as core from '@actions/core'
import type { Octokit } from '@octokit/action'
import type { Context } from './github.js'
import type { TestReport } from './junitxml.js'

export const postComment = async (testReport: TestReport, octokit: Octokit, context: Context) => {}

const format = (testReport: TestReport, context: Context): string => {
const rows = []
const failedTestCases = testReport.testCases.filter((testCase) => !testCase.success)
for (const failedTestCase of failedTestCases) {
rows.push([failedTestCase.filename, failedTestCase.name])
}
const table = `<table>${rows.map((columns) => `<tr>${columns.map((column) => `<td>${column}</td>`).join('')}</tr>`).join('')}</table>`
}

const createOrUpdateComment = async (body: string, octokit: Octokit, context: Context) => {
if (!('pull_request' in context.payload)) {
return
}

const { data: comments } = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
sort: 'created',
direction: 'desc',
per_page: 100,
})
core.info(`Found ${comments.length} comments of ${context.payload.pull_request.html_url}`)

const commentKey = `<!-- ${context.workflow} -->`
const existingComment = comments.find((comment) => comment.body?.includes(commentKey))
if (existingComment) {
core.info(`Updating the existing comment ${existingComment.id} of ${context.payload.pull_request.html_url}`)
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: `${body}\n${commentKey}`,
})
} else {
core.info(`Creating a comment into ${context.payload.pull_request.html_url}`)
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: `${body}\n${commentKey}`,
})
}
}
10 changes: 9 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import assert from 'node:assert'
import { Octokit } from '@octokit/action'
import { retry } from '@octokit/plugin-retry'
import type { WebhookEvent } from '@octokit/webhooks-types'
import * as fs from 'fs/promises'

export const getOctokit = () => new (Octokit.plugin(retry))()

export type Context = {
repo: {
Expand All @@ -10,9 +16,10 @@ export type Context = {
serverUrl: string
sha: string
workflow: string
payload: WebhookEvent
}

export const getContext = (): Context => {
export const getContext = async (): Promise<Context> => {
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
return {
repo: getRepo(),
Expand All @@ -21,6 +28,7 @@ export const getContext = (): Context => {
serverUrl: getEnv('GITHUB_SERVER_URL'),
sha: getEnv('GITHUB_SHA'),
workflow: getEnv('GITHUB_WORKFLOW'),
payload: JSON.parse(await fs.readFile(getEnv('GITHUB_EVENT_PATH'), 'utf-8')) as WebhookEvent,
}
}

Expand Down