-
Notifications
You must be signed in to change notification settings - Fork 6
239 lines (227 loc) · 10.9 KB
/
autoupdate.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
name: Autoupdate
on:
schedule:
- cron: "0 4 * * 1"
push:
branches: [main]
workflow_dispatch:
jobs:
build:
env:
python-version: '3.11'
strategy:
fail-fast: false
matrix:
include:
- upstream_repo_owner: bgruening
upstream_repo_name: galaxytools
upstream_repo_branch: master
upstream_repo_dir: .
- upstream_repo_owner: galaxycomputationalchemistry
upstream_repo_name: galaxy-tools-compchem
upstream_repo_branch: master
upstream_repo_dir: .
- upstream_repo_owner: galaxyproject
upstream_repo_name: iwc
upstream_repo_branch: main
upstream_repo_dir: workflows/
- upstream_repo_owner: galaxyproject
upstream_repo_name: tools-iuc
upstream_repo_branch: main
upstream_repo_dir: tools/
- upstream_repo_owner: lldelisle
upstream_repo_name: tools-lldelisle
upstream_repo_branch: master
upstream_repo_dir: tools/
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v4
with:
path: autoupdate
- name: Checkout our fork of ${{ matrix.upstream_repo_name }}
uses: actions/checkout@v4
with:
repository: planemo-autoupdate/${{ matrix.upstream_repo_name }}
token: ${{ secrets.PAT }} # So we can push to it
path: tools_repo
fetch-depth: 0 # So we can checkout other branches
- uses: actions/setup-python@v4
with:
python-version: ${{ env.python-version }}
- name: Cache pip dir
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: cache-${{ env.python-version }}
- name: Install planemo
run: |
# pip3 install https://github.com/galaxyproject/planemo/archive/refs/heads/master.zip
pip3 install planemo
- name: Set git credentials
run: |
git config --global user.email "[email protected]"
git config --global user.name "planemo-autoupdate"
- name: Pull from upstream tool repo and push to our fork
run: |
git remote add upstream "https://github.com/${{ matrix.upstream_repo_owner }}/${{ matrix.upstream_repo_name }}"
git pull upstream "${{ matrix.upstream_repo_branch}}"
git push origin "${{ matrix.upstream_repo_branch}}"
working-directory: ./tools_repo
- name: Ensure skip_list file exists
run: touch "${{ matrix.upstream_repo_owner }}_${{ matrix.upstream_repo_name }}_skip_list"
working-directory: ./autoupdate
- name: Run a multi-line script
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: |
errors=""
REPOS=$(planemo ci_find_repos "${{ matrix.upstream_repo_dir }}")
for REPO in $REPOS; do
echo $REPO
# First try to update
echo "Running autoupdate command..."
cd "$REPO"
# This may fail
# To get an exit code of 1 even with tee:
set -o pipefail
planemo autoupdate . --skiplist "${{ github.workspace }}/autoupdate/${{ matrix.upstream_repo_owner }}_${{ matrix.upstream_repo_name }}_skip_list" 2>&1 1> "${{ github.workspace }}/autoupdate.log" | tee "${{ github.workspace }}/autoupdate.err" || errors="${errors}\nCannot autoupdate $REPO, last line of stderr is\n$(tail -n 1 "${{ github.workspace }}/autoupdate.err")"
# Restore
set +o pipefail
# Waiting for https://github.com/galaxyproject/planemo/issues/1478 to be solved
if [[ "$(tail -n 1 "${{ github.workspace }}/autoupdate.log")" = "Could not update"* ]]; then
errors="${errors}\n$(tail -n 1 "${{ github.workspace }}/autoupdate.log")\n$(tail -n 1 "${{ github.workspace }}/autoupdate.err")"
fi
rm -f tool_test_output.* tools.yml
cd -
# Check if it changed something
if git diff --quiet; then
echo "nothing has been updated"
continue
fi
# Get the PR title and change the CHANGELOG.md for workflows
git status
case $REPO in
workflows/*)
TITLE=$(python3 "${{ github.workspace }}/autoupdate/pr_text_iwc.py" --repo "$REPO" --log "${{ github.workspace }}/autoupdate.log" --out "${{ github.workspace }}/body.txt" --changelog "$REPO/CHANGELOG.md")
FILE_TO_CHECK=${REPO}/CHANGELOG.md
;;
*)
TITLE=$(python3 "${{ github.workspace }}/autoupdate/pr_text.py" --repo "$REPO" --log "${{ github.workspace }}/autoupdate.log" --shed "$REPO/.shed.yml" --out "${{ github.workspace }}/body.txt")
FILE_TO_CHECK=${REPO}
;;
esac
# Check if there is a branch/PR that already exists
# We cannot use the -A option of `gh pr list` because the PR author changes with the Personal Access Token used,
# so we filter on the 3rd output field (headRefName, i.e. remote_repository:branch).
if gh_pr_list_out=$(gh pr list --search "head:$REPO" | grep -P "^\d+\t[^\t]+\tplanemo-autoupdate:$REPO\t"); then
OLD_TITLE=$(echo "$gh_pr_list_out" | cut -f 2)
PR_EXISTS=1
PR_NUMBER=$(echo "$gh_pr_list_out" | cut -f 1)
PR_STATUS="OPENED"
echo "PR exists and is opened"
DIFF_BRANCH="origin/$REPO"
else
# Check if a closed PR exists with the same title
if gh_pr_list_out=$(gh pr list --search "is:closed is:unmerged '$TITLE' in:title" | grep -P "^\d+\t[^\t]+\tplanemo-autoupdate:"); then
# Only take the first one
gh_pr_list_out=$(echo "$gh_pr_list_out" | head -n 1)
echo "Found a closed PR with title: $TITLE"
OLD_TITLE=$(echo "$gh_pr_list_out" | cut -f 2)
PR_EXISTS=1
PR_NUMBER=$(echo "$gh_pr_list_out" | cut -f 1)
PR_STATUS="CLOSED"
if [ "$(git branch -a --list "origin/$REPO")" != "" ]; then
# The branch still exists and the PR should be reopened only if there is a new change
DIFF_BRANCH="origin/$REPO"
else
# The branch does not exists anymore so any change should reopen the PR
DIFF_BRANCH="upstream/${{ matrix.upstream_repo_branch }}"
fi
else
OLD_TITLE=
PR_EXISTS=0
PR_NUMBER=
PR_STATUS=
if [ "$(git branch -a --list "origin/$REPO")" != "" ]; then
echo "Branch exists without an open PR - deleting"
git push origin --delete "$REPO"
fi
DIFF_BRANCH="upstream/${{ matrix.upstream_repo_branch }}"
fi
fi
if ! git diff $DIFF_BRANCH --quiet $FILE_TO_CHECK; then
# Check if it is only the date in workflows
if [[ "$REPO" = "workflows/"* ]]; then
if [ $(git diff --numstat $DIFF_BRANCH $FILE_TO_CHECK | cut -f 1) = "1" ]; then
echo "A single line was added, probably the date, nothing is changed"
# clean up for the next repo
git checkout -- .
continue
fi
fi
echo "There are changes"
if [ "$DIFF_BRANCH" = "origin/$REPO" ] ; then
# Check if the last commit of the existing branch was manual
LAST_AUTHOR=$(git log -1 --pretty=format:'%an' $DIFF_BRANCH)
if [ "$LAST_AUTHOR" != "planemo-autoupdate" ]; then
# There were manual commits we do not do anything
gh pr comment "$PR_NUMBER" --body "There are new updates, if you want to integrate them, close the PR and delete branch."
# clean up for the next repo
git checkout -- .
continue
fi
fi
echo "Creating branch and checking out"
git checkout -b "$REPO" "upstream/${{ matrix.upstream_repo_branch }}"
echo "Adding..."
git add .
echo "Committing..."
git commit -m "$TITLE"
echo "Push branch to our fork"
git push --force --set-upstream origin "$REPO"
if [ "$PR_EXISTS" -eq 1 ]; then
# just need to update PR title
if [[ $OLD_TITLE == *\) ]]; then
# older PRs
NEW_TITLE="$OLD_TITLE $(echo "$TITLE" | cut -f 6,7 -d ' ')"
else # newer PRs
NEW_TITLE="$(echo "$OLD_TITLE" | cut --complement -f 7 -d ' ') $(echo "$TITLE" | cut -f 7 -d ' ')"
fi
if [ "$NEW_TITLE" != "$OLD_TITLE" ]; then
# This may fail if for example the PR was opened by someone else like
# https://github.com/bgruening/galaxytools/pull/1353
gh pr edit "$PR_NUMBER" -t "$NEW_TITLE" || errors="${errors}\nCannot change title of PR $PR_NUMBER for $REPO"
fi
# If the PR is closed, we need to reopen it.
if [ "$PR_STATUS" = "CLOSED" ]; then
# This may fail
gh pr reopen "$PR_NUMBER" || errors="${errors}\nCannot reopen PR $PR_NUMBER for $REPO"
fi
# Comment on the PR
gh pr comment "$PR_NUMBER" --body "There are new updates, they have been integrated to the PR, check the file diff."
else # we need to create a PR
echo "Creating a PR..."
gh pr create --base "${{ matrix.upstream_repo_branch }}" --head "planemo-autoupdate:$REPO" --title "$TITLE" --repo "${{ matrix.upstream_repo_owner}}/${{ matrix.upstream_repo_name }}" --body-file "${{ github.workspace }}/body.txt"
fi
else
echo "No changes compared to $DIFF_BRANCH"
# clean up for the next repo
git checkout -- .
fi
done
if [ ! -z "$errors" ]; then
echo "ERRORS OCCURED DURING AUTOUPDATE:"
echo -e $errors
# Write these errors to an issue
body="$(date), there were errors when running autoupdate\n\n$errors"
echo -e $body > body.txt
if gh_issue_list=$(gh issue list --author "gxydevbot" | grep -P "^\d+\t[^\t]+\tAutoupdate errors\t"); then
gh issue comment $(echo "$gh_issue_list" | cut -f 1 | head -n 1) -F body.txt
else
gh issue create -t "Autoupdate errors" -F body.txt
fi
rm body.txt
exit 1
fi
working-directory: ./tools_repo