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: support inheriting and adding labels #2

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
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
})
}
Loading