236 lines
8.1 KiB
YAML
236 lines
8.1 KiB
YAML
name: Add Label(s)
|
|
description: adds label(s) to labelable
|
|
inputs:
|
|
gh_token:
|
|
description: gh api access token to use
|
|
required: true
|
|
repository:
|
|
description: the OWNER/REPOSITORY to operate on
|
|
default: ${{ github.repository }}
|
|
issues:
|
|
description: a single or comma separated list of issue numbers
|
|
required: true
|
|
labels:
|
|
description: a single or comma separated list of labels to apply to all listed issues
|
|
required: true
|
|
colors:
|
|
description: |
|
|
A single or comma separated list of colors to create the labels with if needed.
|
|
the list order is the same as `labels`. Missing or blank values (e.g. `FFFFFF,,FFFFFF`) use the `default-color`
|
|
default-color:
|
|
description: default color to create labels with
|
|
default: "#D4C5F9"
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Collect Repo Labels
|
|
id: collect_labels
|
|
shell: bash
|
|
env:
|
|
GH_TOKEN: ${{ inputs.gh_token }}
|
|
REPOSITORY: ${{ inputs.repository }}
|
|
LABELS: ${{ inputs.labels }}
|
|
COLORS: ${{ inputs.colors }}
|
|
DEFAULT_COLOR: ${{ inputs.default-color }}
|
|
run: |
|
|
owner=$(echo "$REPOSITORY" | cut -d '/' -f 1)
|
|
repo=$(echo "$REPOSITORY" | cut -d '/' -f 2)
|
|
query=$(
|
|
jq -nr \
|
|
--arg labels "$LABELS" \
|
|
'
|
|
(reduce ($labels | split (", *"; null) | .[]) as $i ([]; . + [$i])) as $labels
|
|
| "query ($owner: String!, $repo: String!) {\n" +
|
|
" repository(owner: $owner, name: $repo) {\n" +
|
|
" id\n" +
|
|
(
|
|
reduce $labels[] as $i ([0, ""]; [
|
|
.[0] + 1,
|
|
.[1] + (
|
|
" _" + (.[0] | tostring) +
|
|
": label(name: \"" + $i + "\") {\n" +
|
|
" id\n" +
|
|
" name\n"+
|
|
" }\n"
|
|
)
|
|
])
|
|
| .[1]
|
|
)+
|
|
" }\n" +
|
|
"}"
|
|
'
|
|
)
|
|
data=$(
|
|
gh api graphql \
|
|
-f owner="$owner" \
|
|
-f repo="$repo" \
|
|
-f query="$query" \
|
|
| jq -c \
|
|
--arg labels "$LABELS" \
|
|
--arg colors "$COLORS" \
|
|
--arg defaultColor "$DEFAULT_COLOR" \
|
|
'
|
|
. as $in
|
|
| ($labels | split(", *"; null)) as $labels
|
|
| ($colors | split(", *"; null)) as $colors
|
|
| (
|
|
reduce ( [$labels, $colors] | transpose)[] as $i (
|
|
{};
|
|
.[$i[0]] = (
|
|
if ($i[1] and $i[1] != "") then
|
|
$i[1]
|
|
else
|
|
$defaultColor
|
|
end
|
|
| if startswith("#") then .[1:] else . end
|
|
)
|
|
)
|
|
) as $colorMap
|
|
| (
|
|
reduce (
|
|
$in.data.repository[]
|
|
| select( objects | .name as $name | any($labels[]; . == $name ) )
|
|
) as $i ({}; .[$i.name] = {"id": $i.id})
|
|
) as $found
|
|
| (
|
|
reduce (
|
|
$labels[]
|
|
| select( . as $name | $found | has($name) | not )
|
|
) as $i ({}; .[$i] = {"color": $colorMap[$i]})
|
|
) as $missing
|
|
| {
|
|
"repoId": $in.data.repository.id,
|
|
"found": $found,
|
|
"missing": $missing,
|
|
}
|
|
'
|
|
)
|
|
echo "found=$(jq -c '.found' <<< "$data")" >> "$GITHUB_OUTPUT"
|
|
echo "missing=$(jq -c '.missing' <<< "$data")" >> "$GITHUB_OUTPUT"
|
|
echo "repo_id=$(jq -r '.repoId' <<< "$data")" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Collect Item Node IDs
|
|
id: collect_ids
|
|
shell: bash
|
|
env:
|
|
GH_TOKEN: ${{ inputs.gh_token }}
|
|
REPOSITORY: ${{ inputs.repository }}
|
|
ISSUES: ${{ inputs.issues }}
|
|
run: |
|
|
owner=$(echo "$REPOSITORY" | cut -d '/' -f 1)
|
|
repo=$(echo "$REPOSITORY" | cut -d '/' -f 2)
|
|
query=$(
|
|
jq -nr \
|
|
--arg issues "$ISSUES" \
|
|
'
|
|
(reduce ($issues | split (", *"; null) | .[]) as $i ([]; . + [$i])) as $issues
|
|
|
|
|
"query ($owner: String!, $repo: String!) {\n" +
|
|
" repository(owner: $owner, name: $repo) {\n" +
|
|
(
|
|
reduce $issues[] as $i ([0, ""]; [
|
|
.[0] + 1,
|
|
.[1] + (
|
|
" _" + (.[0] | tostring) +
|
|
": issueOrPullRequest(number: " + ($i | tostring) +") {\n" +
|
|
" ... on Issue {\n" +
|
|
" id\n" +
|
|
" }\n" +
|
|
" ... on PullRequest {\n"+
|
|
" id\n"+
|
|
" }\n" +
|
|
" }\n"
|
|
)
|
|
])
|
|
| .[1]
|
|
)+
|
|
" }\n" +
|
|
"}"
|
|
'
|
|
)
|
|
data=$(
|
|
gh api graphql \
|
|
-f owner="$owner" \
|
|
-f repo="$repo" \
|
|
-f query="$query" \
|
|
| jq -c 'reduce .data.repository[].id as $i ([]; . + [$i])'
|
|
)
|
|
echo "issue_ids=$data" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Create Missing Labels
|
|
id: create_missing
|
|
shell: bash
|
|
env:
|
|
GH_TOKEN: ${{ inputs.gh_token }}
|
|
REPO_ID: ${{ steps.collect_labels.outputs.repo_id }}
|
|
EXISTING: ${{ steps.collect_labels.outputs.found }}
|
|
MISSING: ${{ steps.collect_labels.outputs.missing }}
|
|
run: |
|
|
query=$(
|
|
jq -nr \
|
|
--argjson labels "$MISSING" \
|
|
--arg repo "$REPO_ID" '
|
|
"mutation {\n" + (
|
|
reduce ($labels | keys | .[] | [., $labels[.]]) as $i ([0, ""]; [
|
|
.[0] + 1,
|
|
.[1] + (
|
|
" _" + (.[0] | tostring) +
|
|
": createLabel(input: {repositoryId: \"" + $repo +
|
|
"\", name: \"" + $i[0] +
|
|
"\", color: \"" + $i[1].color +
|
|
"\"}) {\n" +
|
|
" clientMutationId\n" +
|
|
" label {\n" +
|
|
" id\n" +
|
|
" name\n" +
|
|
" color\n" +
|
|
" }\n" +
|
|
" }\n"
|
|
)
|
|
])
|
|
| .[1]
|
|
) +
|
|
"}"
|
|
'
|
|
)
|
|
data=$(
|
|
gh api graphql -f query="$query" | jq --argjson existing "$EXISTING" '
|
|
reduce .data[].label as $i ({}; .[$i.name] = {"id": $i.id, "color": $i.color })
|
|
| . + $existing
|
|
'
|
|
)
|
|
label_ids=$(jq -c '[.[].id]' <<< "$data")
|
|
echo "label_ids=$label_ids" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Apply Labels
|
|
id: apply_labels
|
|
shell: bash
|
|
env:
|
|
GH_TOKEN: ${{ inputs.gh_token }}
|
|
ISSUES: ${{ steps.collect_ids.outputs.issue_ids }}
|
|
LABELS: ${{ steps.create_missing.outputs.label_ids }}
|
|
run: |
|
|
query=$(
|
|
jq -nr \
|
|
--argjson labels "$LABELS" \
|
|
--argjson issues "$ISSUES" \
|
|
'
|
|
"mutation {\n" + (
|
|
reduce $issues[] as $i ([0, ""]; [
|
|
.[0] + 1,
|
|
.[1] + (
|
|
" _" + (.[0] | tostring) +
|
|
": addLabelsToLabelable(input: {labelableId: \"" +
|
|
$i + "\", labelIds: " + ($labels | tojson) + "}) {\n" +
|
|
" clientMutationId\n" +
|
|
" }\n"
|
|
)
|
|
])
|
|
| .[1]
|
|
) +
|
|
"}"
|
|
'
|
|
)
|
|
gh api graphql -f query="$query"
|