Skip to content

Commit

Permalink
fix: Fix post processing log output, preserve labels not controlled b…
Browse files Browse the repository at this point in the history
…y CI on PRs
  • Loading branch information
FHeilmann committed Dec 19, 2023
1 parent 5a36018 commit e978f26
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/post_process_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
permissions:
pull-requests: write
runs-on: ubuntu-latest
env:
VORON_TOOLKIT_VERBOSE: true
VORON_CI_GITHUB_TOKEN: ${{ github.token }}
steps:
- name: Create PR comment 💬
id: create_pr_comment
Expand All @@ -19,13 +22,14 @@ jobs:
PR_HELPER_WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
PR_HELPER_ARTIFACT_NAME: ci_output
PR_HELPER_GITHUB_REPOSITORY: ${{ github.repository }}
PR_HELPER_VERBOSE: true
VORON_CI_GITHUB_TOKEN: ${{ github.token }}
with:
args: set-pr-comment-labels

upload_imagekit:
if: ${{ github.event.workflow_run.event == 'pull_request' }}
env:
VORON_TOOLKIT_VERBOSE: true
VORON_CI_GITHUB_TOKEN: ${{ github.token }}
runs-on: ubuntu-latest
steps:
- name: Upload images to imagekit 📸
Expand All @@ -38,7 +42,5 @@ jobs:
IMAGEKIT_UPLOADER_PUBLIC_KEY: ${{ secrets.IMAGEKIT_PUBLIC_KEY }}
IMAGEKIT_UPLOADER_PRIVATE_KEY: ${{ secrets.IMAGEKIT_PRIVATE_KEY }}
IMAGEKIT_UPLOADER_IMAGEKIT_ENDPOINT: https://ik.imagekit.io/vorondesign
IMAGEKIT_UPLOADER_VERBOSE: true
VORON_CI_GITHUB_TOKEN: ${{ github.token }}
with:
args: upload-images
6 changes: 6 additions & 0 deletions voron_toolkit/utils/github_action_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def last_commit_timestamp(cls: type[Self], file_or_directory: Path) -> str:
logger.exception("An error occurred while querying last_changed timestamp for '{}'", file_or_directory.as_posix())
return ""

@classmethod
def get_labels_on_pull_request(cls: type[Self], repo: str, pull_request_number: int) -> list[str]:
github = GitHub(os.environ[VORON_CI_GITHUB_TOKEN_ENV_VAR])
response: Response = github.rest.issues.list_labels_on_issue(owner=repo.split("/")[0], repo=repo.split("/")[1], issue_number=pull_request_number)
return [label["name"] for label in response.json()]

@classmethod
def set_labels_on_pull_request(cls: type[Self], repo: str, pull_request_number: int, labels: list[str]) -> None:
github = GitHub(os.environ[VORON_CI_GITHUB_TOKEN_ENV_VAR])
Expand Down
9 changes: 6 additions & 3 deletions voron_toolkit/utils/imagekit_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ def __init__(self: Self, args: configargparse.Namespace) -> None:
sys.exit(0)

def upload_image(self: Self, image_path: Path) -> bool:
logger.info("Uploading image '{}' ...", image_path.as_posix())
with Path(image_path).open(mode="rb") as image:
imagekit_options: UploadFileRequestOptions = self.imagekit_options_common
imagekit_options.folder = image_path.parent.relative_to(Path(self.image_base_path)).as_posix()
result: UploadFileResult = self.imagekit.upload_file(file=image, file_name=image_path.name, options=imagekit_options)
return result.url != ""
if result.url:
logger.success("Successfully uploaded image '{}' to '{}'", image_path.as_posix(), result.url)
return True
logger.error("Failed to upload image '{}'!", image_path.as_posix())
return False

def run(self: Self) -> None:
logger.info("Downloading artifact '{}' from workflow '{}'", self.artifact_name, self.workflow_run_id)
Expand All @@ -80,7 +83,7 @@ def run(self: Self) -> None:
logger.info("Processing Image files in '{}'", self.tmp_path.as_posix())

images: list[Path] = list(self.image_base_path.glob("**/*.png"))
logger.info("Found {} images", len(images))
logger.success("Found {} images", len(images))
if not images:
logger.warning("No images found in input_dir '{}'!", self.image_base_path.as_posix())
return
Expand Down
24 changes: 20 additions & 4 deletions voron_toolkit/utils/pr_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*Made with ❤️ by the VoronDesign GitHub Team*
"""

ALL_LABELS = [CI_ERROR_LABEL, CI_FAILURE_LABEL, CI_PASSED_LABEL]


class PrHelper:
def __init__(self: Self, args: configargparse.Namespace) -> None:
Expand Down Expand Up @@ -118,9 +120,15 @@ def _parse_artifact_and_get_labels(self: Self) -> set[str]:
result_ok = ExtendedResultEnum.WARNING if ci_step_result.tool_ignore_warnings else ExtendedResultEnum.SUCCESS
if ci_step_result.extended_result > result_ok:
labels.add(CI_FAILURE_LABEL)

logger.success(
"Parsed result for tool {}: Result: {}, Ignore Warnings: {}",
pr_step_identifier,
ci_step_result.extended_result,
ci_step_result.tool_ignore_warnings,
)
self.tool_results[pr_step_identifier] = ci_step_result
if not labels:
logger.success("All CI checks executed without errors!")
labels.add(CI_PASSED_LABEL)
logger.info("Labels: {}", labels)
return labels
Expand All @@ -145,17 +153,25 @@ def run(self: Self) -> None:
)

pr_number: int = self._get_pr_number()
labels: set[str] = self._parse_artifact_and_get_labels()
logger.info("Post Processing PR #{}", pr_number)
if pr_number > 0:
labels_to_set: set[str] = self._parse_artifact_and_get_labels()
labels_on_pr: list[str] = GithubActionHelper.get_labels_on_pull_request(
repo=self.github_repository,
pull_request_number=pr_number,
)
labels_to_preserve: list[str] = [label for label in labels_on_pr if label not in ALL_LABELS]

GithubActionHelper.set_labels_on_pull_request(
repo=self.github_repository,
pull_request_number=pr_number,
labels=list(labels),
labels=list(*labels_to_set, *labels_to_preserve),
)
pr_comment: str = self._generate_pr_comment()
GithubActionHelper.update_or_create_pr_comment(
repo=self.github_repository,
pull_request_number=pr_number,
comment_body=self._generate_pr_comment(),
comment_body=pr_comment,
)


Expand Down

0 comments on commit e978f26

Please sign in to comment.