Skip to content

Commit

Permalink
Add support for finding by labels (#46)
Browse files Browse the repository at this point in the history
* added labels input

* fix input

* removed default

* changed prs to let

* added debug info

* apparantly you need to build

* Refactored PR filtering logic in index.js

* fix debug message

* fixed  retrieval of chosen PR

* build

* removed uneccesary debug

* ran prettier

* removed additional debug notes

* rename labelList

* renamed prLabels

---------

Co-authored-by: Ben Avrahami <[email protected]>
  • Loading branch information
bentheiii and Ben Avrahami authored Mar 24, 2024
1 parent a36c13f commit 57ba229
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inputs:
description: 'The direction of the sort. Can be either `asc` or `desc`.'
default: 'desc'
required: false
labels:
description: 'A comma-seperated list of label names, all of which must be present for the PR to be returned'
required: false

outputs:
number:
Expand Down
17 changes: 14 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30828,6 +30828,7 @@ const main = async () => {
const sort = core.getInput('sort')
const direction = core.getInput('direction')
const repoString = core.getInput('repo')
const labels = core.getInput('labels')

let repoObject
if (repoString) {
Expand Down Expand Up @@ -30860,9 +30861,19 @@ const main = async () => {
const octokit = github.getOctokit(token)

const res = await octokit.rest.pulls.list(query)
const pr = author
? res.data.length && res.data.filter(pr => pr.user.login === author)[0]
: res.data.length && res.data[0]
let prs = res.data
if (author) {
prs = prs.filter(pr => pr.user.login === author)
}
if (labels) {
const labelNames = labels.split(',')
prs = prs.filter(pr => {
const prLabelNames = pr.labels.map(label => label.name)
return labelNames.every(label => prLabelNames.includes(label))
})
}

const pr = prs.length && prs[0]

core.debug(`pr: ${JSON.stringify(pr, null, 2)}`)
core.setOutput('number', pr ? pr.number : '')
Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const main = async () => {
const sort = core.getInput('sort')
const direction = core.getInput('direction')
const repoString = core.getInput('repo')
const labels = core.getInput('labels')

let repoObject
if (repoString) {
Expand Down Expand Up @@ -44,9 +45,19 @@ const main = async () => {
const octokit = github.getOctokit(token)

const res = await octokit.rest.pulls.list(query)
const pr = author
? res.data.length && res.data.filter(pr => pr.user.login === author)[0]
: res.data.length && res.data[0]
let prs = res.data
if (author) {
prs = prs.filter(pr => pr.user.login === author)
}
if (labels) {
const labelNames = labels.split(',')
prs = prs.filter(pr => {
const prLabelNames = pr.labels.map(label => label.name)
return labelNames.every(label => prLabelNames.includes(label))
})
}

const pr = prs.length && prs[0]

core.debug(`pr: ${JSON.stringify(pr, null, 2)}`)
core.setOutput('number', pr ? pr.number : '')
Expand Down

0 comments on commit 57ba229

Please sign in to comment.