Skip to content

Commit d849c82

Browse files
guimorgcchanche
andauthored
feat: Add support to excluded-paths (#1522)
* feat: add excluded-paths configuration for release-drafter * test: include case of same file being included and excluded * chore: re-include short circuit to avoid blowing GraphQL budget --------- Co-authored-by: Clément Chanchevrier <[email protected]>
1 parent 5762bb5 commit d849c82

10 files changed

Lines changed: 280 additions & 61 deletions

README.md

Lines changed: 34 additions & 33 deletions
Large diffs are not rendered by default.

dist/index.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190200,12 +190200,11 @@ const findCommitsWithAssociatedPullRequests = async ({
190200190200
historyLimit: config['history-limit'],
190201190201
}
190202190202
const includePaths = config['include-paths']
190203+
const excludePaths = config['exclude-paths']
190203190204
const dataPath = ['repository', 'object', 'history']
190204190205
const repoNameWithOwner = `${owner}/${repo}`
190205190206

190206-
let data,
190207-
allCommits,
190208-
includedIds = {}
190207+
let data, allCommits
190209190208

190210190209
const since = lastRelease
190211190210
? lastRelease.created_at
@@ -190242,6 +190241,7 @@ const findCommitsWithAssociatedPullRequests = async ({
190242190241
throw new Error(validationResult.error.message)
190243190242
}
190244190243

190244+
const includedIds = new Set()
190245190245
if (includePaths.length > 0) {
190246190246
var anyChanges = false
190247190247
for (const path of includePaths) {
@@ -190251,21 +190251,37 @@ const findCommitsWithAssociatedPullRequests = async ({
190251190251
{ ...variables, since: since, path },
190252190252
dataPath
190253190253
)
190254-
const commitsWithPathChanges = _.get(pathData, [...dataPath, 'nodes'])
190254+
const nodes = _.get(pathData, [...dataPath, 'nodes'], [])
190255190255

190256-
includedIds[path] = includedIds[path] || new Set([])
190257-
for (const { id } of commitsWithPathChanges) {
190256+
for (const { id } of nodes) {
190258190257
anyChanges = true
190259-
includedIds[path].add(id)
190258+
includedIds.add(id)
190260190259
}
190261190260
}
190262-
190263190261
if (!anyChanges) {
190264190262
// Short circuit to avoid blowing GraphQL budget
190265190263
return { commits: [], pullRequests: [] }
190266190264
}
190267190265
}
190268190266

190267+
const excludedIds = new Set()
190268+
if (excludePaths.length > 0) {
190269+
for (const path of excludePaths) {
190270+
const pathData = await paginate(
190271+
context.octokit.graphql,
190272+
findCommitsWithPathChangesQuery,
190273+
{ ...variables, since: since, path },
190274+
dataPath
190275+
)
190276+
190277+
const nodes = _.get(pathData, [...dataPath, 'nodes'], [])
190278+
190279+
for (const { id } of nodes) {
190280+
excludedIds.add(id)
190281+
}
190282+
}
190283+
}
190284+
190269190285
if (since) {
190270190286
log({
190271190287
context,
@@ -190295,12 +190311,15 @@ const findCommitsWithAssociatedPullRequests = async ({
190295190311
allCommits = _.get(data, [...dataPath, 'nodes'])
190296190312
}
190297190313

190298-
const commits =
190299-
includePaths.length > 0
190300-
? allCommits.filter((commit) =>
190301-
includePaths.some((path) => includedIds[path].has(commit.id))
190302-
)
190303-
: allCommits
190314+
const commits = allCommits.filter((commit) => {
190315+
if (excludedIds.has(commit.id)) {
190316+
return false
190317+
}
190318+
if (includePaths.length > 0) {
190319+
return includedIds.has(commit.id)
190320+
}
190321+
return true
190322+
})
190304190323

190305190324
const pullRequests = _.uniqBy(
190306190325
commits.flatMap((commit) => commit.associatedPullRequests.nodes),
@@ -190413,6 +190432,7 @@ const DEFAULT_CONFIG = Object.freeze({
190413190432
'exclude-labels': [],
190414190433
'include-labels': [],
190415190434
'include-paths': [],
190435+
'exclude-paths': [],
190416190436
'exclude-contributors': [],
190417190437
'no-contributors-template': 'No contributors',
190418190438
replacers: [],
@@ -191182,6 +191202,10 @@ const schema = (context) => {
191182191202
.items(Joi.string())
191183191203
.default(DEFAULT_CONFIG['include-paths']),
191184191204

191205+
'exclude-paths': Joi.array()
191206+
.items(Joi.string())
191207+
.default(DEFAULT_CONFIG['exclude-paths']),
191208+
191185191209
'exclude-contributors': Joi.array()
191186191210
.items(Joi.string())
191187191211
.default(DEFAULT_CONFIG['exclude-contributors']),

lib/commits.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ const findCommitsWithAssociatedPullRequests = async ({
120120
historyLimit: config['history-limit'],
121121
}
122122
const includePaths = config['include-paths']
123+
const excludePaths = config['exclude-paths']
123124
const dataPath = ['repository', 'object', 'history']
124125
const repoNameWithOwner = `${owner}/${repo}`
125126

126-
let data,
127-
allCommits,
128-
includedIds = {}
127+
let data, allCommits
129128

130129
const since = lastRelease
131130
? lastRelease.created_at
@@ -162,6 +161,7 @@ const findCommitsWithAssociatedPullRequests = async ({
162161
throw new Error(validationResult.error.message)
163162
}
164163

164+
const includedIds = new Set()
165165
if (includePaths.length > 0) {
166166
var anyChanges = false
167167
for (const path of includePaths) {
@@ -171,21 +171,37 @@ const findCommitsWithAssociatedPullRequests = async ({
171171
{ ...variables, since: since, path },
172172
dataPath
173173
)
174-
const commitsWithPathChanges = _.get(pathData, [...dataPath, 'nodes'])
174+
const nodes = _.get(pathData, [...dataPath, 'nodes'], [])
175175

176-
includedIds[path] = includedIds[path] || new Set([])
177-
for (const { id } of commitsWithPathChanges) {
176+
for (const { id } of nodes) {
178177
anyChanges = true
179-
includedIds[path].add(id)
178+
includedIds.add(id)
180179
}
181180
}
182-
183181
if (!anyChanges) {
184182
// Short circuit to avoid blowing GraphQL budget
185183
return { commits: [], pullRequests: [] }
186184
}
187185
}
188186

187+
const excludedIds = new Set()
188+
if (excludePaths.length > 0) {
189+
for (const path of excludePaths) {
190+
const pathData = await paginate(
191+
context.octokit.graphql,
192+
findCommitsWithPathChangesQuery,
193+
{ ...variables, since: since, path },
194+
dataPath
195+
)
196+
197+
const nodes = _.get(pathData, [...dataPath, 'nodes'], [])
198+
199+
for (const { id } of nodes) {
200+
excludedIds.add(id)
201+
}
202+
}
203+
}
204+
189205
if (since) {
190206
log({
191207
context,
@@ -215,12 +231,15 @@ const findCommitsWithAssociatedPullRequests = async ({
215231
allCommits = _.get(data, [...dataPath, 'nodes'])
216232
}
217233

218-
const commits =
219-
includePaths.length > 0
220-
? allCommits.filter((commit) =>
221-
includePaths.some((path) => includedIds[path].has(commit.id))
222-
)
223-
: allCommits
234+
const commits = allCommits.filter((commit) => {
235+
if (excludedIds.has(commit.id)) {
236+
return false
237+
}
238+
if (includePaths.length > 0) {
239+
return includedIds.has(commit.id)
240+
}
241+
return true
242+
})
224243

225244
const pullRequests = _.uniqBy(
226245
commits.flatMap((commit) => commit.associatedPullRequests.nodes),

lib/default-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const DEFAULT_CONFIG = Object.freeze({
1818
'exclude-labels': [],
1919
'include-labels': [],
2020
'include-paths': [],
21+
'exclude-paths': [],
2122
'exclude-contributors': [],
2223
'no-contributors-template': 'No contributors',
2324
replacers: [],

lib/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ const schema = (context) => {
5959
.items(Joi.string())
6060
.default(DEFAULT_CONFIG['include-paths']),
6161

62+
'exclude-paths': Joi.array()
63+
.items(Joi.string())
64+
.default(DEFAULT_CONFIG['exclude-paths']),
65+
6266
'exclude-contributors': Joi.array()
6367
.items(Joi.string())
6468
.default(DEFAULT_CONFIG['exclude-contributors']),

schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
"type": "string"
6060
}
6161
},
62+
"exclude-paths": {
63+
"type": "array",
64+
"default": [],
65+
"items": {
66+
"type": "string"
67+
}
68+
},
6269
"exclude-contributors": {
6370
"type": "array",
6471
"default": [],

test/fixtures/__generated__/graphql-exclude-path-merge-commit.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
template: |
2+
# What's Changed
3+
$CHANGES
4+
name-template: 'v$INPUT_VERSION (Code name: Placeholder)'
5+
tag-template: v$INPUT_VERSION
6+
version-template: $MAJOR.$MINOR.$PATCH
7+
exclude-paths:
8+
- some/path
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
template: |
2+
# What's Changed
3+
$CHANGES
4+
name-template: 'v$INPUT_VERSION (Code name: Placeholder)'
5+
tag-template: v$INPUT_VERSION
6+
version-template: $MAJOR.$MINOR.$PATCH
7+
include-paths:
8+
- src/5.md
9+
exclude-paths:
10+
- src/5.md

0 commit comments

Comments
 (0)