Skip to content

Commit

Permalink
Add group pull request capability for dependabot pull requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Nov 21, 2023
1 parent 09c613e commit 00b6b1f
Show file tree
Hide file tree
Showing 5 changed files with 612 additions and 174 deletions.
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
52 changes: 52 additions & 0 deletions lib/group-pull-requests.js
@@ -0,0 +1,52 @@
const compareVersions = require("compare-versions");

const groupPullRequests = (pullRequests) => {

const dependabotPRs = {}
const otherPRs = []

const dependabot = new RegExp("Bump ([^\S]+) from ([^\S]+) to ([^\S]+)", "gm");
for (let i in pullRequests) {
const currentPullRequest = pullRequests[i]

if (currentPullRequest.title.match(dependabot)) {
const matches = [...pullRequests[i].title.matchAll(dependabot)]
if (dependabotPRs[matches[0][1]]) {
const curr = dependabotPRs[matches[0][1]]
const from = (compareVersions(matches[0][2], curr.from) < 0) ? matches[0][2] : curr.from
const to = (compareVersions(matches[0][3], curr.to) > 0) ? matches[0][3] : curr.to
curr.number.push(currentPullRequest.number)
dependabotPRs[matches[0][1]] = {
from: from,
to: to,
number: curr.number,
pr: currentPullRequest
}
} else {
dependabotPRs[matches[0][1]] = {
from: matches[0][2],
to: matches[0][3],
number: [currentPullRequest.number],
pr: currentPullRequest
}
}
} else {
otherPRs.push(currentPullRequest)
}
}

const result = [...otherPRs]

for (const [key, value] of Object.entries(dependabotPRs)) {
const item = value.pr
const joined = `Bump ${key} from ${value.from} to ${value.to}`
item.title = joined
item.number = value.number
result.push(item)
}
console.log(result)

return result
}

exports.groupPullRequests = groupPullRequests
2 changes: 2 additions & 0 deletions lib/template.js
Expand Up @@ -12,6 +12,8 @@ const template = (string, object, customReplacers) => {
let result
if (object[k] === undefined || object[k] === null) {
result = k
} else if (Array.isArray(object[k])) {
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.

0 comments on commit 00b6b1f

Please sign in to comment.