Skip to content

Commit

Permalink
Post PR result comment (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 22, 2020
1 parent 5f71714 commit 1b5f154
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
uses: ./
with:
category: theme
comment: false
repository: ${{ matrix.repository }}

plugin:
Expand All @@ -75,6 +76,7 @@ jobs:
with:
category: plugin
ignore: images
comment: false
repository: ${{ matrix.repository }}

netdaemon:
Expand All @@ -95,6 +97,7 @@ jobs:
with:
category: netdaemon
ignore: topics
comment: false
repository: ${{ matrix.repository }}

appdaemon:
Expand All @@ -114,6 +117,7 @@ jobs:
with:
category: appdaemon
ignore: hacsjson
comment: false
repository: ${{ matrix.repository }}

python_script:
Expand All @@ -133,4 +137,5 @@ jobs:
with:
category: python_scripts
ignore: hacsjson information
comment: false
repository: ${{ matrix.repository }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _Repository validation action for HACS_
| -------- | ---------------------------------------------------------------------------------------- |
| ignore | A space seperated list of ignored checks |
| category | The type of repository (integration, plugin, theme, netdaemon, appdaemon, python_script) |
| comment | Post the results of the cheks to the PR (true, false) |

## Example

Expand Down
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
repository:
description: "The full name of repository"
required: false
comment:
description: "Post a comment to the PR with the result of the checks"
required: false
default: "true"
runs:
using: "composite"
steps:
Expand Down Expand Up @@ -74,6 +78,15 @@ runs:
- shell: bash
run: bash "${{ github.action_path }}/helpers/wrapup" "${{ github.action_path }}"

- shell: bash
id: comment
run: |
if [ "${{ inputs.comment }}" == "true" ]; then
python3 -m pip install -U wheel setuptools > /dev/null
python3 -m pip install -U aiogithubapi > /dev/null
python3 helpers/post_result.py
fi
- shell: bash
run: |
bash "${{ github.action_path }}/helpers/cleanup"
59 changes: 59 additions & 0 deletions helpers/post_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import asyncio
import os
import json
from aiogithubapi import GitHub
from aiogithubapi.common.const import BASE_API_HEADERS, BASE_API_URL

IDENTIFIER = "<!-- HACS action comment -->"
HEADER = "🎉 **HACS repository validator action summary** 🎉"


def get_token():
with open(f"{os.getenv('GITHUB_ACTION_PATH')}/data/token", "r") as token:
return token.read().replace("\n", "")


def get_event():
with open(os.getenv('GITHUB_EVENT_PATH'), "r") as event:
return event.read()


def get_result():
with open(f"{os.getenv('GITHUB_ACTION_PATH')}/result", "r") as result:
return result.read()


async def post():
event = json.loads(get_event())
if not event.get('pull_request'):
return

async with GitHub(get_token()) as github:
name = event["repository"]["full_name"]
number = event["pull_request"]["number"]
msg = f"{HEADER}\n{get_result()}\n\n{IDENTIFIER}"

_headers = BASE_API_HEADERS
_headers["Authorization"] = f"token {github.client.token}"
_endpoint = f"{BASE_API_URL}/repos/{name}/issues/{number}/comments"

request = await github.client.session.get(_endpoint, headers=_headers)
comments = await request.json()
for comment in comments:
if IDENTIFIER in comment["body"] and comment["user"]["login"] == 'github-actions[bot]':
_endpoint = f"{BASE_API_URL}/repos/{name}/issues/comments/{comment['id']}"
result = await github.client.session.patch(_endpoint, json={"body": msg}, headers=_headers)
if result.status != 200:
print(_endpoint)
print(result.reason)
exit(1)
return

result = await github.client.session.post(_endpoint, json={"body": msg}, headers=_headers)
if result.status != 201:
print(_endpoint)
print(result.reason)
exit(1)


asyncio.get_event_loop().run_until_complete(post())

0 comments on commit 1b5f154

Please sign in to comment.