forked from FreeCAD/FreeCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
178 lines (172 loc) · 8.69 KB
/
sub_prepare.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
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2023 0penBrain. *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
# This is the pre-check workflow for CI of FreeCAD.
# It aims at running some basic checks about the workflow run ...
# ... and gathering some data needed for the next steps.
name: Prepare
on:
workflow_call:
inputs:
artifactBasename:
type: string
required: true
dontFailOnOldRebase:
default: true
type: boolean
required: false
maxRebaseHours:
default: "48"
type: string
required: false
dontFailOnPrecommit:
default: true
type: boolean
required: false
outputs:
reportFile:
value: ${{ jobs.Prepare.outputs.reportFile }}
changedFiles:
value: ${{ jobs.Prepare.outputs.changedFiles }}
changedPythonFiles:
value: ${{ jobs.Prepare.outputs.changedPythonFiles }}
changedCppFiles:
value: ${{ jobs.Prepare.outputs.changedCppFiles }}
jobs:
Prepare:
env:
isPR: ${{ github.event_name == 'pull_request' }}
isPush: ${{ github.event_name == 'push' }}
logdir: /tmp/logs/
reportdir: /tmp/report/
reportfilename: ${{ inputs.artifactBasename }}-report.md
runs-on: ubuntu-latest
defaults:
run:
shell: bash
outputs:
reportFile: ${{ steps.Init.outputs.reportFile }}
changedFiles: ${{ steps.Output.outputs.changedFiles }}
changedPythonFiles: ${{ steps.Output.outputs.changedPythonFiles }}
changedCppFiles: ${{ steps.Output.outputs.changedCppFiles }}
steps:
- name: Make needed directories, files and initializations
id: Init
run: |
mkdir -p ${{ env.logdir }}
mkdir -p ${{ env.reportdir }}
touch ${{ env.logdir }}changedFiles.lst ${{ env.logdir }}changedCppFiles.lst ${{ env.logdir }}changedPythonFiles.lst
echo "reportFile=${{ env.reportfilename }}" >> $GITHUB_OUTPUT
- name: Determine base and head SHA in case of PR
if: env.isPR == 'true'
run: |
baseSha=${{ github.event.pull_request.base.sha }}
headSha=${{ github.event.pull_request.head.sha }}
echo "baseSha=$baseSha" >> $GITHUB_ENV
echo "headSha=$headSha" >> $GITHUB_ENV
echo "This CI run is performed on a Pull Request" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "Base SHA is $baseSha, Head SHA is $headSha" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
- name: Check if PR has been recently rebased
if: env.isPR == 'true'
continue-on-error: ${{ inputs.dontFailOnOldRebase }}
run: |
baseDate=$(curl -H "Accept: application/vnd.github+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/commits/$baseSha | jq -r '.commit.committer.date')
dateDiff=$(( ( $(date +%s) - $(date -d $baseDate +%s) ) / 3600 ))
echo "Pull request is based on a $dateDiff hour-old commit" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
# Exit the step with appropriate code
if [ $dateDiff -gt ${{ inputs.maxRebaseHours }} ]
then
echo ":warning: Pull request should be rebased" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
exit 1
fi
- name: Determine base and head SHA in case of push
if: env.isPush == 'true'
run: |
baseSha=${{ github.event.before }}
headSha=${{ github.event.after }}
echo "headSha=$headSha" >> $GITHUB_ENV
if [ $baseSha -eq 0 ]
then
echo "This CI run is performed on a Push that created a new branch : files changed will be ignored" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "Head SHA is $headSha" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "isPush='false'" >> $GITHUB_ENV
else
echo "This CI run is performed on a Push" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "Base SHA is $baseSha, Head SHA is $headSha" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
echo "baseSha=$baseSha" >> $GITHUB_ENV
fi
- name: Prepare minimal local clone for subsequent operations
run: |
set -x
git clone --filter=blob:none --no-checkout $GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git
- name: Get number of commits in the changeset
if: env.isPR == 'true' || env.isPush == 'true'
run: |
set -x
cd $(basename "$GITHUB_REPOSITORY")
commitCnt=$(git log $baseSha..$headSha --oneline | wc -l)
echo "Changeset is composed of $commitCnt commit(s)" | tee -a ${{env.reportdir}}${{ env.reportfilename }}
- name: Get files modified in changeset
if: env.isPR == 'true' || env.isPush == 'true'
run: |
set -x
cd $(basename "$GITHUB_REPOSITORY")
git diff $baseSha...$headSha --name-only --diff-filter=d | cut -f 1 > ${{ env.logdir }}changedFiles.lst
grep -E '\.(py|py3)"' ${{ env.logdir }}changedFiles.lst > ${{ env.logdir }}changedPythonFiles.lst || true
grep -E '\.(c|c\+\+|cc|cpp|cu|cuh|cxx|h|h\+\+|hh|hpp|hxx)"' ${{ env.logdir }}changedFiles.lst > ${{ env.logdir }}changedCppFiles.lst || true
# Write the report
echo "::group::Modified files in changeset (removed files are ignored) :" ; cat ${{ env.logdir }}changedFiles.lst ; echo "::endgroup::"
echo "<details><summary>Modified files (removed files are ignored):</summary>" >> ${{env.reportdir}}${{ env.reportfilename }}
cat ${{ env.logdir }}changedFiles.lst >> ${{env.reportdir}}${{ env.reportfilename }}
echo "</details>" >> ${{env.reportdir}}${{ env.reportfilename }}
echo "" >> ${{env.reportdir}}${{ env.reportfilename }}
- name: Running pre-commit
continue-on-error: ${{ inputs.dontFailOnPrecommit }}
run: |
set -x
sudo apt-get install -y --no-install-recommends pre-commit
cd $(basename "$GITHUB_REPOSITORY")
IFS=$'\n' files=($(cat ${{ env.logdir }}changedFiles.lst))
git config gc.auto 0
git fetch --depth=1 origin $GITHUB_REF
git checkout $headSha -- .pre-commit-config.yaml ${files[@]}
pre-commit run -a
- name: Transmitting outputs
id: Output
run: |
echo "changedFiles=$(cat ${{ env.logdir }}changedFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
echo "changedPythonFiles=$(cat ${{ env.logdir }}changedPythonFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
echo "changedCppFiles=$(cat ${{ env.logdir }}changedCppFiles.lst | xargs -I{file} echo -n "\"{file}\" ")" >> $GITHUB_OUTPUT
- name: Upload logs
if: always()
uses: actions/upload-artifact@v3
with:
name: ${{ inputs.artifactBasename }}-Logs
path: |
${{ env.logdir }}
- name: Upload report
if: always()
uses: actions/upload-artifact@v3
with:
name: ${{ env.reportfilename }}
path: |
${{env.reportdir}}${{ env.reportfilename }}