From 7aef49d3800859bfe600ea09c91a0b9830dd565c Mon Sep 17 00:00:00 2001 From: Jakob Jensen Date: Tue, 4 Nov 2025 21:22:02 +0100 Subject: [PATCH] fix: handle body and comments as intented --- README.md | 22 +++++++++--------- src/main.sh | 23 +++++++------------ .../test-close-existing-issue-with-comment.sh | 5 ++-- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index ee2a6a6..02ba175 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,17 @@ Ideal for CI/CD workflows that need to: ## Inputs -| Name | Description | Default | Required | -| ----------- | --------------------------------------------------------------------------------- | -------------------------- | -------- | -| `token` | GitHub token (PAT or `${{ github.token }}`) used for authentication | `${{ github.token }}` | Yes | -| `mode` | Operation mode: `create` or `close` | `create` | Yes | -| `state` | Issue state filter when searching for existing issues: `open`, `closed`, or `all` | `open` | Yes | -| `title` | Title of the issue to create or update | — | Yes | -| `labels` | Comma-separated list of labels used for creation and search | — | No | -| `assignees` | Comma-separated list of users to assign the issue to | — | No | -| `body` | Custom body text for the issue (overrides `template`) | — | No | -| `comment` | Optional comment text to add to an existing issue | — | No | -| `repo` | Repository to operate on (`owner/repo`) | `${{ github.repository }}` | Yes | +| Name | Description | Default | Required | +| ----------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | -------- | +| `token` | GitHub token (or PAT) used for authentication | `${{github.token}}` | Yes | +| `mode` | Operation mode: `create` or `close` | `create` | Yes | +| `state` | Issue state filter when searching for existing issues: `open`, `closed`, or `all` | `open` | Yes | +| `title` | Title of the issue to create or update | - | Yes | +| `labels` | Comma-separated list of labels used for creation and search | - | No | +| `assignees` | Comma-separated list of users to assign the issue to | - | No | +| `body` | Optional body text for the issue | - | No | +| `comment` | Optional comment text to add to an existing issue instead of updating the issue body, or as closing comment if closing the issue | - | No | +| `repo` | Repository to operate on (`owner/repo`) | `${{github.repository}}` | Yes | ## Example Usage diff --git a/src/main.sh b/src/main.sh index 29a3666..f897de7 100644 --- a/src/main.sh +++ b/src/main.sh @@ -29,28 +29,20 @@ echo '::endgroup::' ISSUE_NUMBER=$(gh issue list --repo "${REPO}" --state "${STATE}" --label "${LABELS}" --search "in:title \"${TITLE}\"" --limit 1 --json number --jq '.[].number' || true) -# Perform action based on mode case "${MODE}" in create) - # Determine body args - if [ -n "${BODY}" ]; then - BODY_ARGS="\"${BODY}\"" - else - BODY_ARGS="\"Auto-generated issue with title: ${TITLE}\"" - fi - if [ -n "${ISSUE_NUMBER}" ]; then echo "Issue already exists (#${ISSUE_NUMBER}), updating instead." if [ -n "${COMMENT}" ]; then echo "Adding comment to existing issue..." - gh issue comment "${ISSUE_NUMBER}" --repo "${REPO}" --body "${COMMENT}" + gh issue comment "${ISSUE_NUMBER}" --repo "${REPO}" --body "${COMMENT}" --edit-last --create-if-none --yes else echo "Updating issue body..." - gh issue edit "${ISSUE_NUMBER}" --repo "${REPO}" --title "${TITLE}" --body "${BODY_ARGS}" + gh issue edit "${ISSUE_NUMBER}" --repo "${REPO}" --title "${TITLE}" --body "${BODY}" fi else echo "Creating new issue..." - gh issue create --repo "${REPO}" --title "${TITLE}" --body "${BODY_ARGS}" --label "${LABELS}" --assignee "${ASSIGNEES}" + gh issue create --repo "${REPO}" --title "${TITLE}" --body "${BODY}" --label "${LABELS}" --assignee "${ASSIGNEES}" fi ;; close) @@ -59,11 +51,12 @@ close) exit 0 fi if [ -n "${COMMENT}" ]; then - echo "Adding closure comment..." - gh issue comment "${ISSUE_NUMBER}" --repo "${REPO}" --body "${COMMENT}" + echo "Closing issue #${ISSUE_NUMBER} with comment..." + gh issue close "${ISSUE_NUMBER}" --repo "${REPO}" --comment "${COMMENT}" + else + echo "Closing issue #${ISSUE_NUMBER}..." + gh issue close "${ISSUE_NUMBER}" --repo "${REPO}" fi - echo "Closing issue #${ISSUE_NUMBER}..." - gh issue close "${ISSUE_NUMBER}" --repo "${REPO}" ;; *) echo "Invalid mode '${MODE}'. Valid options: create | close" diff --git a/tests/test-close-existing-issue-with-comment.sh b/tests/test-close-existing-issue-with-comment.sh index 03f5c65..c6d5bf3 100644 --- a/tests/test-close-existing-issue-with-comment.sh +++ b/tests/test-close-existing-issue-with-comment.sh @@ -9,8 +9,7 @@ export INPUT_MODE="close" export INPUT_TITLE="Hello world" export INPUT_COMMENT="Hello comment of world" output=$(bash "${SCRIPT_PATH}" 2>&1) -assert_contains "$output" "Adding closure comment..." -assert_contains "$output" "FAKE: added comment" -assert_contains "$output" "Closing issue #42..." +assert_contains "$output" "Closing issue #42 with comment..." assert_contains "$output" "FAKE: closed issue" echo "passed" +