Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group pull request capability for dependabot pull requests #1371

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -8,6 +8,7 @@ const {
} = require('./lib/releases')
const { findCommitsWithAssociatedPullRequests } = require('./lib/commits')
const { sortPullRequests } = require('./lib/sort-pull-requests')
const { groupPullRequests } = require('./lib/group-pull-requests')
const { log } = require('./lib/log')
const core = require('@actions/core')
const { runnerIsActions } = require('./lib/utils')
Expand Down Expand Up @@ -181,8 +182,10 @@ module.exports = (app, { getRouter }) => {
config,
})

const groupedMergedPullRequests = groupPullRequests(mergedPullRequests)

const sortedMergedPullRequests = sortPullRequests(
mergedPullRequests,
groupedMergedPullRequests,
config['sort-by'],
config['sort-direction']
)
Expand Down
54 changes: 54 additions & 0 deletions lib/group-pull-requests.js
@@ -0,0 +1,54 @@
const compareVersions = require("compare-versions");

const groupPullRequests = (pullRequests) => {

const dependabotPRs = {}
const otherPRs = []

const bumpVersionRegExp = new RegExp("Bump ([^\S]+) from ([^\S]+) to ([^\S]+)", "g");

// group all matching dependabot PRs
for (let i in pullRequests) {
const currentPR = pullRequests[i]

if (currentPR.title.match(bumpVersionRegExp)) {
const match = [...pullRequests[i].title.matchAll(bumpVersionRegExp)][0]

const artifact = match[1]
const fromVersion = match[2]
const toVersion = match[3]

if (dependabotPRs[artifact]) {
const prevDependabotPR = dependabotPRs[artifact]
dependabotPRs[artifact] = {
from: (compareVersions(fromVersion, prevDependabotPR.from) < 0) ? fromVersion : prevDependabotPR.from,
to: (compareVersions(toVersion, prevDependabotPR.to) > 0) ? toVersion : prevDependabotPR.to,
pullRequests: [...prevDependabotPR.pullRequests, currentPR.number],
// use the latest PR for creating the final changelog entry
pr: (compareVersions(toVersion, prevDependabotPR.to) > 0) ? currentPR : prevDependabotPR.pr
}
} else {
dependabotPRs[artifact] = {
from: fromVersion,
to: toVersion,
pullRequests: [currentPR.number],
pr: currentPR
}
}
} else {
otherPRs.push(currentPR)
}
}

// reconstruct all PRs
const result = [...otherPRs]
for (const [key, value] of Object.entries(dependabotPRs)) {
const pr = value.pr
pr.title = `Bump ${key} from ${value.from} to ${value.to}`
pr.number = value.pullRequests
result.push(pr)
}
return result
}

exports.groupPullRequests = groupPullRequests
6 changes: 6 additions & 0 deletions lib/template.js
Expand Up @@ -12,6 +12,12 @@ const template = (string, object, customReplacers) => {
let result
if (object[k] === undefined || object[k] === null) {
result = k
} else if (Array.isArray(object[k])) {
// FIXME: this is basically a hack to support changelog entries to refer to multiple PRs
// we assume that a list of values refers to PR numbers. If somebody has a better
// idea how to handle that in a clean way, i.e. to support the change template for
// each individual PR number.
result = object[k].map(item => `${item}`).join(',#')
} else if (typeof object[k] === 'object') {
result = template(object[k].template, object[k])
} else {
Expand Down
329 changes: 329 additions & 0 deletions test/fixtures/__generated__/graphql-commits-dependabot.json

Large diffs are not rendered by default.