Skip to content

Commit

Permalink
blacklist: run pre-commit before committing
Browse files Browse the repository at this point in the history
  • Loading branch information
sebalix committed Oct 11, 2022
1 parent 596f2ab commit 118fd27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
18 changes: 1 addition & 17 deletions oca_port/migrate_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import click
import os
import subprocess
import tempfile
import urllib.parse

Expand Down Expand Up @@ -115,7 +114,7 @@ def run(self):
with tempfile.TemporaryDirectory() as patches_dir:
self._generate_patches(patches_dir)
self._apply_patches(patches_dir)
self._run_pre_commit()
misc.run_pre_commit(self.repo, self.addon)
# Check if the addon has commits that update neighboring addons to
# make it work properly
PortAddonPullRequest(
Expand Down Expand Up @@ -177,21 +176,6 @@ def _apply_patches(self, patches_dir):
f"has been migrated."
)

def _run_pre_commit(self):
# Run pre-commit
print(
f"\tRun {bc.BOLD}pre-commit{bc.END} and commit changes if any..."
)
# First ensure that 'pre-commit' is initialized for the repository,
# then run it (without checking the return code on purpose)
subprocess.check_call("pre-commit install", shell=True)
subprocess.run("pre-commit run -a", shell=True)
if self.repo.untracked_files or self.repo.is_dirty():
self.repo.git.add("-A")
self.repo.git.commit(
"-m", f"[IMP] {self.addon}: black, isort, prettier", "--no-verify"
)

def _print_tips(self, blacklisted=False):
mig_tasks_url = MIG_TASKS_URL.format(branch=self.to_branch.name)
pr_title_encoded = urllib.parse.quote(
Expand Down
26 changes: 23 additions & 3 deletions oca_port/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import re
import os
import subprocess

import click
import git
Expand Down Expand Up @@ -358,9 +359,8 @@ def commit(self):
# Commit all changes under ./.oca-port
self.repo.index.add(self.storage_dirname)
if self.repo.is_dirty():
self.repo.index.commit(
f".oca-port: store '{self.addon}' data", skip_hooks=True
)
run_pre_commit(self.repo, self.addon, commit=False, hook="prettier")
self.repo.index.commit(f"oca-port: store '{self.addon}' data")
self.dirty = False


Expand All @@ -385,3 +385,23 @@ def _request_github(url, method="get", params=None, json=None):
if not response.ok:
raise RuntimeError(response.text)
return response.json()


def run_pre_commit(repo, addon, commit=True, hook=None):
# Run pre-commit
print(
f"\tRun {bcolors.BOLD}pre-commit{bcolors.END} and commit changes if any..."
)
# First ensure that 'pre-commit' is initialized for the repository,
# then run it (without checking the return code on purpose)
subprocess.check_call("pre-commit install", shell=True)
if hook:
subprocess.run(f"pre-commit run {hook}", shell=True)
else:
subprocess.run("pre-commit run -a", shell=True)
if repo.untracked_files or repo.is_dirty():
repo.git.add("-A")
if commit:
repo.git.commit(
"-m", f"[IMP] {addon}: black, isort, prettier", "--no-verify"
)

0 comments on commit 118fd27

Please sign in to comment.