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

dependabotNudge: add elected maintainers #668

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actions/dependabot-nudge/action.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = async ({ github, context, inputs, actionPath, core, debug = fal
minlevel = 'high'
}

const messages = await dependabotNudge({ debug, org: context.repo.owner, github, minlevel, githubToSlack })
const messages = await dependabotNudge({ debug, org: context.repo.owner, github, minlevel, githubToSlack, actionPath })

for (const message of messages) {
try {
Expand Down
46 changes: 27 additions & 19 deletions src/dependabotNudge.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export default async function dependabotNudge ({
skipHotwords = ['dos', 'denial of service', 'redos', 'denial-of-service', 'memory explosion', 'inefficient regular expression', 'regular expression complexity'],
defaultContact = ['yan'],
githubToSlack = {},
singleOutputMessage = false
singleOutputMessage = false,
actionPath
}) {
const { default: getConfig } = await import(`${actionPath}/src/getConfig.js`)
const { default: getProperties } = await import(`${actionPath}/src/getProperties.js`)

if (!github && githubToken) {
const { Octokit } = await import('octokit')

Expand Down Expand Up @@ -76,26 +80,26 @@ export default async function dependabotNudge ({
type: 'all'
})).filter(r => r.archived === false).filter(r => r.disabled === false)

const properties = await github.paginate('GET /orgs/{org}/properties/values', {
org,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})

// hash out of properties array
const props = {}
for (const prop of properties) {
props[prop.repository_name] = prop.properties.reduce((obj, item) => {
obj[item.property_name] = item.value
return obj
}, {})
}

// get dependabot alerts for each repository
for (const repo of repos) {
if (debug) { console.log(`scanning repo ${repo.name} in org ${org}`) }

const config = await getConfig({ owner: org, repo: repo.name, path: '.github/security-action.json', debug, github })
const props = await getProperties({ owner: org, repo: repo.name, debug, github, prefix: 'security_action_' })

const options = Object.assign({
elected_maintainers: ''
}, config, props)

// elected maintainer is a string of comma separated github usernames map.
// E.g "original_maintainer_1:elected_maintaner_1,original_maintainer_2:elected_maintaner_2"
// split it and convert to object
options.elected_maintainers = options.elected_maintainers.split(/\s*,\s*/).reduce((obj, item) => {
const [original, elected] = item.split(/\s*:\s*/)
obj[original] = elected
return obj
}, {})

if (skipRepositories.includes(repo.name)) {
continue
}
Expand All @@ -114,8 +118,12 @@ export default async function dependabotNudge ({
.filter(a => a.security_vulnerability?.first_patched_version?.identifier)

// get property values for this repository
const prop = props[repo.name] || { properties: {} }
const maintainers = (prop.maintainers || '').toLowerCase().split(',').filter(Boolean).map(m => githubToSlack[m] ? githubToSlack[m] : `@${m}`) || []
let maintainers = (props.maintainers || '').toLowerCase().split(',').filter(Boolean)
.map(m => options.elected_maintainers[m] || m)
.map(m => githubToSlack[m] ? githubToSlack[m] : `@${m}`) || []

// remove duplicates
maintainers = Array.from(new Set(maintainers))

if (alerts.length > 0) {
if (debug) { console.log(`alerts len: ${alerts.length}`) }
Expand Down