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..af6001a --- /dev/null +++ b/.github/workflows/flake8-your-pr.yml @@ -0,0 +1,15 @@ +name: Flake8 your PR +on: [pull_request] +jobs: + flake8-your-pr: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: "${{ github.head_ref }}" + fetch-depth: 2 + + # - uses: tayfun/flake8-your-pr@master + - uses: valentijnscholten/flake8-your-pr@master + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" 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 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..2a8fc0c 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -15,6 +15,22 @@ 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 + +echo "git status" +git status + +echo "git diff" +git diff + +echo "event payload:" +cat $GITHUB_EVENT_PATH + find_base_commit() { BASE_COMMIT=$( jq \ @@ -31,6 +47,17 @@ find_base_commit() { "$GITHUB_EVENT_PATH" ) fi + echo "BASE_COMMIT: $BASE_COMMIT" +} + +find_head_commit() { + HEAD_COMMIT=$( + jq \ + --raw-output \ + .pull_request.head.sha \ + "$GITHUB_EVENT_PATH" + ) + echo "HEAD_COMMIT: $HEAD_COMMIT" } ACTION=$( @@ -45,19 +72,39 @@ 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. + + # 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 \ --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 python files in PR" + fi + # flake8 --format=json . | jq '.' > flake8_output.json || true # NOQA 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