Skip to content

Commit

Permalink
feat: Add support for github user mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Manouvrier committed Jul 17, 2023
1 parent 67a2859 commit 40786bc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This action set the assignees on a PR based of a CODEOWNERS file.
| ----------------------- | ---------------------------------------------------- | -------- |
| `github-token` | Auth token with permissions to label PR | `true` |
| `codewatchers-filename` | Filename of codewatchers file. Default: CODEWATCHERS | `false` |
| `github-user-mappings` | Optional user to github user mappings | `false` |

## Usage

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
required: false
description: 'Filename of codewatchers file.'
default: 'CODEWATCHERS'
github-user-mappings:
required: false
description: 'Optional list of mappings from codewatchers to github users. Delimited by colon.'
runs:
using: 'node16'
main: 'dist/index.js'
20 changes: 9 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

31 changes: 11 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,26 @@ async function run(): Promise<void> {
const watchersForChangedFiles = changedFiles.data.flatMap(file =>
watchers.getOwner(file.filename)
)
const uniqueWatchers = new Set(watchersForChangedFiles)
core.debug(`Filtered watchers: ${JSON.stringify([...uniqueWatchers])}`)
const uniqueWatchers = [...new Set(watchersForChangedFiles)]
core.debug(`Filtered watchers: ${JSON.stringify(uniqueWatchers)}`)

// Set assignees
const assignees = await octokit.rest.issues.listAssignees({
...github.context.repo,
issue_number: github.context.issue.number,
per_page: 100
})
core.debug(
`Current assignees: ${JSON.stringify(
assignees.data.map(assignee => ({
username: assignee.login,
email: assignee.email
}))
)}`
const mappings = new Map(
core
.getMultilineInput('github-user-mappings')
.map(line => line.split(':') as [string, undefined])
)
core.debug(`Mappings: ${JSON.stringify(Object.fromEntries(mappings))}`)

const uniqueAssignees = assignees.data.filter(
assignee =>
(assignee.email != null && uniqueWatchers.has(assignee.email)) ||
uniqueWatchers.has(assignee.login)
const mappedAssignees = uniqueWatchers.map(
watcher => mappings.get(watcher) ?? watcher
)
core.debug(`Filtered assignees: ${JSON.stringify(uniqueAssignees)}`)
core.debug(`Mapped assignees: ${JSON.stringify(mappedAssignees)}`)

await octokit.rest.issues.addAssignees({
...github.context.repo,
issue_number: github.context.issue.number,
assignees: uniqueAssignees.map(assignee => assignee.login)
assignees: mappedAssignees
})
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
Expand Down

0 comments on commit 40786bc

Please sign in to comment.