forked from AliceO2Group/O2Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
151 lines (136 loc) · 6.93 KB
/
code-formatting.yml
File metadata and controls
151 lines (136 loc) · 6.93 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
151
name: PR formatting
on: [pull_request_target]
jobs:
clang-format:
name: clang-format
# We need at least 20.04 to install clang-format-11.
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
# We need the history of the dev branch all the way back to where the PR
# diverged. We're fetching everything here, as we don't know how many
# commits back that point is.
fetch-depth: 0
- name: Install prerequisites
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-11 100
sudo update-alternatives --install /usr/bin/git-clang-format git-clang-format /usr/bin/git-clang-format-11 100
- name: Run clang format
id: clang_format
env:
ALIBUILD_GITHUB_TOKEN: ${{secrets.ALIBUILD_GITHUB_TOKEN}}
run: |
set -x
# We need to fetch the other commit.
git fetch origin ${{ github.event.pull_request.base.ref }} \
pull/${{ github.event.pull_request.number }}/head:${{ github.event.pull_request.head.ref }}
# We create a new branch which we will use for the eventual PR.
git config --global user.email "alibuild@cern.ch"
git config --global user.name "ALICE Action Bot"
git checkout -b alibot-cleanup-${{ github.event.pull_request.number }} ${{ github.event.pull_request.head.sha }}
# github.event.pull_request.base.sha is the latest commit on the branch
# the PR will be merged into, NOT the commit this PR derives from! For
# that, we need to find the latest common ancestor between the PR and
# the branch we are merging into.
BASE_COMMIT=$(git merge-base HEAD ${{ github.event.pull_request.base.sha }})
echo "Running clang-format against branch ${{ github.event.pull_request.base.ref }}, with hash ${{ github.event.pull_request.base.sha }}"
COMMIT_FILES=$(git diff --diff-filter d --name-only $BASE_COMMIT)
[ -n "$COMMIT_FILES" ] || { echo "No files to check" ; exit 0; }
RESULT_OUTPUT=$(git-clang-format --commit $BASE_COMMIT --diff --style file $COMMIT_FILES)
for x in $COMMIT_FILES; do
case $x in
*.hxx|*.cc|*.hpp) echo "$x uses non-allowed extension." && exit 1 ;;
*) ;;
esac
done
if [ "$RESULT_OUTPUT" = 'no modified files to format' ] ||
[ "$RESULT_OUTPUT" = 'clang-format did not modify any files' ]
then
echo "clang-format passed."
git push --set-upstream https://alibuild:$ALIBUILD_GITHUB_TOKEN@github.com/alibuild/O2Physics.git :alibot-cleanup-${{ github.event.pull_request.number }} -f || true
echo ::set-output name=clean::true
else
git-clang-format --diff --commit $BASE_COMMIT --style file $COMMIT_FILES |
patch -p1
echo "clang-format failed."
echo "To reproduce it locally please run"
echo -e "\tgit checkout ${{ github.event.pull_request.head.ref }}"
echo -e "\tgit-clang-format --commit $BASE_COMMIT --diff --style file"
echo "Note: using clang-format version $(clang-format --version)"
echo "Opening a PR to your branch with the fixes"
git commit -m 'Please consider the following formatting changes' -a
git show | cat
git fetch https://github.com/AliceO2Group/O2Physics.git pull/${{ github.event.pull_request.number }}/head
git push --set-upstream https://alibuild:$ALIBUILD_GITHUB_TOKEN@github.com/alibuild/O2Physics.git HEAD:refs/heads/alibot-cleanup-${{ github.event.pull_request.number }} -f
echo ::set-output name=clean::false
fi
- name: pull-request
uses: alisw/pull-request@master
with:
source_branch: 'alibuild:alibot-cleanup-${{ github.event.pull_request.number }}'
destination_branch: '${{ github.event.pull_request.head.label }}'
github_token: ${{ secrets.ALIBUILD_GITHUB_TOKEN }}
pr_title: "Please consider the following formatting changes to #${{ github.event.pull_request.number }}"
pr_body: |
This PR cannot be merged as is. You should either run clang-format yourself and update the pull request, or merge this PR in yours.
You can find AliceO2 coding conventions at http://github.com/AliceO2Group/CodingGuidelines.
continue-on-error: true # We do not create PRs if the branch is not there.
- name: Exit with error if the PR is not clean
run: |
if [ "X${{ steps.clang_format.outputs.clean }}" = X ]; then
exit 0
fi
case ${{ steps.clang_format.outputs.clean }} in
true) echo 'PR clean' ; exit 0 ;;
false) echo 'PR not clean' ; exit 1 ;;
esac
copyright:
name: copyright headers
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
# We need the history of the dev branch all the way back to where the PR
# diverged. We're fetching everything here, as we don't know how many
# commits back that point is.
fetch-depth: 0
- name: Check copyright headers
env:
# The expected copyright notice.
COPYRIGHT: |
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
run: |
# Run copyright notice check
COPYRIGHT=$(printf '%s' "$COPYRIGHT") # strip trailing newline
copyright_lines=$(echo "$COPYRIGHT" | wc -l)
base_commit=$(git merge-base HEAD ${{ github.event.pull_request.base.sha }})
have_err=
while read -r file; do
if [ "$(head -n "$copyright_lines" "$file")" != "$COPYRIGHT" ]; then
echo "::error::$file: missing or malformed copyright notice" >&2
have_err=1
fi
done < <(git diff --diff-filter d --name-only "$base_commit" -- '*.cxx' '*.h')
# Tell user what to do in case of copyright notice error
if [ -n "$have_err" ]; then
echo '::error::The files listed above are missing the correct copyright notice.' >&2
echo '::error::Make sure all your source files begin with the following exact lines:' >&2
echo "$COPYRIGHT" >&2
exit 1
fi