Skip to content

Create gh-issue-gen.yml #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
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
75 changes: 75 additions & 0 deletions .github/workflows/gh-issue-gen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: "Create Issue from Unlinked PR"

on:
pull_request:
types: [opened, edited, reopened]

jobs:
create-gh-issue:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LINEAR_TEAM: PAYFAST

steps:
- name: Check out code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Delay for manual linking (1 hour)
run: |
sleep 3600

- name: Check for linked Issue or Branch pattern
id: check
run: |
BRANCH=$(jq -r .pull_request.head.ref "$GITHUB_EVENT_PATH")
BODY="${{ github.event.pull_request.body }}"
TITLE="${{ github.event.pull_request.title }}"
echo "Checking branch: $BRANCH"

# 1) Branch names like add/123 or add/123-desc
if [[ "$BRANCH" =~ ^(add|fix|remove)/[0-9]+(-[a-zA-Z0-9]+)? ]]; then
echo "linked=true" >> $GITHUB_ENV

# 2) Linear shorthand or full‑URL refs
elif grep -qiE "(Fixes|Closes|Resolves|Relates to) (${LINEAR_TEAM}-[0-9]+|https?://[^ ]*${LINEAR_TEAM}-[0-9]+[^ ]*)" <<< "${BODY}${TITLE}"; then
echo "linked=true" >> $GITHUB_ENV

# 3) GitHub shorthand or full‑URL refs
elif grep -qiE "(Fixes|Closes|Resolves|Relates to) (#[0-9]+|https?://github\.com/[^/]+/[^/]+/issues/[0-9]+)" <<< "${BODY}${TITLE}"; then
echo "linked=true" >> $GITHUB_ENV

# 4) Nothing matched
else
echo "linked=false" >> $GITHUB_ENV
fi

- name: Create GitHub Issue, comment and link on PR
if: env.linked == 'false'
run: |
PR_NUMBER=$(jq -r .pull_request.number "$GITHUB_EVENT_PATH")
PR_TITLE=$(jq -r .pull_request.title "$GITHUB_EVENT_PATH")
PR_URL=$(jq -r .pull_request.html_url "$GITHUB_EVENT_PATH")

# Escape backticks and double-quotes to prevent shell command substitution or quote-breaking
SAFE_TITLE="${PR_TITLE//\`/\\\`}"
SAFE_TITLE="${SAFE_TITLE//\"/\\\"}"

# Create the Issue; gh prints the new Issue URL to stdout
ISSUE_URL=$(gh issue create \
--title "Tracking Issue for PR #${PR_NUMBER}: ${SAFE_TITLE}" \
--body "PR ${PR_URL} did not have a linked issue, so this issue has been created for tracking purposes.")

echo "Created GitHub issue: $ISSUE_URL"

# Comment on the PR linking to the new Issue
gh pr comment "$PR_NUMBER" \
--body "This is linked to GitHub issue ${ISSUE_URL}."

# Append a "Closes #ISSUE_NUM” line on the PR so it shows up as a Linked issue
ISSUE_NUM="${ISSUE_URL##*/}"
EXISTING_BODY=$(gh pr view "$PR_NUMBER" --json body --jq .body)
CLOSING="Closes #${ISSUE_NUM}"
UPDATED_BODY="${EXISTING_BODY}"$'\n\n'"${CLOSING}"

gh pr edit "$PR_NUMBER" --body "$UPDATED_BODY"