Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only run when python files changed, update docs #8

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .github/main.workflow

This file was deleted.

15 changes: 15 additions & 0 deletions .github/workflows/flake8-your-pr.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 50 additions & 3 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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=$(
Expand All @@ -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
}

Expand Down
20 changes: 12 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import os
import requests
from datetime import datetime, timezone
from datetime import datetime
import pytz


class CheckRun:
Expand All @@ -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

Expand All @@ -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'])
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -125,4 +129,4 @@ def create(self):

if __name__ == '__main__':
check_run = CheckRun()
check_run.create()
check_run.create()