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

feat: manage org team #254

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
description: 'To get the teams config from another repo, specify the branch'
required: false
default: ''
org-token:
description: 'The organization-level token for reading team memberships'
required: false
outputs:
team_labels:
description: 'JSON array of team labels for the author'
Expand Down
16 changes: 16 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ export async function addLabels(
labels
})
}

export async function getUserTeams(client: GitHub | null): Promise<string[]> {
if (!client) {
return []
}

try {
const response = await client.rest.teams.listForAuthenticatedUser()
return response.data.map(team => `@${team.organization.login}/${team.slug}`)
} catch (error) {
core.warning(
'Failed to fetch user teams. Ensure the org-token has the necessary permissions.'
)
return []
}
}
15 changes: 12 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
getPrAuthor,
getLabelsConfiguration,
addLabels,
createClient
createClient,
getUserTeams
} from './github'

async function run() {
try {
const token = core.getInput('repo-token', {required: true})
const orgToken = core.getInput('org-token', {required: false})
const configPath = core.getInput('configuration-path', {required: true})
const teamsRepo = core.getInput('teams-repo', {required: false})
const teamsBranch = core.getInput('teams-branch', {required: false})
Expand All @@ -28,17 +30,24 @@ async function run() {
}

const client = createClient(token)
const orgClient = orgToken ? createClient(orgToken) : null
const labelsConfiguration: Map<string, string[]> =
await getLabelsConfiguration(
client,
configPath,
teamsRepo !== '' ? {repo: teamsRepo, ref: teamsBranch} : undefined
)

const userTeams = await getUserTeams(orgClient)
const labels: string[] = getTeamLabel(labelsConfiguration, `@${author}`)
const teamLabels: string[] = userTeams
.map(userTeam => getTeamLabel(labelsConfiguration, userTeam))
.flat()

if (labels.length > 0) await addLabels(client, prNumber, labels)
core.setOutput('team_labels', JSON.stringify(labels))
const allLabels = [...new Set([...labels, ...teamLabels])]

if (allLabels.length > 0) await addLabels(client, prNumber, allLabels)
core.setOutput('team_labels', JSON.stringify(allLabels))
} catch (error) {
if (error instanceof Error) {
core.error(error)
Expand Down
Loading