Skip to content
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
19 changes: 16 additions & 3 deletions mergify_cli/ci/scopes/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import json
import os
import pathlib
import typing
import uuid

import click
import pydantic
Expand All @@ -17,6 +19,8 @@
if typing.TYPE_CHECKING:
from collections import abc

GITHUB_ACTIONS_OUTPUT_NAME = "scopes"


def match_scopes(
files: abc.Iterable[str],
Expand Down Expand Up @@ -56,10 +60,19 @@ def maybe_write_github_outputs(
gha = os.environ.get("GITHUB_OUTPUT")
if not gha:
return
delimiter = f"ghadelimiter_{uuid.uuid4()}"
with pathlib.Path(gha).open("a", encoding="utf-8") as fh:
for key in sorted(all_scopes):
val = "true" if key in scopes_hit else "false"
fh.write(f"{key}={val}\n")
# NOTE(sileht): Boolean in GitHub Workflow should be avoided.
# In GHA, an output is a string, so putting a bool in the JSON
# will be a bool when the JSON is parsed, but once copied into another output
# it's converted to the string "false|true". To avoid any confusion about whether it's
# a bool or a string, make it always a string.
data = {
key: "true" if key in scopes_hit else "false" for key in sorted(all_scopes)
}
fh.write(
f"{GITHUB_ACTIONS_OUTPUT_NAME}<<{delimiter}\n{json.dumps(data)}\n{delimiter}\n",
)


class InvalidDetectedScopeError(exceptions.ScopesError):
Expand Down
4 changes: 1 addition & 3 deletions mergify_cli/tests/ci/scopes/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ def test_maybe_write_github_outputs(
cli.maybe_write_github_outputs(all_scopes, scopes_hit)

content = output_file.read_text()
assert "backend=true\n" in content
assert "docs=true\n" in content
assert "frontend=false\n" in content
assert """{"backend": "true", "docs": "true", "frontend": "false"}""" in content


def test_maybe_write_github_outputs_no_env(
Expand Down