|
| 1 | +# External PR Trigger GitHub Action |
| 2 | + |
| 3 | +This GitHub Action automatically triggers actions in a target repository when a pull request is created by users who are not in an allowed list. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When a pull request is opened, synchronized, or reopened from a branch that does NOT have a `PMM-` prefix, this action will: |
| 8 | +1. Create a new branch in a specified target repository |
| 9 | +2. Modify a `ci.yml` file in that repository with PR information |
| 10 | +3. Create a pull request in the target repository |
| 11 | + |
| 12 | +## Setup Instructions |
| 13 | + |
| 14 | +### 1. Understanding the Branch Prefix Check |
| 15 | + |
| 16 | +The workflow file `.github/workflows/external-pr-trigger.yml` checks the source branch name: |
| 17 | + |
| 18 | +```yaml |
| 19 | +if: | |
| 20 | + !startsWith(github.event.pull_request.head.ref, 'PMM-') |
| 21 | +``` |
| 22 | +
|
| 23 | +This means: |
| 24 | +- PRs from branches WITH the `PMM-` prefix will NOT trigger this action |
| 25 | +- PRs from branches WITHOUT the `PMM-` prefix WILL trigger this action |
| 26 | + |
| 27 | +### 2. Create a Personal Access Token |
| 28 | + |
| 29 | +You need a Personal Access Token (PAT) with permissions to create branches and pull requests in the target repository: |
| 30 | + |
| 31 | +1. Go to GitHub Settings → Developer settings → Personal access tokens |
| 32 | +2. Generate a new token with the following scopes: |
| 33 | + - `repo` (full control of private repositories) |
| 34 | + - `workflow` (if the target repo has GitHub Actions) |
| 35 | +3. Copy the generated token |
| 36 | + |
| 37 | +### 3. Configure Repository Secrets |
| 38 | + |
| 39 | +In your repository settings, go to Secrets and variables → Actions, and add: |
| 40 | + |
| 41 | +- **SECRET**: `TARGET_REPO_TOKEN` - The Personal Access Token you created |
| 42 | + |
| 43 | +### 4. Configure Repository Variables |
| 44 | + |
| 45 | +In your repository settings, go to Secrets and variables → Actions → Variables tab, and add: |
| 46 | + |
| 47 | +- **VARIABLE**: `TARGET_REPO_OWNER` - The owner/organization of the target repository |
| 48 | +- **VARIABLE**: `TARGET_REPO_NAME` - The name of the target repository |
| 49 | + |
| 50 | +Example: |
| 51 | +- `TARGET_REPO_OWNER`: `myorg` |
| 52 | +- `TARGET_REPO_NAME`: `ci-configs` |
| 53 | + |
| 54 | +### 5. Customize the ci.yml Content (Optional) |
| 55 | + |
| 56 | +The action creates/updates a `ci.yml` file in the target repository. You can customize the content by modifying this section in the workflow: |
| 57 | + |
| 58 | +```yaml |
| 59 | +# Modify ci.yml file with PR information |
| 60 | +cat > ci.yml << EOF |
| 61 | +# Auto-generated from external PR |
| 62 | +external_pr: |
| 63 | + deps: |
| 64 | + - name: mongodb_exporter |
| 65 | + url: https://github.com/percona/mongodb_exporter |
| 66 | + branch: branch-name |
| 67 | +EOF |
| 68 | +``` |
| 69 | + |
| 70 | +## How It Works |
| 71 | + |
| 72 | +1. **Trigger**: The action runs on every pull request event (opened, synchronized, reopened) |
| 73 | + |
| 74 | +3. **Branch Creation**: For external users, it: |
| 75 | + - Clones the target repository |
| 76 | + - Creates a new branch named `external-pr-{original-branch-name}` |
| 77 | + - Updates the `ci.yml` file with PR metadata |
| 78 | + |
| 79 | +4. **Pull Request**: Creates a pull request in the target repository with: |
| 80 | + - Title: "External PR: {original PR title}" |
| 81 | + - Body: Contains a link to the original PR and its description |
| 82 | + |
| 83 | +## Security Considerations |
| 84 | + |
| 85 | +1. **Token Security**: The PAT is stored as a secret and never exposed in logs |
| 86 | +2. **Limited Scope**: The action only modifies the specified `ci.yml` file |
| 87 | +3. **Branch Filtering**: Only PRs from branches without the `PMM-` prefix trigger the action |
| 88 | + |
| 89 | +## Troubleshooting |
| 90 | + |
| 91 | +### Action Not Triggering |
| 92 | +- Verify the branch does NOT have a `PMM-` prefix |
| 93 | +- Check that the workflow file is in `.github/workflows/` directory |
| 94 | +- Ensure the workflow has the correct event triggers |
| 95 | + |
| 96 | +### Permission Errors |
| 97 | +- Verify the PAT has the correct scopes |
| 98 | +- Check that the token hasn't expired |
| 99 | +- Ensure the target repository allows the token's access |
| 100 | + |
| 101 | +### Branch/PR Creation Fails |
| 102 | +- Check that the target repository exists |
| 103 | +- Verify the `TARGET_REPO_OWNER` and `TARGET_REPO_NAME` variables are correct |
| 104 | +- Ensure there isn't already a branch with the same name |
| 105 | + |
| 106 | +## Example Scenarios |
| 107 | + |
| 108 | +### Scenario 1: PR from non-PMM branch (Action triggers) |
| 109 | +1. User creates PR #123 from branch `fix-bug` (no PMM- prefix) |
| 110 | +2. This action triggers and: |
| 111 | + - Creates branch `external-pr-fix-bug` in the target repository |
| 112 | + - Updates `ci.yml` with PR #123's information |
| 113 | + - Creates a PR in the target repository titled "External PR: Fix bug" |
| 114 | +3. The target repository can then run its own CI/CD processes based on the `ci.yml` content |
| 115 | + |
| 116 | +### Scenario 2: PR from PMM branch (Action does NOT trigger) |
| 117 | +1. User creates PR #124 from branch `PMM-1234-fix-issue` |
| 118 | +2. This action does NOT trigger because the branch has the `PMM-` prefix |
| 119 | +3. The PR proceeds with normal repository workflows |
0 commit comments