Skip to content

Commit d7e1051

Browse files
committed
ci: decide the pull request type label from its current labels
The title step judged whether a type label was present from the event payload, so a label a maintainer added before a queued run started was not seen and a second type label followed. The decision now lives in dev/ci/pr-type-label.mjs, reads the pull request's current labels through the API, and has a node test that Preflight runs, including the case where the event carried no labels but the pull request already has a type.
1 parent 3576675 commit d7e1051

4 files changed

Lines changed: 126 additions & 32 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ jobs:
8888
- name: Check micro benchmark runner
8989
run: python3 dev/ci/check-benchmark-runner.py
9090

91+
- name: Check pull request type labeling
92+
run: node --test dev/ci/pr-type-label.test.mjs
93+
9194
- name: Install actionlint
9295
run: |
9396
curl -sSfL https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash | bash

‎.github/workflows/label_prs.yml‎

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,44 @@ jobs:
4040
label:
4141
runs-on: ubuntu-slim
4242
steps:
43+
# Checks out the base branch, never the pull request's code: under pull_request_target the
44+
# default ref is the base commit, and only dev/ci/pr-type-label.mjs is read from it.
45+
- uses: actions/checkout@v7
46+
with:
47+
persist-credentials: false
48+
4349
# The type label follows the conventional commit prefix in the title, matching how
44-
# maintainers label by hand. It is added only while the pull request carries no type
45-
# label at all, so a hand-picked replacement stays, and a run cut short by a newer push
46-
# is completed by that push. Dependabot pull requests already carry their label.
50+
# maintainers label by hand. The decision reads the pull request's current labels, not the
51+
# event payload, so a type label a maintainer applied before this run started stays and
52+
# a run cut short by a newer push is completed by that push. Dependabot pull requests
53+
# already carry their label.
4754
- name: Label by title prefix
4855
if: github.actor != 'dependabot[bot]'
4956
uses: actions/github-script@v9
5057
with:
5158
script: |
52-
const title = context.payload.pull_request.title.trim();
53-
const match = /^(feat|fix|perf|test|docs?|chore|refactor|ci|build|deps)(\([^)]*\))?!?:/i.exec(title);
54-
if (!match) {
55-
core.info(`No conventional prefix in "${title}", skipping the type label`);
56-
return;
57-
}
58-
const byPrefix = {
59-
feat: ['enhancement'],
60-
refactor: ['enhancement'],
61-
chore: ['enhancement'],
62-
fix: ['bug'],
63-
perf: ['enhancement', 'performance'],
64-
test: ['enhancement', 'test'],
65-
doc: ['documentation'],
66-
docs: ['documentation'],
67-
ci: ['enhancement', 'build'],
68-
build: ['enhancement', 'build'],
69-
deps: ['dependencies'],
70-
};
71-
const labels = byPrefix[match[1].toLowerCase()];
72-
const typeLabels = new Set(Object.values(byPrefix).flat());
73-
const present = new Set(context.payload.pull_request.labels.map((l) => l.name));
74-
if ([...present].some((l) => typeLabels.has(l))) {
75-
core.info('A type label is already present, leaving it as is');
76-
return;
77-
}
78-
const missing = labels.filter((l) => !present.has(l));
79-
if (missing.length === 0) {
59+
const { typeLabelsToAdd } = await import(
60+
`${process.env.GITHUB_WORKSPACE}/dev/ci/pr-type-label.mjs`
61+
);
62+
const { data: current } = await github.rest.issues.listLabelsOnIssue({
63+
owner: context.repo.owner,
64+
repo: context.repo.repo,
65+
issue_number: context.issue.number,
66+
per_page: 100,
67+
});
68+
const labels = typeLabelsToAdd(
69+
context.payload.pull_request.title,
70+
current.map((label) => label.name),
71+
);
72+
if (labels.length === 0) {
73+
core.info('No type label to add');
8074
return;
8175
}
8276
await github.rest.issues.addLabels({
8377
owner: context.repo.owner,
8478
repo: context.repo.repo,
8579
issue_number: context.issue.number,
86-
labels: missing,
80+
labels,
8781
});
8882
8983
- name: Label by changed paths

‎dev/ci/pr-type-label.mjs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Type label for a pull request from its conventional commit prefix, mirroring how
19+
// maintainers label by hand. Used by .github/workflows/label_prs.yml and tested by
20+
// pr-type-label.test.mjs.
21+
22+
export const LABELS_BY_PREFIX = {
23+
feat: ['enhancement'],
24+
refactor: ['enhancement'],
25+
chore: ['enhancement'],
26+
fix: ['bug'],
27+
perf: ['enhancement', 'performance'],
28+
test: ['enhancement', 'test'],
29+
doc: ['documentation'],
30+
docs: ['documentation'],
31+
ci: ['enhancement', 'build'],
32+
build: ['enhancement', 'build'],
33+
deps: ['dependencies'],
34+
};
35+
36+
const TYPE_LABELS = new Set(Object.values(LABELS_BY_PREFIX).flat());
37+
38+
const PREFIX = /^(feat|fix|perf|test|docs?|chore|refactor|ci|build|deps)(\([^)]*\))?!?:/i;
39+
40+
// Labels to add for `title` given the pull request's current labels. Empty when the title has
41+
// no conventional prefix or when any type label is already present, so a choice a maintainer
42+
// has made stays as it is.
43+
export function typeLabelsToAdd(title, currentLabels) {
44+
const match = PREFIX.exec(title.trim());
45+
if (!match) {
46+
return [];
47+
}
48+
const present = new Set(currentLabels);
49+
if ([...present].some((label) => TYPE_LABELS.has(label))) {
50+
return [];
51+
}
52+
return LABELS_BY_PREFIX[match[1].toLowerCase()].filter((label) => !present.has(label));
53+
}

‎dev/ci/pr-type-label.test.mjs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
import assert from 'node:assert/strict';
19+
import { test } from 'node:test';
20+
import { typeLabelsToAdd } from './pr-type-label.mjs';
21+
22+
test('adds the type label for a conventional title with no labels', () => {
23+
assert.deepEqual(typeLabelsToAdd('fix: keep the entry alive', []), ['bug']);
24+
assert.deepEqual(typeLabelsToAdd('perf(shuffle)!: reuse contexts', ['area:shuffle']), [
25+
'enhancement',
26+
'performance',
27+
]);
28+
assert.deepEqual(typeLabelsToAdd('chore(deps): bump actions', []), ['enhancement']);
29+
});
30+
31+
test('leaves a type label a maintainer already applied, whatever the event carried', () => {
32+
// The event payload showed no labels, but the pull request has one by the time the run starts.
33+
assert.deepEqual(typeLabelsToAdd('fix: keep the entry alive', ['enhancement']), []);
34+
assert.deepEqual(typeLabelsToAdd('perf: reuse contexts', ['performance']), []);
35+
});
36+
37+
test('adds nothing for a title without a conventional prefix', () => {
38+
assert.deepEqual(typeLabelsToAdd('Add Lance contrib build gate', []), []);
39+
assert.deepEqual(typeLabelsToAdd('[draft] ci: pinned image', []), []);
40+
});
41+
42+
test('does not repeat labels that are already present', () => {
43+
assert.deepEqual(typeLabelsToAdd('test: cover struct columns', ['test']), []);
44+
});

0 commit comments

Comments
 (0)