|
1 | | -name: Create Feature Branches |
| 1 | +name: Auto-create Feature Branches and PRs |
2 | 2 |
|
3 | 3 | on: |
4 | | - workflow_dispatch: |
5 | | - inputs: |
6 | | - feature_name: |
7 | | - description: "Name of the feature (e.g. new-dashboard)" |
8 | | - required: true |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
9 | 7 |
|
10 | 8 | jobs: |
11 | 9 | create_branches_and_prs: |
12 | 10 | runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + env: |
| 16 | + FEATURE_NAME: testRes |
| 17 | + DEV_ASSIGNEE: dev-user |
| 18 | + DOC_ASSIGNEE: doc-user |
| 19 | + QA_ASSIGNEE: qa-user |
| 20 | + RELEASE_ASSIGNEE: release-owner |
| 21 | + |
13 | 22 | steps: |
14 | | - - name: Checkout |
| 23 | + - name: Checkout repository |
15 | 24 | uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
16 | 27 |
|
17 | 28 | - name: Set up GH CLI |
18 | 29 | uses: cli/gh-action@v2 |
19 | 30 |
|
20 | | - - name: Create branches |
| 31 | + - name: Configure GitHub CLI |
| 32 | + run: gh auth setup-git |
| 33 | + |
| 34 | + - name: Create branches if not exist |
21 | 35 | run: | |
22 | | - fname="${{ github.event.inputs.feature_name }}" |
| 36 | + fname=${FEATURE_NAME} |
23 | 37 | base=main |
24 | 38 |
|
25 | | - gh api repos/${{ github.repository }}/git/ref/heads/$base || exit 1 |
| 39 | + echo "Creating branches for feature: $fname" |
| 40 | +
|
| 41 | + # Function to create branch if not exists |
| 42 | + create_branch() { |
| 43 | + branch=$1 |
| 44 | + from=$2 |
| 45 | + if ! git ls-remote --exit-code origin "refs/heads/$branch" >/dev/null 2>&1; then |
| 46 | + echo "Creating branch $branch from $from" |
| 47 | + git fetch origin $from |
| 48 | + git branch $branch origin/$from |
| 49 | + git push origin $branch |
| 50 | + else |
| 51 | + echo "Branch $branch already exists, skipping." |
| 52 | + fi |
| 53 | + } |
26 | 54 |
|
27 | | - # Create feature branches |
28 | | - gh api repos/${{ github.repository }}/git/refs \ |
29 | | - -f ref=refs/heads/src/$fname -f sha=$(git rev-parse origin/$base) |
30 | | - gh api repos/${{ github.repository }}/git/refs \ |
31 | | - -f ref=refs/heads/doc/$fname -f sha=$(git rev-parse origin/$base) |
32 | | - gh api repos/${{ github.repository }}/git/refs \ |
33 | | - -f ref=refs/heads/test/$fname -f sha=$(git rev-parse origin/src/$fname) |
34 | | - gh api repos/${{ github.repository }}/git/refs \ |
35 | | - -f ref=refs/heads/feature/$fname -f sha=$(git rev-parse origin/$base) |
| 55 | + create_branch src/$fname main |
| 56 | + create_branch doc/$fname main |
| 57 | + create_branch test/$fname src/$fname |
| 58 | + create_branch feature/$fname main |
36 | 59 |
|
37 | 60 | - name: Create draft PRs |
38 | 61 | run: | |
39 | | - fname="${{ github.event.inputs.feature_name }}" |
40 | | - |
41 | | - gh pr create --base feature/$fname --head src/$fname --title "Source changes for $fname" --draft |
42 | | - gh pr create --base feature/$fname --head doc/$fname --title "Docs for $fname" --draft |
43 | | - gh pr create --base src/$fname --head test/$fname --title "Tests for $fname" --draft |
44 | | - gh pr create --base main --head feature/$fname --title "Integrate $fname" --draft |
| 62 | + fname=${FEATURE_NAME} |
| 63 | +
|
| 64 | + echo "Creating draft PRs for $fname" |
| 65 | +
|
| 66 | + # Helper function to check and create PR |
| 67 | + create_pr() { |
| 68 | + head=$1 |
| 69 | + base=$2 |
| 70 | + title=$3 |
| 71 | + assignee=$4 |
| 72 | +
|
| 73 | + if ! gh pr list --head $head --base $base --json headRefName,baseRefName --jq '.[] | select(.headRefName=="'$head'" and .baseRefName=="'$base'")' >/dev/null; then |
| 74 | + echo "Creating PR: $head → $base" |
| 75 | + gh pr create --base $base --head $head --title "$title" --draft --assignee $assignee |
| 76 | + else |
| 77 | + echo "PR from $head to $base already exists." |
| 78 | + fi |
| 79 | + } |
| 80 | +
|
| 81 | + create_pr src/$fname feature/$fname "Source changes for $fname" |
| 82 | + create_pr doc/$fname feature/$fname "Docs for $fname" |
| 83 | + create_pr test/$fname src/$fname "Tests for $fname" |
| 84 | + create_pr feature/$fname main "Integrate $fname" |
0 commit comments