Skip to content

Commit 19d09fd

Browse files
committed
scaffold automated feature build for external contributors
1 parent 55813e2 commit 19d09fd

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: External PR Trigger
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened]
6+
7+
jobs:
8+
trigger-external-pr:
9+
runs-on: ubuntu-latest
10+
# Only run if the source branch does not have a PMM- prefix
11+
if: |
12+
!startsWith(github.event.pull_request.head.ref, 'PMM-')
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Setup Git Config
19+
run: |
20+
git config --global user.name "github-actions[bot]"
21+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
22+
23+
- name: Create branch in target repository
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.TARGET_REPO_TOKEN }}
26+
TARGET_REPO: ${{ vars.TARGET_REPO_OWNER }}/${{ vars.TARGET_REPO_NAME }}
27+
SOURCE_BRANCH: ${{ github.event.pull_request.head.ref }}
28+
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
29+
run: |
30+
# Clone the target repository
31+
git clone https://x-access-token:${GITHUB_TOKEN}@github.com/${TARGET_REPO}.git target-repo
32+
cd target-repo
33+
34+
# Create a new branch based on the PR branch name
35+
git checkout -b "mongodb-exporter-external-pr-${SOURCE_BRANCH}"
36+
37+
# Check if ci.yml exists
38+
if [ ! -f "ci.yml" ]; then
39+
echo "Creating ci.yml file"
40+
mkdir -p $(dirname ci.yml)
41+
touch ci.yml
42+
fi
43+
44+
# Modify ci.yml file with PR information
45+
cat > ci.yml << EOF
46+
# Auto-generated from external PR
47+
deps:
48+
-name: mongodb_exporter
49+
url: https://github.com/${GITHUB_REPOSITORY}
50+
branch: ${SOURCE_BRANCH}
51+
EOF
52+
53+
# Commit changes
54+
git add ci.yml
55+
git commit -m "Update ci.yml for external PR #${PR_NUMBER} from ${PR_AUTHOR}"
56+
57+
# Push the new branch
58+
git push origin "external-pr-${SOURCE_BRANCH}"
59+
60+
- name: Create Pull Request in target repository
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.TARGET_REPO_TOKEN }}
63+
TARGET_REPO: ${{ vars.TARGET_REPO_OWNER }}/${{ vars.TARGET_REPO_NAME }}
64+
SOURCE_BRANCH: ${{ github.event.pull_request.head.ref }}
65+
PR_NUMBER: ${{ github.event.pull_request.number }}
66+
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
67+
run: |
68+
# Create PR using GitHub CLI
69+
gh pr create \
70+
--repo "${TARGET_REPO}" \
71+
--title "External PR: ${{ github.event.pull_request.title }}" \
72+
--body "This PR was automatically generated from external pull request #${PR_NUMBER} by @${PR_AUTHOR} in ${{ github.repository }} (branch: ${SOURCE_BRANCH}).
73+
74+
Original PR: ${{ github.event.pull_request.html_url }}
75+
76+
## Original PR Description
77+
${{ github.event.pull_request.body }}" \
78+
--base main \
79+
--head "external-pr-${SOURCE_BRANCH}"

EXTERNAL_PR_ACTION.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)