-
Notifications
You must be signed in to change notification settings - Fork 250
170 lines (146 loc) · 6.17 KB
/
pr-lint.yaml
File metadata and controls
170 lines (146 loc) · 6.17 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Lint Pull Request Title
on:
pull_request:
branches:
- main
types:
- opened
- edited
- synchronize
jobs:
lint-pr-title:
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Lint PR title
id: lint-pr-title
uses: amannn/action-semantic-pull-request@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# chore(deps): bump <some dependency>
# ^ ^ ^
# | | | __Subject
# | |_______ Scope
# |____________ Type
# Configure which types are allowed (newline-delimited).
# These are regex patterns auto-wrapped in `^ $`.
# Default: https://github.com/commitizen/conventional-commit-types
types: |
ci
chore
docs
feat
fix
refactor
test
# Configure which scopes are allowed (newline-delimited).
# These are regex patterns auto-wrapped in `^ $`.
scopes: |
.*
# Configure scope as optional.
requireScope: false
# Configure additional validation for the subject based on a regex.
# This ensures the subject doesn't start with an uppercase character.
subjectPattern: ^(?![A-Z]).+$
# If `subjectPattern` is configured, you can use this property to override
# the default error message that is shown when the pattern doesn't match.
# The variables `subject` and `title` can be used within the message.
subjectPatternError: |
The subject "{subject}" found in the pull request title "{title}"
didn't match the configured pattern. Please ensure that the subject
doesn't start with an uppercase character.
# Continue on error so we can leave a comment on the PR to give the author some guidance.
continue-on-error: true
# Delete previous lint failure comments if linting succeeded.
- name: Delete previous lint failure comments
if: steps.lint-pr-title.outcome == 'success'
uses: actions/github-script@v8
with:
script: |
const prNumber = context.payload.pull_request.number;
// Get all comments on this PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
// Find all bot comments that are lint failure comments
const lintFailureComments = comments.filter(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('PR Title Lint Failed')
);
// Delete each lint failure comment
for (const comment of lintFailureComments) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
console.log(`Deleted lint failure comment ${comment.id}`);
}
# Add a comment to the PR to provide guidance if the linting failed.
- name: Leave guidance comment
if: steps.lint-pr-title.outcome == 'failure'
uses: actions/github-script@v8
with:
script: |
const prNumber = context.payload.pull_request.number;
const prTitle = context.payload.pull_request.title;
const comment = `## PR Title Lint Failed ❌
**Current Title:** \`${prTitle}\`
Your PR title doesn't follow the expected format. Please update your PR title to follow one of these patterns:
### Conventional Commits Format:
- \`feat: add new feature\` - for new features
- \`fix: resolve bug in component\` - for bug fixes
- \`docs: update README\` - for documentation changes
- \`refactor: improve code structure\` - for refactoring
- \`test: add unit tests\` - for test additions
- \`chore: remove dead code\` - for maintenance tasks
- \`chore(deps): update dependencies\` - for updating dependencies
- \`ci: update build pipeline\` - for CI/CD changes
### Guidelines:
- Use lowercase for the type and description
- Keep the description concise but descriptive
- Use imperative mood (e.g., "add" not "adds" or "added")
- Don't end with a period
### Examples:
- ✅ \`feat(windows): add secure TLS bootstrapping for Windows nodes\`
- ✅ \`fix: resolve kubelet certificate rotation issue\`
- ✅ \`docs: update installation guide\`
- ❌ \`Added new feature\`
- ❌ \`Fix bug.\`
- ❌ \`Update docs\`
Please update your PR title and the lint check will run again automatically.`;
// Check if we already commented on this PR to avoid spam
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('PR Title Lint Failed')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: comment
});
}
# If linting failed, fail the workflow run.
- name: Fail the workflow
if: steps.lint-pr-title.outcome == 'failure'
run: exit 1