From 74a22cbb68ddde85766175b05ce8a969bf2e7321 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Sat, 26 Sep 2020 20:14:57 +0200 Subject: [PATCH 01/16] only run when python files changed, update docs --- .github/main.workflow | 10 ---------- .github/workflows/flake8-your-pr.yml | 14 ++++++++++++++ README.md | 23 ++++++++++++++--------- src/entrypoint.sh | 20 +++++++++++++++++--- 4 files changed, 45 insertions(+), 22 deletions(-) delete mode 100644 .github/main.workflow create mode 100644 .github/workflows/flake8-your-pr.yml diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index 9c7154c..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,10 +0,0 @@ -workflow 'on pull request update, run flake8 and post results' { - on = 'pull_request' - resolves = 'run flake8' -} - -action 'run flake8' { - uses = 'tayfun/flake8-your-pr@master' - secrets = ["GITHUB_TOKEN"] -} - diff --git a/.github/workflows/flake8-your-pr.yml b/.github/workflows/flake8-your-pr.yml new file mode 100644 index 0000000..6a27947 --- /dev/null +++ b/.github/workflows/flake8-your-pr.yml @@ -0,0 +1,14 @@ +name: Flake8 your PR +on: [pull_request] +jobs: + flake8-your-pr: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + + # - uses: tayfun/flake8-your-pr@master + - uses: valentijnscholten/flake8-your-pr@master + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" diff --git a/README.md b/README.md index 0621cac..3b9a178 100644 --- a/README.md +++ b/README.md @@ -22,15 +22,20 @@ This is where Github Actions comes along. Github basically runs a docker image o Easy, tiger. Sign up for beta on [Github](https://github.com/features/actions). And then simply add the following code in your repo root `.github/main.workflow`: ``` -workflow "on check suite creation, run flake8 and post results" { - on = "pull_request" - resolves = "run flake8" -} - -action "run flake8" { - uses = "tayfun/flake8-your-pr@master" - secrets = ["GITHUB_TOKEN"] -} +name: Flake8 your PR +on: [pull_request] +jobs: + flake8-your-pr: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + + - uses: valentijnscholten/flake8-your-pr@master + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + ``` Create a pull request with some Python code and voila, you should see any errors as annotations. diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 196d9bd..f7f21ee 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -52,12 +52,26 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' + "$BASE_COMMIT" ) - echo "New files in branch: $new_files_in_branch" + new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') + + echo "New files in PR: $new_files_in_branch1" # Feed to flake8 which will return the output in json format. # shellcheck disable=SC2086 - flake8 --format=json $new_files_in_branch | jq '.' > flake8_output.json || true # NOQA + # only run flake8 if there are python files changed + if [[ $new_files_in_branch =~ .*".py".* ]]; then + new_python_files_in_branch=$( + git diff \ + --name-only \ + --diff-filter=AM \ + "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' + ) + echo "New python files in PR: $new_python_files_in_branch" + flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA + else + echo "No new pythong files in PR" + fi python /src/main.py } From 78153600cefda7fd4ff1f66001b2f34fac26524a Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Sat, 26 Sep 2020 21:06:59 +0200 Subject: [PATCH 02/16] fix for no python files in PR --- src/entrypoint.sh | 2 +- src/main.py | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index f7f21ee..1ae95ba 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -70,7 +70,7 @@ main() { echo "New python files in PR: $new_python_files_in_branch" flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA else - echo "No new pythong files in PR" + echo "No new python files in PR" fi python /src/main.py } diff --git a/src/main.py b/src/main.py index f9d0d35..d2833d3 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,8 @@ import json import os import requests -from datetime import datetime, timezone +from datetime import datetime +import pytz class CheckRun: @@ -11,8 +12,8 @@ class CheckRun: URI = 'https://api.github.com' # We need preview version to access check run API API_VERSION = 'antiope-preview' - ACCEPT_HEADER_VALUE = f"application/vnd.github.{API_VERSION}+json" - AUTH_HEADER_VALUE = f"token {GITHUB_TOKEN}" + ACCEPT_HEADER_VALUE = "application/vnd.github.{}+json".format(API_VERSION) + AUTH_HEADER_VALUE = "token {}".format(GITHUB_TOKEN) # This is the max annotations Github API accepts in one go. MAX_ANNOTATIONS = 50 @@ -37,8 +38,11 @@ def read_meta_data(self): self.head_sha = check_suite['pull_requests'][0]['base']['sha'] def read_flake8_output(self): - with open('flake8_output.json') as flake8_output_file: - self.flake8_output = json.loads(flake8_output_file.read()) + if os.path.exists('flake8_output.json'): + with open('flake8_output.json') as flake8_output_file: + self.flake8_output = json.loads(flake8_output_file.read()) + else: + self.flake8_output = {} def create_single_annotation(self, error, file_path): message = '{} ({})'.format(error['text'], error['code']) @@ -97,7 +101,7 @@ def get_payload(self): 'head_sha': self.head_sha, 'status': 'completed', 'conclusion': conclusion, - 'completed_at': datetime.now(timezone.utc).isoformat(), + 'completed_at': datetime.now(pytz.utc).isoformat(), 'output': { 'title': 'Flake8 Result', 'summary': summary, @@ -112,7 +116,7 @@ def create(self): payload = self.get_payload() print(payload) response = requests.post( - f'{self.URI}/repos/{self.repo_full_name}/check-runs', + '{}/repos/{}/check-runs'.format(self.URI, self.repo_full_name), headers={ 'Accept': self.ACCEPT_HEADER_VALUE, 'Authorization': self.AUTH_HEADER_VALUE, @@ -125,4 +129,4 @@ def create(self): if __name__ == '__main__': check_run = CheckRun() - check_run.create() + check_run.create() \ No newline at end of file From bd8bab9547a6ea4d012f6894e5dbc7fa2fb1f031 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Sat, 26 Sep 2020 21:08:31 +0200 Subject: [PATCH 03/16] fix for no python files in PR --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 274c917..f34db26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ LABEL "com.github.actions.maintainer"="Tayfun Sen" # RUN apk add --no-cache build-base gcc RUN apk add --no-cache git bash jq curl RUN pip install --upgrade pip -RUN pip install flake8 flake8-json requests +RUN pip install flake8 flake8-json requests pytz RUN python --version; pip --version; flake8 --version COPY src /src From 103917c7261362796a4aa0c33c28a6f9e12ac06e Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 19:09:01 +0200 Subject: [PATCH 04/16] testing --- src/entrypoint.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 1ae95ba..da8cb65 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -15,6 +15,9 @@ if [[ -z "$GITHUB_TOKEN" ]]; then exit 1 fi +echo "event payload:" +cat $GITHUB_EVENT_PATH + find_base_commit() { BASE_COMMIT=$( jq \ From 8c6e7d1366764664f428608c917092747afb1e4f Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 19:40:47 +0200 Subject: [PATCH 05/16] testing --- src/entrypoint.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index da8cb65..ee2f3a0 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -15,6 +15,9 @@ if [[ -z "$GITHUB_TOKEN" ]]; then exit 1 fi +echo "current commit" +git log -1 + echo "event payload:" cat $GITHUB_EVENT_PATH From eb8a118558fbc40708917d787a46c7428a2add05 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 20:02:12 +0200 Subject: [PATCH 06/16] testing --- .github/workflows/flake8-your-pr.yml | 1 + src/entrypoint.sh | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/flake8-your-pr.yml b/.github/workflows/flake8-your-pr.yml index 6a27947..af6001a 100644 --- a/.github/workflows/flake8-your-pr.yml +++ b/.github/workflows/flake8-your-pr.yml @@ -6,6 +6,7 @@ jobs: steps: - uses: actions/checkout@v2 with: + ref: "${{ github.head_ref }}" fetch-depth: 2 # - uses: tayfun/flake8-your-pr@master diff --git a/src/entrypoint.sh b/src/entrypoint.sh index ee2f3a0..d5595e1 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -39,6 +39,15 @@ find_base_commit() { fi } +find_head_commit() { + HEAD_COMMIT=$( + jq \ + --raw-output \ + .pull_request.head.sha \ + "$GITHUB_EVENT_PATH" + ) +} + ACTION=$( jq --raw-output .action "$GITHUB_EVENT_PATH" ) @@ -58,7 +67,7 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT" + "$BASE_COMMIT" "$HEAD_COMMIT" ) new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') @@ -71,7 +80,7 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' + "$BASE_COMMIT" "$HEAD_COMMIT" | grep '\.py$' | tr '\n' ' ' ) echo "New python files in PR: $new_python_files_in_branch" flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA From 0668bca3d291a685fd264498aad245a36e605d8b Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 20:04:11 +0200 Subject: [PATCH 07/16] tests --- src/entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index d5595e1..c21b76e 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -61,6 +61,7 @@ main() { exit fi find_base_commit + find_head_commit # Get files Added or Modified wrt base commit, filter for Python, # replace new lines with space. new_files_in_branch=$( From 826388f54a3daab76f670790573db92b061e199f Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 20:07:12 +0200 Subject: [PATCH 08/16] tests --- src/entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index c21b76e..6e2d2ef 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -81,7 +81,7 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT" "$HEAD_COMMIT" | grep '\.py$' | tr '\n' ' ' + "$BASE_COMMIT".."$HEAD_COMMIT" | grep '\.py$' | tr '\n' ' ' ) echo "New python files in PR: $new_python_files_in_branch" flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA From 993f37f03eb0bd3aebe1c803e7c1a0775808d99e Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 20:19:57 +0200 Subject: [PATCH 09/16] tests --- src/entrypoint.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 6e2d2ef..ebcbcb4 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -18,6 +18,12 @@ fi echo "current commit" git log -1 +echo "git status" +git status + +echo "git diff" +git diff + echo "event payload:" cat $GITHUB_EVENT_PATH From 290d38a971a9967ee1a33f07f559e62d0bf90edb Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 23:01:42 +0200 Subject: [PATCH 10/16] tests --- src/entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index ebcbcb4..b889cd3 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -74,7 +74,7 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT" "$HEAD_COMMIT" + "$BASE_COMMIT" ) new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') @@ -87,7 +87,7 @@ main() { git diff \ --name-only \ --diff-filter=AM \ - "$BASE_COMMIT".."$HEAD_COMMIT" | grep '\.py$' | tr '\n' ' ' + "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' ) echo "New python files in PR: $new_python_files_in_branch" flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA From 54d1ed33b534194fca3306c3f5fc5c0cca0e3926 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 2 Oct 2020 23:07:20 +0200 Subject: [PATCH 11/16] tests --- src/entrypoint.sh | 56 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index b889cd3..200aed6 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -66,34 +66,36 @@ main() { echo -e "Not interested in this event: $ACTION.\nExiting..." exit fi - find_base_commit - find_head_commit - # Get files Added or Modified wrt base commit, filter for Python, - # replace new lines with space. - new_files_in_branch=$( - git diff \ - --name-only \ - --diff-filter=AM \ - "$BASE_COMMIT" - ) - new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') - echo "New files in PR: $new_files_in_branch1" - # Feed to flake8 which will return the output in json format. - # shellcheck disable=SC2086 - # only run flake8 if there are python files changed - if [[ $new_files_in_branch =~ .*".py".* ]]; then - new_python_files_in_branch=$( - git diff \ - --name-only \ - --diff-filter=AM \ - "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' - ) - echo "New python files in PR: $new_python_files_in_branch" - flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA - else - echo "No new python files in PR" - fi + # find_base_commit + # find_head_commit + # # Get files Added or Modified wrt base commit, filter for Python, + # # replace new lines with space. + # new_files_in_branch=$( + # git diff \ + # --name-only \ + # --diff-filter=AM \ + # "$BASE_COMMIT" + # ) + # new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') + + # echo "New files in PR: $new_files_in_branch1" + # # Feed to flake8 which will return the output in json format. + # # shellcheck disable=SC2086 + # # only run flake8 if there are python files changed + # if [[ $new_files_in_branch =~ .*".py".* ]]; then + # new_python_files_in_branch=$( + # git diff \ + # --name-only \ + # --diff-filter=AM \ + # "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' + # ) + # echo "New python files in PR: $new_python_files_in_branch" + # flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA + # else + # echo "No new python files in PR" + # fi + flake8 --format=json . | jq '.' > flake8_output.json || true # NOQA python /src/main.py } From 38a63409db4def102243b53ebf16b91ffc1f1960 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Sat, 3 Oct 2020 15:09:10 +0200 Subject: [PATCH 12/16] tests --- src/entrypoint.sh | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 200aed6..c23c3c9 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -67,35 +67,35 @@ main() { exit fi - # find_base_commit - # find_head_commit - # # Get files Added or Modified wrt base commit, filter for Python, - # # replace new lines with space. - # new_files_in_branch=$( - # git diff \ - # --name-only \ - # --diff-filter=AM \ - # "$BASE_COMMIT" - # ) - # new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') + find_base_commit + find_head_commit + # Get files Added or Modified wrt base commit, filter for Python, + # replace new lines with space. + new_files_in_branch=$( + git diff \ + --name-only \ + --diff-filter=AM \ + "$BASE_COMMIT" + ) + new_files_in_branch1=$(echo $new_files_in_branch | tr '\n' ' ') - # echo "New files in PR: $new_files_in_branch1" - # # Feed to flake8 which will return the output in json format. - # # shellcheck disable=SC2086 - # # only run flake8 if there are python files changed - # if [[ $new_files_in_branch =~ .*".py".* ]]; then - # new_python_files_in_branch=$( - # git diff \ - # --name-only \ - # --diff-filter=AM \ - # "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' - # ) - # echo "New python files in PR: $new_python_files_in_branch" - # flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA - # else - # echo "No new python files in PR" - # fi - flake8 --format=json . | jq '.' > flake8_output.json || true # NOQA + echo "New files in PR: $new_files_in_branch1" + # Feed to flake8 which will return the output in json format. + # shellcheck disable=SC2086 + # only run flake8 if there are python files changed + if [[ $new_files_in_branch =~ .*".py".* ]]; then + new_python_files_in_branch=$( + git diff \ + --name-only \ + --diff-filter=AM \ + "$BASE_COMMIT" | grep '\.py$' | tr '\n' ' ' + ) + echo "New python files in PR: $new_python_files_in_branch" + flake8 --format=json $new_python_files_in_branch | jq '.' > flake8_output.json || true # NOQA + else + echo "No new python files in PR" + fi + # flake8 --format=json . | jq '.' > flake8_output.json || true # NOQA python /src/main.py } From b0abfa1046f4bcfa8eb1884bfc3e7d060cabf7ee Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 16 Oct 2020 19:03:08 +0200 Subject: [PATCH 13/16] testing github actions --- src/entrypoint.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index c23c3c9..e05308b 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -43,6 +43,7 @@ find_base_commit() { "$GITHUB_EVENT_PATH" ) fi + echo "BASE_COMMIT: $BASE_COMMIT" } find_head_commit() { @@ -52,6 +53,7 @@ find_head_commit() { .pull_request.head.sha \ "$GITHUB_EVENT_PATH" ) + echo "HEAD_COMMIT: $HEAD_COMMIT" } ACTION=$( From d51a2ae9514fd744f32a1c4b6f9aae2d8d888e82 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 16 Oct 2020 19:44:41 +0200 Subject: [PATCH 14/16] testing github actions --- src/entrypoint.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index e05308b..2f32a39 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -70,9 +70,12 @@ main() { fi find_base_commit - find_head_commit + find_head_commit # Get files Added or Modified wrt base commit, filter for Python, # replace new lines with space. + + # currently in github actions the base commit is the original commit the PR was branched from + # we could try to rebase on top of the HEAD of dev to make sure it picks up the new code in dev new_files_in_branch=$( git diff \ --name-only \ From 067266b0e446518d192afb3bafaa443e04665d2f Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 16 Oct 2020 20:30:00 +0200 Subject: [PATCH 15/16] testing github actions --- src/entrypoint.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 2f32a39..a197ce8 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -15,6 +15,10 @@ if [[ -z "$GITHUB_TOKEN" ]]; then exit 1 fi +cat docker/entrypoint-integration-tests.sh +cat entrypoint-integration-tests.sh + + echo "current commit" git log -1 From d3a2405aaa7fe857209ed495b8bdcfb9cbe89296 Mon Sep 17 00:00:00 2001 From: Valentijn Scholten Date: Fri, 16 Oct 2020 21:17:00 +0200 Subject: [PATCH 16/16] testing github actions --- src/entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index a197ce8..2a8fc0c 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -15,8 +15,8 @@ if [[ -z "$GITHUB_TOKEN" ]]; then exit 1 fi -cat docker/entrypoint-integration-tests.sh -cat entrypoint-integration-tests.sh +# cat docker/entrypoint-integration-tests.sh +# cat entrypoint-integration-tests.sh echo "current commit"