Skip to content

Commit

Permalink
Add pre-commit support with black and linting with flake8 #77
Browse files Browse the repository at this point in the history
 - Add .flake8 and .pre-commit-config.yaml
 - Add GitHub actions for linting
 - Blackened tests
 - Add code style badge in README.md
 - Bump version: v0.4.9 -> 0.4.10
  • Loading branch information
astrochun committed May 5, 2022
1 parent a55df62 commit 161fb63
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 197 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
select = E,F,W
exclude = .git,.idea,.*egg-info,*data,build,dist,test*,venv
max-complexity = 10
23 changes: 23 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Linting

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run black
uses: psf/black@stable
with:
options: ". --check"
- name: Run flake8
if: ${{ github.event_name == 'pull_request' }}
# This uses a temporary fix by pinning markup_safe.
# See https://github.com/grantmcconnaughey/lintly-flake8-github-action/pull/16
uses: bniedzie/lintly-flake8-github-action@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
failIf: new
args: "--config=.flake8 ."
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-yaml
- id: check-shebang-scripts-are-executable
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.10
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
args:
- "--config=.flake8"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Retrieve statistics for a user's repositories and populate the information onto
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/astrochun/github-stats-pages/Python%20package?color=blue&label=build%20%28latest%29&logo=github)](https://github.com/astrochun/github-stats-pages/actions?query=workflow%3A%22Python+package%22)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/github-stats-pages)
[![PyPI](https://img.shields.io/pypi/v/github-stats-pages?color=blue)](https://pypi.org/project/github-stats-pages)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

![PyPI - Downloads](https://img.shields.io/pypi/dm/github-stats-pages?color=light%20green&label=PyPI-download)

Expand Down
9 changes: 5 additions & 4 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@

def pytest_addoption(parser):
parser.addoption("--username", action="store", default="GitHub username")
parser.addoption("--token", action="store",
default="GitHub API personal access token")
parser.addoption(
"--token", action="store", default="GitHub API personal access token"
)


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def username(request):
name_value = request.config.option.username
if name_value is None:
pytest.skip()
return name_value


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def token(request):
name_value = request.config.option.token
if name_value is None:
Expand Down
2 changes: 1 addition & 1 deletion github_stats_pages/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.9"
__version__ = "0.4.10"
16 changes: 8 additions & 8 deletions github_stats_pages/gts_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

def run_each_repo(username, token, reponame, save_csv=True):
if save_csv:
os.system(f'gts {username}:{token} {reponame}')
os.system(f"gts {username}:{token} {reponame}")
else:
os.system(f'gts {username}:{token} {reponame} no_csv')
os.system(f"gts {username}:{token} {reponame} no_csv")


def get_top_paths(username: str, token: str, reponame: str,
save_csv: bool = True):
def get_top_paths(
username: str, token: str, reponame: str, save_csv: bool = True
):

now = dt.now()

Expand All @@ -25,16 +26,16 @@ def get_top_paths(username: str, token: str, reponame: str,
if len(top_path_list) > 0:
result = [p.raw_data for p in top_path_list]
df = pd.DataFrame.from_records(result)
pandas_write_buffer(df, ['path', 'count', 'uniques'], reponame)
df.insert(loc=0, column='date', value=now.strftime('%Y-%m-%d'))
pandas_write_buffer(df, ["path", "count", "uniques"], reponame)
df.insert(loc=0, column="date", value=now.strftime("%Y-%m-%d"))

if save_csv:
outfile = f"{now.strftime('%Y-%m-%d-%Hh-%Mm')}-paths-stats.csv"
path = Path(outfile)
if not path.exists():
df.to_csv(path, index=False, header=True)
else:
df.to_csv(path, mode='a', index=False, header=False)
df.to_csv(path, mode="a", index=False, header=False)
else:
print(f"Empty top paths for {reponame}")

Expand All @@ -45,4 +46,3 @@ def pandas_write_buffer(df, columns, reponame):
print(f"> {reponame} - Top paths")
print(buffer.getvalue())
buffer.close()

30 changes: 24 additions & 6 deletions github_stats_pages/repo_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
import json
import pandas as pd

SHORTEN_COLUMNS = ['id', 'name', 'html_url', 'description', 'language',
'fork', 'archived', 'stargazers_count', 'watchers_count',
'has_issues', 'has_downloads', 'has_wiki', 'has_pages',
'forks_count', 'disabled', 'open_issues_count', 'license',
'forks', 'open_issues', 'watchers', 'default_branch']
SHORTEN_COLUMNS = [
"id",
"name",
"html_url",
"description",
"language",
"fork",
"archived",
"stargazers_count",
"watchers_count",
"has_issues",
"has_downloads",
"has_wiki",
"has_pages",
"forks_count",
"disabled",
"open_issues_count",
"license",
"forks",
"open_issues",
"watchers",
"default_branch",
]


def get_repo_list(user: str) -> Tuple[list, pd.DataFrame]:
Expand All @@ -22,7 +40,7 @@ def get_repo_list(user: str) -> Tuple[list, pd.DataFrame]:
print("get_repo_list - Retrieving repository list ...")

endpoint = f"https://api.github.com/users/{user}/repos"
params = {'per_page': 100}
params = {"per_page": 100}
response = requests.get(endpoint, params=params)
repository_list: List[Dict] = json.loads(response.content)

Expand Down
Loading

0 comments on commit 161fb63

Please sign in to comment.