commit e22f716fd6938fb3c94ba81cb31c96babd0935c5 Author: Zhongwei Li Date: Sun Nov 30 08:58:30 2025 +0800 Initial commit diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json new file mode 100644 index 0000000..1f2745b --- /dev/null +++ b/.claude-plugin/plugin.json @@ -0,0 +1,14 @@ +{ + "name": "pr-automation", + "description": "Automate PR workflow approvals and ok-to-test comments", + "version": "0.0.1", + "author": { + "name": "stbenjam" + }, + "skills": [ + "./skills" + ], + "commands": [ + "./commands" + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cca1865 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# pr-automation + +Automate PR workflow approvals and ok-to-test comments diff --git a/commands/approve-workflows.md b/commands/approve-workflows.md new file mode 100644 index 0000000..2bc1b8d --- /dev/null +++ b/commands/approve-workflows.md @@ -0,0 +1,51 @@ +--- +description: Approve pending GitHub Actions workflows and add /ok-to-test comments to PRs +argument-hint: +--- + +## Name +pr-automation:approve-workflows + +## Synopsis +``` +/pr-automation:approve-workflows +``` + +## Description +The `pr-automation:approve-workflows` command automates the process of reviewing and approving pending pull requests from external contributors. It performs two key tasks: + +1. **Approves pending GitHub Actions workflows**: Finds all workflow runs that require approval (those with "action_required" conclusion) and approves them so they can execute. + +2. **Adds /ok-to-test comments**: Identifies all open PRs with the "needs-ok-to-test" label and adds a `/ok-to-test` comment to trigger CI testing. + +This command is particularly useful for repository maintainers who need to regularly process contributions from external collaborators. + +## Implementation + +Use the `pr-automation:approve-workflows` skill to implement this command. The skill is located at: +`~/.claude/plugins/pr-automation/skills/approve-workflows/SKILL.md` + +Invoke the skill and pass the repository name as context. + +## Return Value + +- **Format**: Summary of actions taken + - List of workflow runs approved + - List of PRs that received /ok-to-test comments + - Any errors or issues encountered + +## Examples + +1. **Approve workflows for a specific repository**: + ``` + /pr-automation:approve-workflows openshift-eng/ai-helpers + ``` + +2. **Use with current repository** (if in a git repo): + ``` + /pr-automation:approve-workflows + ``` + +## Arguments + +- `$1` (optional): Repository in `owner/repo` format. If not provided, will attempt to detect from current directory's git remote. diff --git a/plugin.lock.json b/plugin.lock.json new file mode 100644 index 0000000..ce67792 --- /dev/null +++ b/plugin.lock.json @@ -0,0 +1,49 @@ +{ + "$schema": "internal://schemas/plugin.lock.v1.json", + "pluginId": "gh:stbenjam/claude-nine:plugins/pr-automation", + "normalized": { + "repo": null, + "ref": "refs/tags/v20251128.0", + "commit": "44bb961da3301ccc2a22783449fe0126d60ee2a3", + "treeHash": "3051f7f300202bbc8f92b93171e47691a7e43466aaaca5860ea31bd30a1f5e16", + "generatedAt": "2025-11-28T10:28:26.930562Z", + "toolVersion": "publish_plugins.py@0.2.0" + }, + "origin": { + "remote": "git@github.com:zhongweili/42plugin-data.git", + "branch": "master", + "commit": "aa1497ed0949fd50e99e70d6324a29c5b34f9390", + "repoRoot": "/Users/zhongweili/projects/openmind/42plugin-data" + }, + "manifest": { + "name": "pr-automation", + "description": "Automate PR workflow approvals and ok-to-test comments", + "version": "0.0.1" + }, + "content": { + "files": [ + { + "path": "README.md", + "sha256": "921bd5479152cc8c838e352079fde6c5b72d6400b0ecf9f7c408a80bbf98114c" + }, + { + "path": ".claude-plugin/plugin.json", + "sha256": "cac6ed31b7952a120a9c0e38616f2328b032be3cc280facf803bd65da62e4766" + }, + { + "path": "commands/approve-workflows.md", + "sha256": "7bfee87c7369df96b6b18c9fe6271e36337f895efe491a0bcbb7f4b81f8d934e" + }, + { + "path": "skills/approve-workflows/SKILL.md", + "sha256": "2d7c1e832e3bc38a866aef6a051be0951bd21b50097829ccd2f960bf2933d94d" + } + ], + "dirSha256": "3051f7f300202bbc8f92b93171e47691a7e43466aaaca5860ea31bd30a1f5e16" + }, + "security": { + "scannedAt": null, + "scannerVersion": null, + "flags": [] + } +} \ No newline at end of file diff --git a/skills/approve-workflows/SKILL.md b/skills/approve-workflows/SKILL.md new file mode 100644 index 0000000..61ff775 --- /dev/null +++ b/skills/approve-workflows/SKILL.md @@ -0,0 +1,192 @@ +--- +name: PR Automation - Approve Workflows +description: Approve pending GitHub Actions workflows and add /ok-to-test comments to PRs that need testing +--- + +# PR Automation - Approve Workflows + +This skill automates the approval of pending GitHub Actions workflows and adds `/ok-to-test` comments to pull requests that require external contributor verification. + +## When to Use This Skill + +Use this skill when you need to: +- Approve multiple pending GitHub Actions workflows from external contributors +- Add `/ok-to-test` comments to PRs labeled with "needs-ok-to-test" +- Automate the PR review workflow for repositories with many external contributions + +## Prerequisites + +1. **GitHub CLI (`gh`) installed** + - Check if installed: `which gh` + - If not installed, user needs to install it: https://cli.github.com/ + +2. **GitHub authentication** + - Must be authenticated with `gh auth login` + - Must have write access to the repository + +3. **Repository context** + - Repository name in `owner/repo` format + - Or current directory must be a git repository + +## Implementation Steps + +### Step 1: Determine Repository + +If a repository argument is provided, use it. Otherwise, detect from the current directory: + +```bash +# If no repo provided, detect from git remote +if [ -z "$repo" ]; then + repo=$(git remote get-url origin | sed -E 's/.*github\.com[:/]([^/]+\/[^.]+)(\.git)?/\1/') +fi +``` + +### Step 2: Find PRs Needing /ok-to-test + +Query all open PRs with the "needs-ok-to-test" label: + +```bash +gh pr list --repo "$repo" --limit 100 --json number,title,labels --state open \ + --jq '.[] | select(.labels[]? | .name == "needs-ok-to-test") | {number: .number, title: .title}' +``` + +For each PR found: +1. Extract the PR number +2. Add a comment with `/ok-to-test`: + ```bash + gh pr comment $pr_number --repo "$repo" --body "/ok-to-test" + ``` +3. Track success/failure for final summary + +### Step 3: Find Workflow Runs Needing Approval + +Query GitHub Actions API for workflow runs that need approval. There are two scenarios: + +**Scenario A: Completed runs with action_required (already timed out)** +These cannot be approved but should be reported: +```bash +gh api "repos/$repo/actions/runs?per_page=100" \ + --jq '.workflow_runs[] | select(.conclusion == "action_required") | {id: .id, name: .name, branch: .head_branch, created: .created_at}' +``` + +**Scenario B: Waiting runs (can be approved)** +These are the ones we can actually approve: +```bash +gh api "repos/$repo/actions/runs" \ + --jq '.workflow_runs[] | select(.status == "waiting") | {id: .id, name: .name, branch: .head_branch}' +``` + +### Step 4: Approve Workflow Runs + +For each workflow run that needs approval (status == "waiting"): + +```bash +gh api "repos/$repo/actions/runs/$run_id/approve" -X POST +``` + +Track which runs were successfully approved vs which failed. + +### Step 5: Generate Summary Report + +Create a summary report with: + +1. **PRs that received /ok-to-test comments**: + - PR number and title + - Comment URL + +2. **Workflow runs approved**: + - Run ID + - Workflow name + - Branch name + - Status (approved/failed) + +3. **Action required runs (informational)**: + - Runs that timed out and cannot be approved + - Suggest that new commits will trigger new runs + +## Error Handling + +Handle the following error cases: + +1. **No repository found**: If not provided and can't detect from git, prompt user for repository name + +2. **GitHub API errors**: + - 404: Resource not found (run may have been deleted) + - 403: Permission denied (user lacks approval permissions) + - Report errors but continue processing other items + +3. **No PRs or workflows found**: Report that everything is up to date + +4. **Rate limiting**: If GitHub API rate limit is hit, report and suggest trying again later + +## Output Format + +Provide a structured summary: + +``` +# PR Workflow Approval Summary + +## PRs Marked as /ok-to-test (3) +- PR #116: CLID-473: subagent to generate weekly reports +- PR #105: Add claude interactive google service account creation +- PR #76: an olmv1 claude plugin for managing clusterextensions + +## GitHub Actions Workflows Approved (2) +- Run 18977369749: Lint Plugins (branch: add_qa_assignee_query) ✓ +- Run 18979766265: Lint Plugins (branch: gh2jira) ✓ + +## Timed Out Workflows (informational) (5) +These workflows timed out waiting for approval. New commits will trigger new runs. +- Run 18968492733: Lint Plugins (branch: raise_pr_debug_cluster) +- Run 18967558922: Lint Plugins (branch: ovn_analyzer) +... + +## Status +✓ All pending approvals completed successfully +``` + +## Examples + +### Example 1: Approve workflows for specific repository + +```bash +# User invokes: /pr-automation:approve-workflows openshift-eng/ai-helpers + +# Step 1: Set repo variable +repo="openshift-eng/ai-helpers" + +# Step 2: Find PRs needing ok-to-test +prs=$(gh pr list --repo "$repo" --limit 100 --json number,title,labels --state open \ + --jq '.[] | select(.labels[]? | .name == "needs-ok-to-test")') + +# Step 3: Comment on each PR +echo "$prs" | jq -r '.number' | while read pr; do + gh pr comment $pr --repo "$repo" --body "/ok-to-test" +done + +# Step 4: Find and approve waiting workflows +gh api "repos/$repo/actions/runs" \ + --jq '.workflow_runs[] | select(.status == "waiting") | .id' | \ + while read run_id; do + gh api "repos/$repo/actions/runs/$run_id/approve" -X POST + done +``` + +### Example 2: Approve workflows for current repository + +```bash +# User invokes: /pr-automation:approve-workflows + +# Detect repo from git remote +repo=$(git remote get-url origin | sed -E 's/.*github\.com[:/]([^/]+\/[^.]+)(\.git)?/\1/') + +# Continue with same steps as Example 1... +``` + +## Notes + +- The `/ok-to-test` command is specific to Prow CI and triggers Prow jobs, not GitHub Actions +- GitHub Actions workflows from external contributors require manual approval via the approve API +- Once a PR has the "ok-to-test" label, future commits may not require re-approval +- Workflow runs that have already timed out (action_required conclusion) cannot be approved retroactively +- Some repositories may use different label names - this skill assumes "needs-ok-to-test"