-
Notifications
You must be signed in to change notification settings - Fork 137
150 lines (133 loc) · 6.21 KB
/
Copy pathupdate-feature-branches.yml
File metadata and controls
150 lines (133 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Update Feature Branches
on:
workflow_dispatch:
inputs:
branch_patterns:
description: 'Space-separated list of feature branch patterns'
default: 'feature_branch/*'
required: true
main_branch:
description: 'Main branch to merge'
default: 'main'
required: true
schedule:
- cron: "0 16 * * 1" # Mondays, 4pm UTC = 9am PST / 10am PDT
permissions:
contents: read
env:
defaultBranchPattern: "feature_branch/*"
defaultMainBranch: "main"
triggerTestsLabel: "tests-requested: quick"
branchPrefix: "workflow/auto-merge-feature-branch-"
jobs:
list_feature_branches:
name: list-feature-branches
runs-on: ubuntu-22.04
outputs:
branch_list: ${{ steps.get-branches.outputs.branch_list }}
steps:
- name: Check out repo (if needed)
if: ${{ github.event.inputs.branch_list == '' }}
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # ratchet:actions/checkout@v3
- name: Get list of feature branches
id: get-branches
run: |
branch_pattern='origin/${{ env.defaultBranchPattern }}'
if [[ -n "${GITHUB_EVENT_INPUTS_BRANCH_PATTERNS}" ]]; then
branch_pattern=origin/$(echo "${GITHUB_EVENT_INPUTS_BRANCH_PATTERNS}" | sed 's| | origin/|g')
fi
git remote update
echo "Branch pattern: ${branch_pattern}"
branch_list=$(git branch --list --all "${branch_pattern}")
if [[ -n ${branch_list} ]]; then
# If there's at least one entry, process the list.
echo "Remote branch list: ${branch_list}"
# Remove remotes/origin/ from each branch.
branch_list=$(echo ${branch_list} | sed 's| remotes/origin/| |g' | sed 's|^remotes/origin/||')
# Change spaces to commas.
branch_list=$(echo ${branch_list} | sed 's/ /,/g')
# Add quotes around each branch name.
branch_list='"'$(echo ${branch_list} | sed 's/,/","/g')'"'
fi
echo "::warning ::Branch list: [${branch_list}]"
echo "branch_list=[${branch_list}]" >> $GITHUB_OUTPUT
env:
GITHUB_EVENT_INPUTS_BRANCH_PATTERNS: ${{ github.event.inputs.branch_patterns }}
create_merge_prs:
name: create-merge-pr-${{ matrix.branch_name }}
needs: [ list_feature_branches ]
runs-on: ubuntu-22.04
permissions:
contents: write
if: ${{ needs.list_feature_branches.outputs.branch_list != '[]' }}
strategy:
fail-fast: false
matrix:
branch_name: ${{ fromJson(needs.list_feature_branches.outputs.branch_list) }}
steps:
- name: Get token for firebase-workflow-trigger
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # ratchet:actions/create-github-app-token@v1
id: generate-token
with:
client-id: ${{ secrets.WORKFLOW_TRIGGER_CLIENT_ID }}
private-key: ${{ secrets.WORKFLOW_TRIGGER_APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
- name: Setup python
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # ratchet:actions/setup-python@v4
with:
python-version: 3.9
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # ratchet:actions/checkout@v3
with:
ref: ${{ matrix.branch_name }}
fetch-depth: 0
submodules: false
- name: Install prerequisites
run: |
python scripts/gha/install_prereqs_desktop.py
python -m pip install requests
- name: Name new branch
run: |
date_str=$(date "+%Y%m%d-%H%M%S")
echo "NEW_BRANCH=${{env.branchPrefix}}${{github.run_number}}-${date_str}" >> $GITHUB_ENV
- name: Create merge PR
id: create-pr
run: |
git config user.email "firebase-workflow-trigger-bot@google.com"
git config user.name "firebase-workflow-trigger-bot"
git config core.commentChar "%" # so we can use # in git commit messages
main_branch='${{ env.defaultMainBranch }}'
if [[ -n "${GITHUB_EVENT_INPUTS_MAIN_BRANCH}" ]]; then
main_branch="${GITHUB_EVENT_INPUTS_MAIN_BRANCH}"
fi
# Attempt a merge, then check if any files changed.
git merge --no-commit --no-ff "origin/${main_branch}" || true
if git diff --quiet ${MATRIX_BRANCH_NAME}; then
# No merge necessary.
echo "::warning ::No merge needed for ${MATRIX_BRANCH_NAME}, won't create pull request."
echo "created_pr_number=0" >> $GITHUB_OUTPUT
exit 0
fi
# Undo the actual merge. Let the PR creation handle it.
git merge --abort
date_str=$(date "+%b %d, %Y")
pr_title="Automatic merge of ${main_branch} into ${MATRIX_BRANCH_NAME} - ${date_str}"
pr_body="Automatic merge of ${main_branch} into ${MATRIX_BRANCH_NAME}.
> Created on ${date_str} by [${GITHUB_WORKFLOW} workflow]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID).
"
git checkout main
git checkout -b "${NEW_BRANCH}"
git push --set-upstream origin "${NEW_BRANCH}"
pr_number=$(python scripts/gha/create_pull_request.py --token ${STEPS_GENERATE_TOKEN_OUTPUTS_TOKEN} --base "${MATRIX_BRANCH_NAME}" --head "${NEW_BRANCH}" --title "${pr_title}" --body "${pr_body}")
echo "created_pr_number=${pr_number}" >> $GITHUB_OUTPUT
env:
GITHUB_EVENT_INPUTS_MAIN_BRANCH: ${{ github.event.inputs.main_branch }}
MATRIX_BRANCH_NAME: ${{ matrix.branch_name }}
STEPS_GENERATE_TOKEN_OUTPUTS_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Set test trigger label.
uses: actions-ecosystem/action-add-labels@18f1af5e3544586314bbe15c0273249c770b2daf # ratchet:actions-ecosystem/action-add-labels@v1
if: ${{ steps.create-pr.outputs.created_pr_number }}
with:
github_token: ${{ steps.generate-token.outputs.token }}
number: ${{ steps.create-pr.outputs.created_pr_number }}
labels: "${{ env.triggerTestsLabel }}"