From d4e405a788c9e2bfe78cd808dc3d0231a3850b78 Mon Sep 17 00:00:00 2001 From: Filipe Miranda Date: Wed, 10 May 2023 11:37:48 -0300 Subject: [PATCH] feat: add authors matchconfig option Closes #100 --- src/labeler.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/labeler.ts b/src/labeler.ts index 96e152bc3..0f847d77d 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -6,6 +6,7 @@ import {Minimatch} from 'minimatch'; interface MatchConfig { all?: string[]; any?: string[]; + authors?: string[]; } type StringOrMatchConfig = string | MatchConfig; @@ -71,6 +72,15 @@ function getPrNumber(): number | undefined { return pullRequest.number; } +function getPrAuthor(): string | undefined { + const pullRequest = github.context.payload.pull_request; + if (!pullRequest) { + return undefined; + } + + return pullRequest.user.login; +} + async function getChangedFiles( client: ClientType, prNumber: number @@ -213,6 +223,22 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { return true; } +function checkAuthors(authors: string[]): boolean { + const prAuthor = getPrAuthor(); + if (!prAuthor) { + core.info('Could not get pull request author from context, exiting'); + return false; + } + + if (authors.includes(prAuthor)) { + core.debug(` author ${prAuthor} is on the list`); + return true; + } + + core.debug(` author ${prAuthor} is not on the list`); + return false; +} + function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean { if (matchConfig.all !== undefined) { if (!checkAll(changedFiles, matchConfig.all)) { @@ -226,6 +252,12 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean { } } + if (matchConfig.authors !== undefined) { + if (!checkAuthors(matchConfig.authors)) { + return false; + } + } + return true; }