Skip to content

Commit

Permalink
fix: add labeling function
Browse files Browse the repository at this point in the history
  • Loading branch information
bonyuta0204 committed Dec 2, 2023
1 parent 5b574a4 commit 00b56d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/auto-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ jobs:
target-branch: "release/v1"
title: "Main to Release v1"
body: "main to Release v1"
lables: "release, test"
20 changes: 17 additions & 3 deletions src/github-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { getOctokit } from '@actions/github'

type OctoKit = ReturnType<typeof getOctokit>

export type PullRequestCreateParam = Parameters<
OctoKit['rest']['pulls']['create']
>[number]
type PullRequestCreateParam = Parameters<OctoKit['rest']['pulls']['create']>[0]

type issueAddLabelsParam = Parameters<OctoKit['rest']['issues']['addLabels']>[0]

/** Pull Request response in list API */
export type PullRequestItem = Awaited<
Expand Down Expand Up @@ -48,3 +48,17 @@ export async function createPullRequest(
setFailed(`Error creating pull request: ${error.message}`)
}
}

/** Add labels to pull request */
export async function addLabelsToPullRequest(
octokit: OctoKit,
param: issueAddLabelsParam
) {
debug(`Adding labels to pull request: ${JSON.stringify(param)}`)
try {
const response = await octokit.rest.issues.addLabels(param)
return response.data
} catch (error: any) {
setFailed(`Error adding labels to pull request: ${error.message}`)
}
}
14 changes: 13 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fetchRemoteBranches, hasCommitsBetween } from './git-util'
import {
PullRequestDetailItem,
PullRequestItem,
addLabelsToPullRequest,
createPullRequest,
fetchExistingPullRequest
} from './github-utils'
Expand Down Expand Up @@ -84,5 +85,16 @@ export async function run(options: OptionParams) {

if (!pullRequest) return

info(`Pull request created: ${pullRequest.html_url}`)
if (options.labels.length > 0) {
/** Adds labels to the pull request */
const labels = await addLabelsToPullRequest(octokit, {
owner,
repo,
issue_number: pullRequest.number,
labels: options.labels
})
if (labels) {
info(`Added labels to pull request: ${labels.map((label) => label.name)}`)
}
}
}

0 comments on commit 00b56d4

Please sign in to comment.