Repository navigation
171 lines (157 loc) · 6.14 KB
/
Copy pathpull-request-labels.yml
File metadata and controls
171 lines (157 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Recommend and apply labels for new pull requests.
name: "Pull request labels"
on:
workflow_dispatch:
inputs:
pull_request:
description: "A pull request number"
required: true
type: number
head_sha:
description: "The pull request head SHA at dispatch time"
required: false
type: string
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ inputs.pull_request }}
cancel-in-progress: true
env:
UV_LOCKED: 1
jobs:
labels:
if: github.repository == 'astral-sh/uv' || github.repository == 'astral-sh/uv-dev'
runs-on: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-4' || 'ubuntu-latest' }}
environment:
name: automations
deployment: false
timeout-minutes: 15
permissions:
contents: read
issues: read
pull-requests: read
outputs:
result: ${{ steps.labels.outputs.final-message }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.head_sha || format('refs/pull/{0}/head', inputs.pull_request) }}
path: pull-request
persist-credentials: false
- name: "Collect pull request context"
working-directory: pull-request
run: |
# Remove potential PR-controlled paths for label configuration.
rm -f .pull-request-labels-event.json .pull-request-labels.json
gh pr view "$PULL_REQUEST_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--json number,title,body,author,baseRefName,headRefName,isDraft,labels,files,additions,deletions,changedFiles \
> .pull-request-labels-event.json
gh label list \
--repo "$GITHUB_REPOSITORY" \
--limit 1000 \
--json name,description \
| jq --slurpfile allowed "$GITHUB_WORKSPACE/.github/allowed-pull-request-labels.json" \
'[.[] | select(.name as $label | $allowed[0] | index($label))]' \
> .pull-request-labels.json
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_NUMBER: ${{ inputs.pull_request }}
- name: "Determine pull request labels"
id: labels
uses: openai/codex-action@c05cfd74ad95c626cdf05c6685e62becc3bf7813
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
codex-version: "0.157.1"
working-directory: "${{ github.workspace }}/pull-request"
codex-home: "${{ github.workspace }}/agents/codex"
permission-profile: "pull-request-labels"
safety-strategy: drop-sudo
allow-bot-users: "astral-automations-bot[bot]"
prompt-file: "${{ github.workspace }}/agents/prompts/pull-request-labels.md"
output-schema-file: "${{ github.workspace }}/agents/schemas/pull-request-labels.json"
report:
needs: labels
if: needs.labels.outputs.result != ''
runs-on: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-4' || 'ubuntu-latest' }}
permissions: {}
steps:
- name: "Write label recommendations"
run: printf '%s\n' "$LABEL_RESULT" >> "$GITHUB_STEP_SUMMARY"
env:
LABEL_RESULT: ${{ needs.labels.outputs.result }}
validate:
needs: labels
if: >-
github.repository == 'astral-sh/uv-dev' &&
needs.labels.outputs.result != ''
runs-on: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-4' || 'ubuntu-latest' }}
permissions: {}
outputs:
labels: ${{ steps.validate.outputs.labels }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
persist-credentials: false
- name: "Validate recommended labels"
id: validate
run: |
if ! labels=$(jq --exit-status --compact-output --slurp \
--slurpfile allowed .github/allowed-pull-request-labels.json '
select(length == 1)
| .[0]
| select(type == "object")
| .labels
| select(type == "array" and length <= 3)
| select(length == (unique | length))
| select(all(.[];
type == "string"
and (. as $label | $allowed[0] | index($label) != null)))
' <<< "$LABEL_RESULT"); then
echo "Codex recommended an invalid or disallowed pull request label." >&2
exit 1
fi
echo "labels=$labels" >> "$GITHUB_OUTPUT"
env:
LABEL_RESULT: ${{ needs.labels.outputs.result }}
apply:
needs: validate
if: >-
needs.validate.outputs.labels != '' &&
fromJSON(needs.validate.outputs.labels)[0] != null
runs-on: ${{ github.repository_owner == 'astral-sh' && 'github-ubuntu-24.04-x86_64-2' || 'ubuntu-slim' }}
environment:
name: automations
deployment: false
timeout-minutes: 5
permissions:
id-token: write # for the repository label credential broker
steps:
- name: "Get repository label token"
id: token
uses: astral-sh/github-actions/simple-token-service@eb81cf4d13127c3f9539f621c17e4c65327636c8
with:
exchange-url: ${{ secrets.STS_API_URL }}/exchange
audience: ${{ secrets.STS_API_URL }}
repository: ${{ github.repository }}
permissions: |
pull_requests: write
- name: "Apply recommended labels"
run: |
# POST adds labels without removing any labels already on the pull request.
jq --null-input --argjson labels "$LABELS" '{labels: $labels}' \
| gh api \
--method POST \
"repos/$GITHUB_REPOSITORY/issues/$PULL_REQUEST_NUMBER/labels" \
--input - \
--silent
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
LABELS: ${{ needs.validate.outputs.labels }}
PULL_REQUEST_NUMBER: ${{ inputs.pull_request }}