Files
2026-07-13 13:22:34 +08:00

92 lines
2.9 KiB
YAML

# Generate PR or issue title using AI
name: Title
on:
issue_comment:
types: [created]
pull_request:
paths:
- ".github/workflows/title.yml"
- ".github/workflows/title.js"
defaults:
run:
shell: bash
permissions: {}
jobs:
title:
runs-on: ubuntu-slim
timeout-minutes: 5
# Trigger conditions:
# - pull_request: skip fork PRs (no access to secrets)
# - issue_comment: only maintainers can trigger via `/title` on PRs or issues
if: >
(
github.event_name == 'pull_request'
&& github.event.pull_request.head.repo.full_name == github.repository
)
|| (
startsWith(github.event.comment.body, '/title')
&& contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association)
)
permissions:
pull-requests: write
issues: write
env:
NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false
sparse-checkout: .github/workflows/title.js
sparse-checkout-cone-mode: false
- name: Add reaction to comment
if: github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
"/repos/$REPO/issues/comments/$COMMENT_ID/reactions" \
-f content='eyes'
- name: Generate new title
id: generate
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
IS_PR: ${{ github.event_name == 'pull_request' || github.event.issue.pull_request != null }}
with:
retries: 3
script: |
const script = require('./.github/workflows/title.js');
await script({ github, context, core });
- name: Update title
if: github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NEW_TITLE: ${{ steps.generate.outputs.new_title }}
run: |
if [ -z "$NEW_TITLE" ]; then
echo "Error: Generated title is empty"
exit 1
fi
gh issue edit "$NUMBER" --repo "$REPO" --title "$NEW_TITLE"
- name: Post failure comment
if: failure() && github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
gh issue comment "$NUMBER" --repo "$REPO" --body \
"> [!WARNING]
> Failed to generate the title. See [workflow run]($RUN_URL) for details."