Skip to content

Commit

Permalink
feat: support inheriting and adding labels (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugods committed Apr 29, 2024
1 parent 1d23a98 commit 4fb161c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 24 additions & 6 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.

25 changes: 18 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { PullRequest } from '@octokit/webhooks-types'
import { createPullRequest } from './pr'
import { Label, PullRequest } from '@octokit/webhooks-types'
import { addLabels, createPullRequest } from './pr'
import {
cherryPickChangesToNewBranch,
configureGitUser,
Expand Down Expand Up @@ -41,11 +41,11 @@ export async function run(): Promise<void> {
// Use the author of the original PR as the assignee of the cherry-pick
prAssignee = mergedPR.user.login
}
// const labelsInput = core.getInput('pr-labels')
// let prLabels: string[] = []
// if (labelsInput !== '') {
// prLabels = labelsInput.split(',')
// }
const labelsInput = core.getInput('pr-labels')
let addedLabels: string[] = []
if (labelsInput !== '') {
addedLabels = labelsInput.split(',')
}

// GH Actions bot email address
// https://github.com/orgs/community/discussions/26560#discussioncomment-3252339
Expand Down Expand Up @@ -101,6 +101,17 @@ export async function run(): Promise<void> {
newBranchName,
targetBranch
)

const inheritedLabels = mergedPR.labels.map(
(label: Label) => label.name
)

await addLabels(
githubToken,
resultPrNumber,
inheritedLabels.concat(addedLabels)
)

core.setOutput('pr-number', resultPrNumber)
} catch (error) {
// Fail the workflow run if an error occurs
Expand Down
20 changes: 20 additions & 0 deletions src/pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ export async function createPullRequest(
})
return resultPr.data.number
}

export async function addLabels(
githubToken: string,
issueNumber: number,
labelsToAdd: string[]
): Promise<void> {
if (labelsToAdd.length === 0) {
console.info('Skipping addition of labels, as none are needed')
return
}
const octokit = github.getOctokit(githubToken)
const repoOwner = github.context.repo.owner
const repoName = github.context.repo.repo
await octokit.rest.issues.addLabels({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
labels: labelsToAdd
})
}

0 comments on commit 4fb161c

Please sign in to comment.