Skip to content

Commit 2d8b539

Browse files
authored
Update pr_creation.yml
1 parent a90ff50 commit 2d8b539

1 file changed

Lines changed: 65 additions & 25 deletions

File tree

.github/workflows/pr_creation.yml

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,84 @@
1-
name: Create Feature Branches
1+
name: Auto-create Feature Branches and PRs
22

33
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
97

108
jobs:
119
create_branches_and_prs:
1210
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+
1322
steps:
14-
- name: Checkout
23+
- name: Checkout repository
1524
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0
1627

1728
- name: Set up GH CLI
1829
uses: cli/gh-action@v2
1930

20-
- name: Create branches
31+
- name: Configure GitHub CLI
32+
run: gh auth setup-git
33+
34+
- name: Create branches if not exist
2135
run: |
22-
fname="${{ github.event.inputs.feature_name }}"
36+
fname=${FEATURE_NAME}
2337
base=main
2438
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+
}
2654
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
3659
3760
- name: Create draft PRs
3861
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

Comments
 (0)