-
Notifications
You must be signed in to change notification settings - Fork 117
137 lines (121 loc) · 5.39 KB
/
update-feature-branches.yml
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
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
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-20.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@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
create_merge_prs:
name: create-merge-pr-${{ matrix.branch_name }}
needs: [ list_feature_branches ]
runs-on: ubuntu-20.04
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: tibdex/github-app-token@v1
id: generate-token
with:
app_id: ${{ secrets.WORKFLOW_TRIGGER_APP_ID }}
private_key: ${{ secrets.WORKFLOW_TRIGGER_APP_PRIVATE_KEY }}
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: 3.8
- uses: 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 "[email protected]"
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
- name: Set test trigger label.
uses: 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 }}"