Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: harupy <hkawamura0130@gmail.com>
- Loading branch information
Showing
2 changed files
with
46 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,39 @@ | ||
const getData = async (context, github) => { | ||
const createCommitStatus = async (context, github, sha, state) => { | ||
const { workflow, runId } = context; | ||
const { owner, repo } = context.repo; | ||
const pull_number = context.issue.number; | ||
const pr = await github.pulls.get({ owner, repo, pull_number }); | ||
const head_sha = pr.data.head.sha; | ||
const run_url = `https://github.com/${owner}/${repo}/actions/runs/${runId}`; | ||
const pr_url = `https://github.com/${owner}/${repo}/pull/${pull_number}`; | ||
return { | ||
const target_url = `https://github.com/${owner}/${repo}/actions/runs/${runId}`; | ||
await github.repos.createCommitStatus({ | ||
owner, | ||
repo, | ||
head_sha, | ||
workflow, | ||
run_url, | ||
pr_url, | ||
}; | ||
}; | ||
|
||
const objectToMarkdownTable = (obj) => { | ||
return [ | ||
'|Key|Value|', | ||
'|---|-----|', | ||
...Object.entries(obj).map(([key, value]) => `|${key}|${value}|`), | ||
].join('\n'); | ||
sha, | ||
state, | ||
target_url, | ||
description: sha, | ||
context: workflow, | ||
}); | ||
}; | ||
|
||
const createCheckRun = async (context, github) => { | ||
const { owner, repo, head_sha, workflow, run_url, pr_url } = await getData(context, github); | ||
const status = 'in_progress'; | ||
const summaryObject = { | ||
head_sha, | ||
run_url, | ||
pr_url, | ||
status: status, | ||
conclusion: '-', | ||
const createStatus = async (context, github) => { | ||
const { owner, repo } = context.repo; | ||
const pull_number = context.issue.number; | ||
const pr = await github.pulls.get({ owner, repo, pull_number }); | ||
const { sha, ref } = pr.data.head; | ||
await createCommitStatus(context, github, sha, 'pending'); | ||
return { | ||
repository: `${owner}/${repo}`, | ||
pull_number, | ||
sha, | ||
ref, | ||
}; | ||
const summary = objectToMarkdownTable(summaryObject); | ||
const check_run = await github.checks.create({ | ||
owner, | ||
repo, | ||
head_sha, | ||
status, | ||
name: workflow, | ||
output: { | ||
title: workflow, | ||
summary, | ||
}, | ||
}); | ||
return check_run.data.id; | ||
}; | ||
|
||
const updateCheckRun = async (context, github, check_run_id, needs) => { | ||
const { owner, repo, head_sha, workflow, run_url, pr_url } = await getData(context, github); | ||
const updateStatus = async (context, github, sha, needs) => { | ||
const failed = Object.values(needs).some(({ result }) => result === 'failure'); | ||
const conclusion = failed ? 'failure' : 'success'; | ||
const summaryObject = { | ||
head_sha, | ||
run_url, | ||
pr_url, | ||
status: 'completed', | ||
conclusion: conclusion, | ||
}; | ||
const summary = objectToMarkdownTable(summaryObject); | ||
await github.checks.update({ | ||
owner, | ||
repo, | ||
check_run_id, | ||
conclusion, | ||
output: { | ||
title: workflow, | ||
summary, | ||
}, | ||
}); | ||
const state = failed ? 'failure' : 'success'; | ||
await createCommitStatus(context, github, sha, state); | ||
}; | ||
|
||
module.exports = { | ||
createCheckRun, | ||
updateCheckRun, | ||
createStatus, | ||
updateStatus, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters