Skip to content

Commit

Permalink
Allow passing a base commit as an argument to the action (#3)
Browse files Browse the repository at this point in the history
* Allow passing the base commit as an input

* os.exit -> sys.exit

* list.expand -> list.extend

* Add log for git diff

* Remove newlines from git stdout
  • Loading branch information
mathispesch committed Oct 28, 2022
1 parent da627bb commit 4aedfc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ inputs:
Turn on if you only want to check changed files instead of all files.
required: false
default: "false"
base_commit:
description: |
Sha of the commit to compare files to.
required: false
default: ""
outputs:
is_formatted:
description: "Whether the files were formatted using the black formatter."
Expand Down
24 changes: 14 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"print_diff_mode",
"main_branch",
"only_changed_files",
"base_commit",
"additional_args",
],
)
Expand All @@ -28,7 +29,7 @@ def get_head_commit():
)
if process.returncode != 0:
raise Exception(f"unexpected non-zero return code from git: {repr(process)}")
return process.stdout
return process.stdout.replace("\n", "")


def get_merge_base(main_branch, head_commit):
Expand All @@ -39,14 +40,16 @@ def get_merge_base(main_branch, head_commit):
universal_newlines=True,
shell=True,
)
return process.stdout
return process.stdout.replace("\n", "")


def get_changed_files(main_branch):
def get_changed_files(main_branch, base_commit):
head_commit = get_head_commit()
merge_base = get_merge_base(main_branch, head_commit)
if not base_commit:
base_commit = get_merge_base(main_branch, head_commit)
print(f"[action-black] Formatting files between {base_commit} and {head_commit}")
process = subprocess.run(
f"git diff --diff-filter=d --name-only {merge_base}..{head_commit}",
f"git diff --diff-filter=d --name-only {base_commit}..{head_commit}",
stdout=subprocess.PIPE,
stderr=sys.stderr.buffer,
universal_newlines=True,
Expand Down Expand Up @@ -113,11 +116,11 @@ def main(config):
action_msg_mode += " (all files)"

if config.additional_args:
invocation_args.expand(config.additional_args.split(" "))
invocation_args.extend(config.additional_args.split(" "))

print(f"[action-black] {action_msg_mode} python code using the black formatter...")
if config.only_changed_files:
changed_python_files = get_changed_files(config.main_branch)
changed_python_files = get_changed_files(config.main_branch, config.base_commit)
retcode, stdout = invoke_black_on_changed_files(
invocation_args, changed_python_files
)
Expand All @@ -144,7 +147,7 @@ def main(config):
print(
f"[action-black] ERROR: (non-formatting) Something went wrong while trying to run the black formatter (error code: {retcode})."
)
os.exit(1)
sys.exit(1)
else:
# Check if black formatted files
matcher = re.compile(r"\s?[0-9]+\sfiles?\sreformatted(\.|,)\s?")
Expand All @@ -167,10 +170,10 @@ def main(config):
print(
f"[action-black] ERROR: (formatting) Something went wrong while trying to run the black formatter (error code: {retcode})."
)
os.exit(1)
sys.exit(1)

if config.fail_on_error and is_error:
os.exit(1)
sys.exit(1)


def env_bool(variable_name, default_value):
Expand All @@ -191,6 +194,7 @@ def env(variable_name, default_value):
print_diff_mode=env_bool("print_diff_mode", True),
main_branch=env("main_branch", "main"),
only_changed_files=env_bool("only_changed_files", False),
base_commit=env("base_commit", ""),
additional_args=env("additional_args", ""),
)
print(f"[action-black] configuration: {config}")
Expand Down

0 comments on commit 4aedfc9

Please sign in to comment.