-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaction.yml
188 lines (174 loc) · 9.24 KB
/
action.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
name: 'Manifest PR handler'
description: 'Handles manifest PR related tasks'
inputs:
target-repo:
required: false
type: string
default: nrfconnect/sdk-nrf
description: 'target repository for manifest PR'
base-branch:
required: false
type: string
default: main
description: 'base/target branch for manifest PR'
forked-repo:
required: false
type: string
default: nordicbuilder/sdk-nrf
description: 'forked repository where changes are pushed'
skip-string:
required: false
type: string
default: manifest-pr-skip
manifest-pr-title-details:
required: false
type: string
default: 'Update revision'
description: 'Input to the manifest PR title: "manifest: <repo>: <manifest-pr-title-details>"'
token:
required: true
type: string
runs:
using: "composite"
steps:
- name: Skip string detection
id: check
if: contains(github.event.pull_request.title, inputs.skip-string ) ||
contains(github.event.pull_request.body, inputs.skip-string )
shell: bash
run: |
echo "Skip string detected. Will skip following steps."
echo "SKIP_STRING=true" >> "$GITHUB_OUTPUT"
# fail in case of external user
- name: Add external label
uses: carlescufi/action-contribs@main
with:
github-token: ${{ inputs.token }}
command: 'external'
labels: 'external'
- name: Stop in case external label is set
shell: bash
run: |
LABELS=$(gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json labels --jq '.labels[].name')
if [[ "$LABELS" == *"external"* ]]; then
echo "External contribution detected. Automatic creation of manifest PR failed."
echo "To test your changes, please create PR in the sdk-nrf repository."
exit 1
fi
env:
GH_TOKEN: ${{ inputs.token }}
# Common setup for all the phases
- name: Setup git
if: ${{ steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
git config --global user.email "[email protected]"
git config --global user.name "Nordic Builder"
# Checkout phase for manifest PR creation
- name: Checkout sources
if: ${{ github.event.action == 'opened' && steps.check.outputs.SKIP_STRING != 'true' }}
uses: actions/checkout@v4
with:
repository: ${{ inputs.target-repo }}
token: ${{ inputs.token }}
ref: ${{ inputs.base-branch }}
# west.yml preparation for manifest PR creation
- name: Change west.yml PR revision
if: ${{ github.event.action == 'opened' && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
PROJECT_KEY=$(yq --exit-status '.manifest.projects[] | select(.repo-path == "${{ github.event.repository.name }}") | key' west.yml)
yq ".manifest.projects[$PROJECT_KEY].revision = \"pull/${{ github.event.pull_request.number }}/head\"" west.yml > tmp.yml
diff -B west.yml tmp.yml | patch west.yml - || true
- name: Change west.yml Dragoon revision
if: ${{ startsWith(github.event.pull_request.title, 'Update MPSL and SoftDevice Controller') && github.event.action == 'opened' && steps.check.outputs.SKIP_STRING != 'true'}}
env:
COMMITS_URL: ${{ github.event.pull_request.commits_url }}
shell: bash
run: |
if [ "${COMMITS_URL}x" != "x" ]; then
# API URL details: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-commits-on-a-pull-request
LAST_MSG=$(curl -H "Authorization: token ${{ inputs.token }}" "${COMMITS_URL}?per_page=3" | jq -r .[-1].commit.message)
fi
DRAGOON_REV=$(echo $LAST_MSG | awk 'NR==1 {print $3}' | head)
echo "DRAGOON_REV is $DRAGOON_REV"
PROJECT_KEY=$(yq --exit-status '.manifest.projects[] | select(.name == "dragoon") | key' west.yml)
yq ".manifest.projects[$PROJECT_KEY].revision = \"$DRAGOON_REV\"" west.yml > tmp.yml
diff -B west.yml tmp.yml | patch west.yml - || true
- name: Change west.yml nrf-802154 revision
if: ${{ startsWith(github.event.pull_request.title, 'Update revision of nrf_802154') && github.event.action == 'opened' && steps.check.outputs.SKIP_STRING != 'true' }}
env:
COMMITS_URL: ${{ github.event.pull_request.commits_url }}
shell: bash
run: |
if [ "${COMMITS_URL}x" != "x" ]; then
# API URL details: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-commits-on-a-pull-request
LAST_MSG=$(curl -H "Authorization: token ${{ inputs.token }}" "${COMMITS_URL}?per_page=3" | jq -r .[-1].commit.message)
fi
NRF_802154_REV=$(echo $LAST_MSG | awk 'NR==1 {print $3}' | head)
echo "NRF_802154_REV is $NRF_802154_REV"
PROJECT_KEY=$(yq --exit-status '.manifest.projects[] | select(.name == "nrf-802154") | key' west.yml)
yq ".manifest.projects[$PROJECT_KEY].revision = \"$NRF_802154_REV\"" west.yml > tmp.yml
diff -B west.yml tmp.yml | patch west.yml - || true
# create actual manifest PR
- name: Commit changed west.yml and create PR
if: ${{ github.event.action == 'opened' && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
MANIFEST_PR_TITLE: ${{ inputs.manifest-pr-title-details }}
run: |
git checkout -b manifest_pr
git add west.yml
git commit -m "manifest: Update ${{ github.event.repository.name }} revision (auto-manifest PR)" -m "Automatically created by Github Action" --signoff
git remote add fork https://nordicbuilder:${{ inputs.token }}@github.com/${{ inputs.forked-repo }}
git push -u fork manifest_pr:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
gh pr create --base ${{ inputs.base-branch }} --repo ${{ inputs.target-repo }} --title "manifest: ${{ github.event.repository.name }}: $MANIFEST_PR_TITLE" \
--body "Automatically created by action-manifest-pr GH action from PR: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
# Checkout phase for retrigger CI or update west.yml from PR to sha
- name: Checkout sources from fork
if: ${{ (github.event.action == 'synchronize' || github.event.pull_request.merged == true) && steps.check.outputs.SKIP_STRING != 'true' }}
uses: actions/checkout@v4
with:
repository: ${{ inputs.forked-repo }}
token: ${{ inputs.token }}
fetch-depth: 0
# This is used for retriggering CI in case sub-PR got updates (assumes manifest PR exists)
- name: Retrigger CI by changing commit sha and pushing
if: ${{ github.event.action == 'synchronize' && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
git checkout auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
git remote add upstream https://github.com/${{ inputs.target-repo }}
git fetch upstream
git rebase upstream/${{ inputs.base-branch }} -X theirs
git commit --amend --no-edit
git push origin auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }} -f
# This is used for changing PR pointer to sha in west.yml (assumes manifest PR exists)
- name: Change commit sha and push it
if: ${{ github.event.pull_request.merged == true && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
git checkout auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
PROJECT_KEY=$(yq --exit-status '.manifest.projects[] | select(.repo-path == "${{ github.event.repository.name }}") | key' west.yml)
yq ".manifest.projects[$PROJECT_KEY].revision = \"$GITHUB_SHA\"" west.yml > tmp.yml
diff -B west.yml tmp.yml | patch west.yml - || true
git add west.yml
git commit --amend --no-edit
git remote add upstream https://github.com/${{ inputs.target-repo }}
git fetch upstream
set +e
git merge-tree --write-tree --name-only upstream/${{ inputs.base-branch }} auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
has_conflict="$?"
echo "has_conflict is" $has_conflict
set -e
if (( $has_conflict == 1)) ; then git rebase upstream/${{ inputs.base-branch }} -X theirs ; fi
git push origin auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }} -f
- name: Close manifest-PR upon closing PR
if: ${{ github.event.action == 'closed' && github.event.pull_request.merged == false && steps.check.outputs.SKIP_STRING != 'true'}}
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
run: |
gh pr close -R ${{ inputs.target-repo }} NordicBuilder:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }} \
--comment "Automatically closed by action-manifest-pr GH action" || true