Skip to content

summary #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
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
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
3,758 changes: 2,736 additions & 1,022 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions dist/licenses.txt

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

51 changes: 42 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/core": "^1.10.0",
"@actions/github": "^5.0.0",
"markdown-table": "^3.0.2"
},
Expand Down
2 changes: 0 additions & 2 deletions src/compareCommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ export async function compareCommits(base: string, head: string): Promise<Commit
const response = await octokit.rest.repos.compareCommits({base, head, owner, repo})

const files = response.data.files ?? []

const newFiles: string[] = []
const modifiedFiles: string[] = []

// Also uncommon to write for loops in JS these days - unless you want async tasks to run sequentally
for (const file of files) {
if (file.status === 'added') newFiles.push(file.filename)
if (file.status === 'modified') modifiedFiles.push(file.filename)
Expand Down
69 changes: 2 additions & 67 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import * as core from '@actions/core'
import {context} from '@actions/github'
import {parseCoverageReport} from './coverage'
import {compareCommits} from './compareCommits'
import {messagePr} from './messagePr'
import {octokit} from './client'
import {scorePr} from './scorePr'
import readFile from './readFile'

const checkName = 'Coverage Results'

async function run(): Promise<void> {
let checkId = -1

try {
const coverageFile: string = core.getInput('coverageFile', {required: true})
core.debug(`coverageFile: ${coverageFile}`)
Expand All @@ -24,81 +19,21 @@ async function run(): Promise<void> {
const base = pull_request?.base.sha
const head = pull_request?.head.sha

checkId = await getCheck(head)

core.info(`comparing commits: base ${base} <> head ${head}`)
const files = await compareCommits(base, head)
core.info(`git new files: ${JSON.stringify(files.newFiles)} modified files: ${JSON.stringify(files.modifiedFiles)}`)

const report = readFile(coverageFile)
const filesCoverage = parseCoverageReport(report, files)
const {passOverall, message} = messagePr(filesCoverage)

const status = 'completed'
const conclusion = passOverall ? 'success' : 'failure'

if (checkId > 0) {
octokit.rest.checks.update({
...context.repo,
check_run_id: checkId,
status,
conclusion,
output: {title: 'Coverage Results - ', summary: message}
})
}
const passOverall = scorePr(filesCoverage)

if (!passOverall) {
core.setFailed('Coverage is lower then configured threshold 😭')
}
} catch (error) {
const message = JSON.stringify(error instanceof Error ? error.message : error)
core.setFailed(message)
if (checkId > 0) {
octokit.rest.checks.update({
...context.repo,
check_run_id: checkId,
status: 'completed',
conclusion: 'failure',
output: {title: 'Coverage Results Failed - ', summary: message}
})
}
}
}

run()

async function getCheck(head: string): Promise<number> {
const checks = await octokit.rest.checks.listForRef({
...context.repo,
ref: head
})

const existingCheck = checks.data?.check_runs?.find(check => check.name === checkName)

let checkId = -1

try {
if (existingCheck) {
checkId = existingCheck.id
core.info(`existing checkId: ${checkId}`)
await octokit.rest.checks.update({
...context.repo,
check_run_id: checkId,
status: 'in_progress'
})
} else {
const respond = await octokit.rest.checks.create({
...context.repo,
head_sha: head,
name: checkName,
status: 'in_progress'
})
checkId = respond.data.id
core.info(`new checkId: ${checkId}`)
}
} catch (e) {
core.warning('could not create a check - you might be running from a fork or token has no write permission')
}

return checkId
}
13 changes: 7 additions & 6 deletions src/messagePr.ts → src/scorePr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import {markdownTable} from 'markdown-table'
import {octokit} from './client'

const passOrFailIndicator = (predicate: boolean): string => (predicate ? '🟢' : '🔴')
const TITLE = `# ☂️ Python Cov`

export async function publishMessage(pr: number, message: string): Promise<void> {
const title = `# ☂️ Python Cov`
const body = title.concat(message)
const body = TITLE.concat(message)
core.summary.addRaw(body).write()

const comments = await octokit.rest.issues.listComments({
...context.repo,
issue_number: pr
})
const exist = comments.data.find(commnet => {
return commnet.body?.startsWith(title)
return commnet.body?.startsWith(TITLE)
})

if (exist) {
Expand Down Expand Up @@ -75,12 +76,12 @@ function formatAverageTable(cover: AverageCoverage): {coverTable: string; pass:
return {coverTable, pass: cover.pass}
}

export function messagePr(filesCover: FilesCoverage): {passOverall: boolean; message: string} {
export function scorePr(filesCover: FilesCoverage): boolean {
let message = ''
let passOverall = true

const {coverTable: avgCoverTable, pass: passTotal} = formatAverageTable(filesCover.averageCover)
core.startGroup('Results')
const {coverTable: avgCoverTable, pass: passTotal} = formatAverageTable(filesCover.averageCover)
message = message.concat(`\n## Overall Coverage\n${avgCoverTable}`)
passOverall = passOverall && passTotal
const coverAll = toPercent(filesCover.averageCover.ratio)
Expand Down Expand Up @@ -112,5 +113,5 @@ export function messagePr(filesCover: FilesCoverage): {passOverall: boolean; mes
publishMessage(context.issue.number, message)
core.endGroup()

return {passOverall, message}
return passOverall
}