-
Notifications
You must be signed in to change notification settings - Fork 198
207 lines (180 loc) · 7.73 KB
/
linter.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
---
# Copyright (C) 2024 EnterpriseDB
name: Linters
on:
pull_request:
branches:
- master
push:
branches:
- master
schedule:
# Lint code base every Monday 12:00 am. The idea here is to catch possible
# issues that were not detected during the normal development workflow.
- cron: '0 0 * * 1'
workflow_dispatch:
inputs:
source-ref:
description: Source code branch/ref name
default: master
required: true
type: string
env:
SOURCE_REF: ${{ inputs.source-ref || github.ref }}
GITHUB_TOKEN: ${{ secrets.GH_SLONIK }}
jobs:
run-super-linter:
name: Run super linter
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
# To report GitHub Actions status checks
statuses: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ env.SOURCE_REF }}
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
- name: Super-linter
uses: super-linter/super-linter/slim@v7
env:
# To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Linters configuration.
LINTER_RULES_PATH: '.'
# We are not interested in linting the these files:
# * Security workflows provided by the Foundation team, as we are not supposed
# to be changing them.
# * Markdown files under `doc` or `sphinx` directories, which belong to the
# old docs, and are going to be replaced soon.
FILTER_REGEX_EXCLUDE: '\.github/workflows/[^\s]*-scan.ya?ml|(doc|sphinx)/.*\.md'
DOCKERFILE_HADOLINT_FILE_NAME: .hadolint.yaml
GITLEAKS_CONFIG_FILE: .gitleaks.toml
MARKDOWN_CONFIG_FILE: .markdownlint.yml
PYTHON_BLACK_CONFIG_FILE: .python-black
PYTHON_FLAKE8_CONFIG_FILE: tox.ini
PYTHON_ISORT_CONFIG_FILE: .isort.cfg
YAML_CONFIG_FILE: .yamllint.yml
YAML_ERROR_ON_WARNING: false
# On runs triggered by PRs we only lint the added/modified files.
VALIDATE_ALL_CODEBASE: ${{ github.event_name != 'pull_request' }}
# Validate file types used in the Barman repo.
# Bash because of bash scripts.
VALIDATE_BASH: true
VALIDATE_BASH_EXEC: true
# Dockerfile because we might add some of them soon.
VALIDATE_DOCKERFILE_HADOLINT: true
# Validate the own GitHub workflows and actions.
VALIDATE_GITHUB_ACTIONS: true
# Search for leaks in the repository.
VALIDATE_GITLEAKS: true
# Validate all documentation files from the repo.
VALIDATE_MARKDOWN: true
# Validate Python code.
VALIDATE_PYTHON_BLACK: true
VALIDATE_PYTHON_FLAKE8: true
VALIDATE_PYTHON_ISORT: true
# Validate YAML files from workflows and release notes.
VALIDATE_YAML: true
check-conventions:
if: github.event_name == 'pull_request'
name: Check the compliance to contribution guidelines in PRs
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
- name: Set up Python
uses: actions/setup-python@v5
- name: Checkout repo-actions
uses: actions/checkout@v4
with:
repository: EnterpriseDB/repo-actions
token: ${{ env.GITHUB_TOKEN }}
ref: v1
path: ./.github/repo-actions
- name: Check conventions for PRs
id: validate-conventions
shell: python
run: |
import os
import re
import json
import subprocess
# Get the event payload from the GitHub event
with open(os.getenv('GITHUB_EVENT_PATH'), 'r') as f:
event_payload = json.load(f)
# Get the branch name, PR number and PR description from the event payload
pr_number = event_payload['pull_request']['number']
branch_name = event_payload['pull_request']['head']['ref']
pr_description = event_payload['pull_request']['body']
# Initialize a list to collect when conventions are not followed
errors = []
# Check for the conventions for PRs, starting with branch name
# By storing the ticket number in the branch name, we also indirectly check that
# the commit messages and the PR description all reference the same ticket number
match = re.match(r'^dev/bar-(\d+)(?=[^\s]*$)', branch_name, re.IGNORECASE)
if match:
# Get the ticket number from the branch name
ticket_number = match.group(1)
else:
errors.append(f'Branch name {branch_name} does not follow the convention')
ticket_number = None
if ticket_number:
# Check the PR description for ticket reference
if not re.search(rf"^References: BAR-{ticket_number}(?=\.|\s+|$)", pr_description, re.IGNORECASE | re.MULTILINE):
errors.append(f'PR description does not reference BAR-{ticket_number}')
# Check PR commit messages for the ticket reference
# Get the target branch from the PR payload
target_branch = event_payload['pull_request']['base']['ref']
# Get a list of commits that exist only in the source branch but not in the target branch
commit_hashes = subprocess.check_output(
['git', 'rev-list', '--right-only', f'origin/{target_branch}...{branch_name}']
).decode().split()
# Check each commit message for the ticket reference
for commit_hash in commit_hashes:
commit_message = subprocess.check_output(
['git', 'show', '-s', '--format=%b', commit_hash]
).decode()
if not re.search(rf"^References: BAR-{ticket_number}(?=\.|\s+|$)", commit_message, re.IGNORECASE | re.MULTILINE):
errors.append(f'Commit {commit_hash} does not reference BAR-{ticket_number}')
exit_code = 0
comments = []
if len(errors) == 0:
comments.append(
"✅ This PR follows all conventions!"
)
else:
# If the PR fails to follow any of the conventions, report it and mark as failed
comments.append(
"❌ This PR does not follow all conventions!"
)
comments.append(
"\n".join(errors)
)
exit_code = 1
with open(os.environ["GITHUB_OUTPUT"], "a") as fd:
# We need to "encode" line-breaks as literal "\n" values when writing to
# GITHUB_OUTPUT so we avoid "Invalid format" errors in Actions
comments = '\n\n'.join(comments).replace("\n", r"\n")
fd.write(f"comments={comments}\n")
fd.write(f"exit_code={exit_code}\n")
- name: Create or update comment about conventions validation
uses: ./.github/repo-actions/create-or-update-comment
with:
token: ${{ env.GITHUB_TOKEN }}
repo: ${{ github.repository }}
issue-number: ${{ github.event.pull_request.number }}
comment: '# Contributing Conventions Status\n\n${{ steps.validate-conventions.outputs.comments }}'
matches: '^# Contributing Conventions Status\n\n'
- name: Exit with exit code reported by conventions validation
run: |
# shellcheck disable=SC2242
exit ${{ steps.validate-conventions.outputs.exit_code }}