Skip to content

Commit

Permalink
Create Labels, set their colors and descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewland committed Jun 18, 2023
1 parent e9a51bd commit a9cc95d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
68 changes: 67 additions & 1 deletion src/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function computeLabels(
return labels
}

export async function writeLabels(
export async function labelPR(
labels: LabelMutations,
prNumber: number,
context: Context,
Expand All @@ -79,3 +79,69 @@ export async function writeLabels(
}
}
}

// an exported function called ensureLabels that takes in a labelPrefix
// string, a context object, and an octokit object this function either creates
// or updates each of the labels that the computeLabels function may return to
// assign them colors that reflect their meaning (to me at least).
export async function ensureLabels(
labelPrefix: string,
context: Context,
octokit: OctokitInstance
): Promise<void> {
const labels = [
{
name: `${labelPrefix} updates`,
color: 'ffcc00',
description: `This PR will update Pulumi resources in ${labelPrefix}.`
},
{
name: `${labelPrefix} replacements`,
color: 'ff9900',
description: `This PR will replace Pulumi resources in ${labelPrefix}.`
},
{
name: `${labelPrefix} creations`,
color: '0000ff',
description: `This PR will create Pulumi resources in ${labelPrefix}.`
},
{
name: `${labelPrefix} deletions`,
color: 'ff3300',
description: `This PR will delete Pulumi resources in ${labelPrefix}.`
},
{
name: `${labelPrefix} noop`,
color: '00cc00',
description: `This PR is a ${labelPrefix} Pulumi noop.`
}
]

for (const label of labels) {
try {
await octokit.rest.issues.createLabel({
...context.repo,
...label
})
} catch (error: unknown) {
if (error instanceof Error && 'status' in error && error.status !== 422) {
throw error
}
// update the label's color if it already exists
try {
await octokit.rest.issues.updateLabel({
...context.repo,
...label
})
} catch (updateError: unknown) {
if (
updateError instanceof Error &&
'status' in updateError &&
updateError.status !== 422
) {
throw updateError
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import {context, getOctokit} from '@actions/github'
import {requestLog} from '@octokit/plugin-request-log'
import {retry} from '@octokit/plugin-retry'
import {computeLabels, writeLabels} from './labels'
import {computeLabels, ensureLabels, labelPR} from './labels'

async function run(): Promise<void> {
try {
Expand All @@ -23,7 +23,8 @@ async function run(): Promise<void> {
}

const labels = await computeLabels(pulumiOutput, labelPrefix)
await writeLabels(labels, prNumber, context, octokit)
await ensureLabels(labelPrefix, context, octokit)
await labelPR(labels, prNumber, context, octokit)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
Expand Down

0 comments on commit a9cc95d

Please sign in to comment.