diff --git a/action.yml b/action.yml index 40eff354..e078611c 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/src/github.ts b/src/github.ts index 3b030112..b637093c 100644 --- a/src/github.ts +++ b/src/github.ts @@ -105,3 +105,19 @@ export async function addLabels( labels }) } + +export async function getUserTeams(client: GitHub | null): Promise { + 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 [] + } +} diff --git a/src/main.ts b/src/main.ts index a35861f5..8b0145d1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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}) @@ -28,6 +30,7 @@ async function run() { } const client = createClient(token) + const orgClient = orgToken ? createClient(orgToken) : null const labelsConfiguration: Map = await getLabelsConfiguration( client, @@ -35,10 +38,16 @@ async function run() { 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)