From 4d98d83fbb4ccf368666698a1a9d6627a4072326 Mon Sep 17 00:00:00 2001 From: lyn Date: Wed, 1 Apr 2026 17:09:04 +0000 Subject: [PATCH] fix: use /pulls instead of /pulls/pinned (#4) --- git-hangman-lab/SKILL.md | 1 + git-hangman-lab/scripts/git-ctrl | 2 +- git-hangman-lab/scripts/pr | 108 ++++++++++++++++++++++++++----- 3 files changed, 94 insertions(+), 17 deletions(-) diff --git a/git-hangman-lab/SKILL.md b/git-hangman-lab/SKILL.md index 57e2c19..bf0e2a9 100644 --- a/git-hangman-lab/SKILL.md +++ b/git-hangman-lab/SKILL.md @@ -52,6 +52,7 @@ Manage pull requests on git.hangman-lab.top. {baseDir}/scripts/git-ctrl pr list {baseDir}/scripts/git-ctrl pr commits {baseDir}/scripts/git-ctrl pr merge [commit-id] [title] [message] +{baseDir}/scripts/git-ctrl pr show ``` > **Note**: The access token will be automatically generated if not found. diff --git a/git-hangman-lab/scripts/git-ctrl b/git-hangman-lab/scripts/git-ctrl index 4f301a2..11a13e3 100755 --- a/git-hangman-lab/scripts/git-ctrl +++ b/git-hangman-lab/scripts/git-ctrl @@ -11,7 +11,7 @@ if [[ $# -eq 0 ]]; then echo " check-git-cred Verify git credentials" echo " create-git-account Create a new git account" echo " create-repo Create a new repository" - echo " pr Pull request operations (create/list/commits/merge)" + echo " pr Pull request operations (create/list/commits/merge/show)" echo " generate-access-token Generate access token for current user" echo " link-keycloak Link Keycloak account with Gitea" echo " repo-add-collaborators Add collaborator to repository" diff --git a/git-hangman-lab/scripts/pr b/git-hangman-lab/scripts/pr index 0fc9c53..9f237f9 100755 --- a/git-hangman-lab/scripts/pr +++ b/git-hangman-lab/scripts/pr @@ -97,7 +97,7 @@ cmd_list() { echo "Listing PRs for: $OWNER/$REPO_NAME" - RESPONSE=$(curl -s -X GET "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls/pinned" \ + RESPONSE=$(curl -s -X GET "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls" \ -H 'accept: application/json' \ -H "Authorization: token ${token}") @@ -134,6 +134,40 @@ cmd_commits() { echo "$RESPONSE" | jq -r '.[] | "\(.sha[0:7])\t\(.commit.message | split("\n")[0])"' 2>/dev/null || echo "$RESPONSE" } +# Subcommand: show +cmd_show() { + local repo_path="$1" + local pr_index="$2" + + get_repo_info "$repo_path" + local token + token="$(ensure_token)" + + echo "Showing PR #$pr_index in: $OWNER/$REPO_NAME" + + RESPONSE=$(curl -s -X GET "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls/${pr_index}" \ + -H 'accept: application/json' \ + -H "Authorization: token ${token}") + + if echo "$RESPONSE" | jq -e '.message' >/dev/null 2>&1; then + ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message') + echo "Error: $ERROR_MSG" + exit 1 + fi + + echo "$RESPONSE" | jq '{ + id, number, title, body, state, draft, mergeable, merged, + additions, deletions, changed_files, comments, + html_url, diff_url, patch_url, + labels, + milestone, + base: .base | {label, ref, sha}, + head: .head | {label, ref, sha}, + merge_base, + created_at, updated_at, closed_at + }' +} + # Subcommand: merge cmd_merge() { local repo_path="$1" @@ -180,27 +214,61 @@ cmd_merge() { merge_when_checks_succeed: true }') - RESPONSE=$(curl -s -X POST "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls/${pr_index}/merge" \ + TEMP_FILE=$(mktemp) + HTTP_CODE=$(curl -s -o "$TEMP_FILE" -w "%{http_code}" -X POST "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls/${pr_index}/merge" \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -H "Authorization: token ${token}" \ -d "$json") - if echo "$RESPONSE" | jq -e '.message' >/dev/null 2>&1; then - ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message') - echo "Error: $ERROR_MSG" - exit 1 - fi + RESPONSE=$(cat "$TEMP_FILE") + rm -f "$TEMP_FILE" - MERGED=$(echo "$RESPONSE" | jq -r '.merged') - - if [[ "$MERGED" == "true" ]]; then - echo "Pull request merged successfully!" - else - echo "Error: Failed to merge pull request" - echo "$RESPONSE" | jq '.' - exit 1 - fi + case "$HTTP_CODE" in + 200) + MERGED=$(echo "$RESPONSE" | jq -r '.merged') + if [[ "$MERGED" == "true" ]]; then + echo "merge success" + else + echo "merge failed" + echo "$RESPONSE" | jq '.' + exit 1 + fi + ;; + 409) + ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message // "Conflicting changes"') + echo "Error [$HTTP_CODE]: $ERROR_MSG" + exit 1 + ;; + 404) + ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message // "Not found"') + echo "Error [$HTTP_CODE]: $ERROR_MSG" + exit 1 + ;; + 423) + ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message // "Repository is archived"') + echo "Error [$HTTP_CODE]: $ERROR_MSG" + exit 1 + ;; + 405) + echo "merge failed check the pr status" + # Fetch PR details to show merge status + PR_INFO=$(curl -s -X GET "https://git.hangman-lab.top/api/v1/repos/${OWNER}/${REPO_NAME}/pulls/${pr_index}" \ + -H 'accept: application/json' \ + -H "Authorization: token ${token}") + echo "$PR_INFO" | jq '{mergeable: .mergeable, head_sha: .head.sha, base_ref: .base.ref, head_ref: .head.ref}' + exit 1 + ;; + *) + if echo "$RESPONSE" | jq -e '.message' >/dev/null 2>&1; then + ERROR_MSG=$(echo "$RESPONSE" | jq -r '.message') + echo "Error [$HTTP_CODE]: $ERROR_MSG" + else + echo "Error [$HTTP_CODE]: Unknown error" + fi + exit 1 + ;; + esac } # Usage @@ -214,6 +282,7 @@ usage() { echo " commits List commits in a PR" echo " merge [commit-id] [title] [message]" echo " Merge a pull request" + echo " show Show PR details" echo "" echo " can be: merge, squash, rebase, manually-merged" exit 2 @@ -256,6 +325,13 @@ case "$COMMAND" in fi cmd_merge "$@" ;; + show) + if [[ $# -lt 2 ]]; then + echo "Error: show requires " + exit 2 + fi + cmd_show "$@" + ;; *) echo "Error: Unknown command: $COMMAND" usage