Skip to content

Commit

Permalink
Handle more cases where automations make changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryGSC committed Oct 2, 2024
1 parent 900f465 commit 3b5e13d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ inputs:
required: false
default: '15'
dependabot-fallback:
description: 'The fallback GitHub username to use for standard changes when created by a Dependabot auto-merge.'
description: 'The Net ID to assign changes to when robots make changes (e.g., on a Dependabot auto-merge).'
required: false
outputs:
change-sys-id:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

41 changes: 29 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,7 @@ Last updated on: ${alreadyCreatedRfc.sys_updated_on}`)
process.exit(0)
}

const netId = await getNetIdAssociatedWithGithubUsernameInServicenow(githubUsername).catch(() => {
const isDependabot = (payload.pusher.name === 'dependabot[bot]' || payload.pusher.name === 'dependabot-merge-action[bot]')
if (isDependabot) {
const dependabotFallback = getInput('dependabot-fallback')
if (dependabotFallback !== '') {
return dependabotFallback
} else {
warning(`Could not get dependabot-fallback input. This action will fail.
If you want Dependabot auto-merges to succeed, use that input to define a GitHub username to attach Dependabot changes to.\n`)
}
}

const netId = await determineNetIdToAttributeRfc(githubUsername, templateId).catch(() => {
error(`⚠ An error occurred while getting the Net ID associated with your GitHub username.
Is your GitHub username associated with your profile in ServiceNow?
You can check by going to https://${servicenowHost}/nav_to.do?uri=%2Fsys_user.do%3Fsys_id%3Djavascript:gs.getUserID()%26sysparm_view%3Dess`)
Expand Down Expand Up @@ -158,6 +147,34 @@ async function getRfcIfAlreadyCreated (linkToWorkflowRun) {
return existingRfc
}

async function determineNetIdToAttributeRfc (githubUsername, templateId) {
// If this is some automated change (e.g. on a schedule or from Dependabot)
const isAutomation = (githubUsername === 'byu-oit-bot' || githubUsername === 'github-actions[bot]')
const isDependabot = (githubUsername === 'dependabot[bot]' || githubUsername === 'dependabot-merge-action[bot]')
if (isAutomation || isDependabot) {
// If dependabot-fallback input is provided, attribute the change to that Net ID
const dependabotFallback = getInput('dependabot-fallback')
if (dependabotFallback !== '') {
return dependabotFallback
}

// Otherwise, if an application-specific standard change template was specified (i.e., not the generic one baked into our template repos),
// attribute the change to our GitHub Actions bot user in ServiceNow. A useful template is required so that Ops still knows who to contact
// if something goes wrong with the change.
const genericTemplateInUse = (templateId === 'Codepipeline-Standard-Change')
if (!genericTemplateInUse) {
return 'githubac'
}

warning(`This change appears to have been made by a robot. Ops needs to know who to contact if something goes wrong.
You have two options to fix this:
1) Use a more specific standard change template.
2) Blame a human for this change by providing a Net ID in the dependabot-fallback input.\n`)
}

return getNetIdAssociatedWithGithubUsernameInServicenow(githubUsername)
}

async function getNetIdAssociatedWithGithubUsernameInServicenow (githubUsername) {
const optionsToGetNetId = {
method: 'GET',
Expand Down

0 comments on commit 3b5e13d

Please sign in to comment.