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

Configure pre-commit #24

Merged
merged 2 commits into from
May 5, 2023
Merged
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
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[flake8]
max-line-length = 88
max-complexity = 21
# B = bugbear
# B9 = bugbear opinionated (incl line length)
select = C,E,F,W,B,B9
# E203: whitespace before ':' (black behaviour)
# E501: flake8 line length (covered by bugbear B950)
# W503: line break before binary operator (black behaviour)
ignore = E203,E501,W503
per-file-ignores=
__init__.py:F401
34 changes: 34 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: pre-commit

on:
pull_request:
branches:
- "main"
push:
branches:
- "main"

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Get python version
run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- name: Install pre-commit
run: pip install pre-commit
- name: Run pre-commit
run: pre-commit run --all-files --show-diff-on-failure --color=always
- name: Check that all files generated by pre-commit are in git
run: |
newfiles="$(git ls-files --others --exclude-from=.gitignore)"
if [ "$newfiles" != "" ] ; then
echo "Please check-in the following files:"
echo "$newfiles"
exit 1
fi
11 changes: 11 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[settings]
; see https://github.com/psf/black
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
combine_as_imports=True
use_parentheses=True
line_length=88
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
default_section=THIRDPARTY
ensure_newline_before_comments = True
55 changes: 55 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
exclude: |
# Maybe reactivate this when all README files include prettier ignore tags?
^README\.md$|
# Repos using Sphinx to generate docs don't need prettying
^docs/_templates/.*\.html$|
# You don't usually want a bot to modify your legal texts
(LICENSE.*|COPYING.*)
default_language_version:
python: python3
repos:
- repo: https://github.com/myint/autoflake
rev: v1.6.1
hooks:
- id: autoflake
args:
- --expand-star-imports
- --ignore-init-module-imports
- --in-place
- --remove-all-unused-imports
- --remove-duplicate-keys
- --remove-unused-variables
- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: debug-statements
- id: fix-encoding-pragma
args: ["--remove"]
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: mixed-line-ending
args: ["--fix=lf"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.2
hooks:
- id: pyupgrade
args: ["--keep-percent-format"]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
exclude: /__init__\.py$
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
name: flake8
additional_dependencies: ["flake8-bugbear==21.9.2"]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Pre-commit Status](https://github.com/OCA/oca-port/actions/workflows/pre-commit.yml/badge.svg?branch=main)](https://github.com/OCA/oca-port/actions/workflows/pre-commit.yml?query=branch%3Amain)

oca-port
========

Expand All @@ -12,7 +14,7 @@ Installing
$ git clone git@github.com:oca/oca-port.git
$ cd oca-port
$ pipx install .

Using
-----

Expand Down
101 changes: 69 additions & 32 deletions oca_port/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,67 @@
import git

from . import utils
from .utils.misc import bcolors as bc
from .migrate_addon import MigrateAddon
from .port_addon_pr import PortAddonPullRequest
from .utils.misc import bcolors as bc


@click.command()
@click.argument("from_branch", required=True)
@click.argument("to_branch", required=True)
@click.argument("addon", required=True)
@click.option("--upstream-org", default="OCA", show_default=True,
help="Upstream organization name.")
@click.option("--upstream", default="origin", show_default=True, required=True,
help="Git remote from which source and target branches are fetched by default.")
@click.option(
"--upstream-org",
default="OCA",
show_default=True,
help="Upstream organization name.",
)
@click.option(
"--upstream",
default="origin",
show_default=True,
required=True,
help="Git remote from which source and target branches are fetched by default.",
)
@click.option("--repo-name", help="Repository name, eg. server-tools.")
@click.option("--fork",
help="Git remote where branches with ported commits are pushed.")
@click.option(
"--fork", help="Git remote where branches with ported commits are pushed."
)
@click.option("--user-org", show_default="--fork", help="User organization name.")
@click.option("--verbose", is_flag=True,
help="List the commits of Pull Requests.")
@click.option("--non-interactive", is_flag=True,
help="Disable all interactive prompts.")
@click.option("--verbose", is_flag=True, help="List the commits of Pull Requests.")
@click.option(
"--non-interactive", is_flag=True, help="Disable all interactive prompts."
)
@click.option("--no-cache", is_flag=True, help="Disable user's cache.")
@click.option("--clear-cache", is_flag=True, help="Clear the user's cache.")
def main(
from_branch, to_branch, addon, upstream_org, upstream, repo_name,
fork, user_org, verbose, non_interactive, no_cache, clear_cache
):
from_branch,
to_branch,
addon,
upstream_org,
upstream,
repo_name,
fork,
user_org,
verbose,
non_interactive,
no_cache,
clear_cache,
):
"""Migrate ADDON from FROM_BRANCH to TO_BRANCH or list Pull Requests to port
if ADDON already exists on TO_BRANCH.
if ADDON already exists on TO_BRANCH.

Migration:
Migration:

Assist the user in the migration of the addon, following the OCA guidelines.
Assist the user in the migration of the addon, following the OCA guidelines.

Port of Pull Requests (missing commits):
Port of Pull Requests (missing commits):

The PRs are found from FROM_BRANCH commits that do not exist in TO_BRANCH.
The user will be asked if he wants to port them.
The PRs are found from FROM_BRANCH commits that do not exist in TO_BRANCH.
The user will be asked if he wants to port them.

To start the migration process, the `--fork` option must be provided in
order to push the resulting branch on the user's remote.
To start the migration process, the `--fork` option must be provided in
order to push the resulting branch on the user's remote.
"""
repo = git.Repo()
if repo.is_dirty(untracked_files=True):
Expand Down Expand Up @@ -118,14 +138,34 @@ def main(
# - if it already exists, check if some PRs could be ported
if _check_addon_exists(addon, to_branch):
PortAddonPullRequest(
repo, upstream_org, repo_name, from_branch, to_branch,
fork, user_org, addon, storage, cache, verbose, non_interactive
repo,
upstream_org,
repo_name,
from_branch,
to_branch,
fork,
user_org,
addon,
storage,
cache,
verbose,
non_interactive,
).run()
# - if not, migrate it
else:
MigrateAddon(
repo, upstream_org, repo_name, from_branch, to_branch,
fork, user_org, addon, storage, cache, verbose, non_interactive
repo,
upstream_org,
repo_name,
from_branch,
to_branch,
fork,
user_org,
addon,
storage,
cache,
verbose,
non_interactive,
).run()
if clear_cache:
cache.clear()
Expand Down Expand Up @@ -157,9 +197,7 @@ def _fetch_branches(*branches, verbose=False):
continue
remote_url = branch.repo.remotes[branch.remote].url
if verbose:
print(
f"Fetch {bc.BOLD}{branch.ref()}{bc.END} from {remote_url}"
)
print(f"Fetch {bc.BOLD}{branch.ref()}{bc.END} from {remote_url}")
branch.repo.remotes[branch.remote].fetch(branch.name)


Expand All @@ -168,8 +206,7 @@ def _check_branches(from_branch, to_branch):
# Check if the source branch exists (required)
if not from_branch.remote:
raise click.ClickException(
"No source branch "
f"{bc.BOLD}{from_branch.ref()}{bc.END} available."
f"No source branch {bc.BOLD}{from_branch.ref()}{bc.END} available."
)
# Check if the target branch exists (with or w/o remote, allowing to work
# on a local one)
Expand All @@ -193,5 +230,5 @@ def _check_addon_exists(addon, branch, raise_exc=False):
return True


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading