Skip to content

Commit

Permalink
use commit status
Browse files Browse the repository at this point in the history
Signed-off-by: harupy <hkawamura0130@gmail.com>
  • Loading branch information
harupy committed Nov 28, 2021
1 parent 3a1c4c0 commit 47f7ca2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 106 deletions.
90 changes: 25 additions & 65 deletions .github/workflows/autoformat.js
@@ -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,
};
62 changes: 21 additions & 41 deletions .github/workflows/autoformat.yml
Expand Up @@ -15,7 +15,10 @@ jobs:
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'autoformat') }}
outputs:
matched: ${{ steps.check-comment.outputs.result }}
check_run_id: ${{ steps.create-check-run.outputs.result }}
repository: ${{ fromJSON(steps.create-status.outputs.result).repository }}
ref: ${{ fromJSON(steps.create-status.outputs.result).ref }}
sha: ${{ fromJSON(steps.create-status.outputs.result).sha }}
pull_number: ${{ fromJSON(steps.create-status.outputs.result).pull_number }}
steps:
- uses: actions/checkout@v2
- name: Check comment
Expand All @@ -25,48 +28,25 @@ jobs:
result-encoding: string
script: |
return context.payload.comment.body.trim() === 'autoformat';
- name: Create check run
id: create-check-run
- name: Create status
id: create-status
if: ${{ steps.check-comment.outputs.result == 'true' }}
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const autoformat = require('./.github/workflows/autoformat.js');
return await autoformat.createCheckRun(context, github);
return await autoformat.createStatus(context, github);
check-diff:
runs-on: ubuntu-latest
needs: check-comment
if: ${{ needs.check-comment.outputs.matched == 'true' }}
outputs:
repository: ${{ steps.format-result.outputs.repository }}
ref: ${{ steps.format-result.outputs.ref }}
py_changed: ${{ steps.check-diff.outputs.py_changed }}
ui_changed: ${{ steps.check-diff.outputs.ui_changed }}
steps:
- uses: actions/github-script@v4
id: get-pr
with:
script: |
const { owner, repo } = context.repo;
const pull_number = context.issue.number;
const pr = await github.pulls.get({ owner, repo, pull_number });
return pr.data;
- name: Format result
id: format-result
run: |
repository="${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}"
ref="${{ fromJSON(steps.get-pr.outputs.result).head.ref }}"
pull_number="${{ fromJSON(steps.get-pr.outputs.result).number }}"
echo "::set-output name=repository::$repository"
echo "::set-output name=ref::$ref"
echo "::set-output name=pull_number::$pull_number"
- uses: actions/checkout@v2
with:
repository: ${{ steps.format-result.outputs.repository }}
ref: ${{ steps.format-result.outputs.ref }}
- uses: actions/setup-python@v2
with:
python-version: '3.6'
Expand All @@ -75,8 +55,8 @@ jobs:
- name: Check diff
id: check-diff
run: |
repository="${{ steps.format-result.outputs.repository }}"
pull_number="${{ steps.format-result.outputs.pull_number }}"
repository="${{ needs.check-comment.outputs.repository }}"
pull_number="${{ needs.check-comment.outputs.pull_number }}"
changed_files="$(python dev/list_changed_files.py --repository $repository --pr-num $pull_number)"
py_changed=$([[ -z $(echo "$changed_files" | grep '\.py$') ]] && echo "false" || echo "true")
ui_changed=$([[ -z $(echo "$changed_files" | grep '^mlflow/server/js') ]] && echo "false" || echo "true")
Expand All @@ -86,15 +66,15 @@ jobs:
python:
# Generate a patch to format python files.
runs-on: ubuntu-latest
needs: check-diff
needs: [check-comment, check-diff]
if: ${{ needs.check-diff.outputs.py_changed == 'true' }}
outputs:
has_diff: ${{ steps.check-diff.outputs.has_diff }}
steps:
- uses: actions/checkout@v2
with:
repository: ${{ needs.check-diff.outputs.repository }}
ref: ${{ needs.check-diff.outputs.ref }}
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
- uses: actions/setup-python@v2
with:
python-version: '3.6'
Expand All @@ -121,7 +101,7 @@ jobs:
ui:
# Generate a patch to format files for MLflow UI.
runs-on: ubuntu-latest
needs: check-diff
needs: [check-comment, check-diff]
if: ${{ needs.check-diff.outputs.ui_changed == 'true' }}
outputs:
has_diff: ${{ steps.check-diff.outputs.has_diff }}
Expand All @@ -131,8 +111,8 @@ jobs:
steps:
- uses: actions/checkout@v2
with:
repository: ${{ needs.check-diff.outputs.repository }}
ref: ${{ needs.check-diff.outputs.ref }}
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
- uses: actions/setup-node@v1
with:
node-version: 10.x
Expand Down Expand Up @@ -160,16 +140,16 @@ jobs:
apply-patches:
# Apply the patches and commit changes to the PR branch.
runs-on: ubuntu-latest
needs: [check-diff, python, ui]
needs: [check-comment, check-diff, python, ui]
if: |
always() &&
(needs.python.result == 'success' && needs.python.outputs.has_diff == 'true') ||
(needs.ui.result == 'success' && needs.ui.outputs.has_diff == 'true')
steps:
- uses: actions/checkout@v2
with:
repository: ${{ needs.check-diff.outputs.repository }}
ref: ${{ needs.check-diff.outputs.ref }}
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
# As described in the doc below, if we use `secrets.GITHUB_TOKEN`, a commit created by
# this workflow will not trigger other workflows:
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow
Expand Down Expand Up @@ -200,7 +180,7 @@ jobs:
update-status:
runs-on: ubuntu-latest
needs: [check-comment, check-diff, python, ui, apply-patches]
if: ${{ always() && needs.check-comment.outputs.check_run_id != '' }}
if: ${{ always() && needs.check-comment.outputs.matched == 'true' }}
steps:
- uses: actions/checkout@v2
- name: Update status
Expand All @@ -209,6 +189,6 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const needs = ${{ toJson(needs) }};
const check_run_id = '${{ needs.check-comment.outputs.check_run_id }}';
const sha = '${{ needs.check-comment.outputs.sha }}'
const autoformat = require('./.github/workflows/autoformat.js');
await autoformat.updateCheckRun(context, github, check_run_id, needs);
await autoformat.updateStatus(context, github, sha, needs);

0 comments on commit 47f7ca2

Please sign in to comment.