Skip to content

Commit

Permalink
Merge pull request #12 from urcomputeringpal/set-label-colors
Browse files Browse the repository at this point in the history
Create labels, set their colors and descriptions
  • Loading branch information
jnewland authored Jun 18, 2023
2 parents e9a51bd + ad9a4a0 commit fba1b3a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 8 deletions.
66 changes: 62 additions & 4 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.

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 fba1b3a

Please sign in to comment.