mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 19:15:59 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8be4a3473f | ||
|
|
73b1309d56 | ||
|
|
6e0d4641df | ||
|
|
0faeb52441 | ||
|
|
930cef71d2 |
No files matched your search
@@ -0,0 +1,128 @@
|
||||
// Requests a Copilot code review on a pull request. A failed request only
|
||||
// warns: Copilot review is advisory and must never block the PR.
|
||||
//
|
||||
// GitHub answers a request it will not act on (one made by an identity without
|
||||
// a Copilot seat, which includes GITHUB_TOKEN and GitHub Apps when the PR
|
||||
// author has no Copilot access) with a success and no review, so the timeline
|
||||
// is read back to confirm the request was recorded.
|
||||
|
||||
const COPILOT_REVIEWER = "copilot-pull-request-reviewer[bot]";
|
||||
// The timeline names the reviewer by its display login; the reviews API uses
|
||||
// the bot account.
|
||||
const COPILOT_LOGINS = new Set(["Copilot", COPILOT_REVIEWER]);
|
||||
|
||||
module.exports = async ({ github, context, core }) => {
|
||||
const pull = context.payload.pull_request;
|
||||
if (pull.user?.type === "Bot") {
|
||||
core.info(`#${pull.number} was opened by a bot; skipping the Copilot review.`);
|
||||
return;
|
||||
}
|
||||
await requestCopilotReview({ github, core, ...context.repo, prNumber: pull.number });
|
||||
};
|
||||
|
||||
async function requestCopilotReview({ github, core, owner, repo, prNumber }) {
|
||||
const readTimeline = () =>
|
||||
github.paginate(github.rest.issues.listEventsForTimeline, {
|
||||
owner,
|
||||
repo,
|
||||
issue_number: prNumber,
|
||||
per_page: 100,
|
||||
});
|
||||
const { login: requester, stop } = await identify({ github, core });
|
||||
if (stop) return;
|
||||
let before = null;
|
||||
if (requester) {
|
||||
try {
|
||||
before = await readTimeline();
|
||||
} catch (err) {
|
||||
core.warning(
|
||||
`Could not read the timeline of #${prNumber}, so the request goes unconfirmed: ${err.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await github.rest.pulls.requestReviewers({
|
||||
owner,
|
||||
repo,
|
||||
pull_number: prNumber,
|
||||
reviewers: [COPILOT_REVIEWER],
|
||||
});
|
||||
} catch (err) {
|
||||
core.warning(`Could not request a Copilot review on #${prNumber}: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
if (!before) return;
|
||||
let events;
|
||||
try {
|
||||
events = await readTimeline();
|
||||
} catch (err) {
|
||||
core.warning(
|
||||
`Requested a Copilot review on #${prNumber} but could not confirm it: ${err.message}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
const seen = new Set(before.map((event) => event.id));
|
||||
const copilotRequests = events.filter(
|
||||
(event) =>
|
||||
event.event === "review_requested" &&
|
||||
COPILOT_LOGINS.has(event.requested_reviewer?.login)
|
||||
);
|
||||
// Ours is a request this token's account filed that was not there before.
|
||||
if (
|
||||
copilotRequests.some(
|
||||
(event) => event.actor?.login === requester && !seen.has(event.id)
|
||||
)
|
||||
) {
|
||||
core.info(`Requested a Copilot review on #${prNumber}.`);
|
||||
return;
|
||||
}
|
||||
// The latest request or removal decides whether a review is still queued.
|
||||
const last = events
|
||||
.filter(
|
||||
(event) =>
|
||||
(event.event === "review_requested" ||
|
||||
event.event === "review_request_removed") &&
|
||||
COPILOT_LOGINS.has(event.requested_reviewer?.login)
|
||||
)
|
||||
.at(-1);
|
||||
if (last?.event === "review_requested" && !reviewedSince(events, last.created_at)) {
|
||||
core.info(
|
||||
`A Copilot review requested by ${last.actor?.login} is still pending on #${prNumber}.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
core.warning(
|
||||
`GitHub accepted the Copilot review request on #${prNumber} but recorded none; ` +
|
||||
`${requester} likely has no Copilot seat.`
|
||||
);
|
||||
}
|
||||
|
||||
// The account the token belongs to: { login }, {} when it can't be told
|
||||
// (the request goes ahead unconfirmed), or { stop: true } when the token is rejected.
|
||||
async function identify({ github, core }) {
|
||||
try {
|
||||
const { data } = await github.rest.users.getAuthenticated();
|
||||
return { login: data.login };
|
||||
} catch (err) {
|
||||
// An expired or revoked token fails loudly so someone rotates it.
|
||||
if (err.status === 401) {
|
||||
core.setFailed(
|
||||
`COPILOT_REVIEW_TOKEN was rejected (${err.message}); rotate esphome-review-bot's token.`
|
||||
);
|
||||
return { stop: true };
|
||||
}
|
||||
core.warning(
|
||||
`Could not identify the requesting account, so the request goes unconfirmed: ${err.message}`
|
||||
);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function reviewedSince(events, since) {
|
||||
return events.some(
|
||||
(event) =>
|
||||
event.event === "reviewed" &&
|
||||
COPILOT_LOGINS.has(event.user?.login) &&
|
||||
Date.parse(event.submitted_at) >= Date.parse(since)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "request-copilot-review",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test": "node --test tests/*.test.js"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const requestCopilotReview = require('../index');
|
||||
|
||||
const REPO = { owner: 'esphome', repo: 'esphome' };
|
||||
|
||||
const OLD = '2020-01-01T00:00:00Z';
|
||||
const LATER = '2020-01-01T00:05:00Z';
|
||||
// The account the workflow's token belongs to.
|
||||
const REQUESTER = 'esphome-review-bot';
|
||||
// "ours" is a request filed by REQUESTER; anything else stands for the ruleset or a maintainer.
|
||||
const copilotRequest = (id, by, createdAt = LATER) => ({
|
||||
id,
|
||||
event: 'review_requested',
|
||||
actor: { login: by === 'ours' ? REQUESTER : 'someone', type: 'User' },
|
||||
requested_reviewer: { login: 'Copilot' },
|
||||
created_at: createdAt,
|
||||
});
|
||||
const copilotReview = (id, submittedAt, login = 'Copilot') => ({
|
||||
id,
|
||||
event: 'reviewed',
|
||||
user: { login },
|
||||
submitted_at: submittedAt,
|
||||
});
|
||||
const copilotRemoval = (id) => ({
|
||||
id,
|
||||
event: 'review_request_removed',
|
||||
requested_reviewer: { login: 'Copilot' },
|
||||
created_at: LATER,
|
||||
});
|
||||
|
||||
// `before` and `after` are the timeline reads around the request, `timeline` sets both, and
|
||||
// `timelineError` makes that read throw.
|
||||
function makeGithub(opts = {}) {
|
||||
const calls = [];
|
||||
const reads = [
|
||||
{ name: 'before', events: opts.timeline ?? opts.before ?? [] },
|
||||
{ name: 'after', events: opts.timeline ?? opts.after ?? [copilotRequest(1, 'ours')] },
|
||||
];
|
||||
const listEventsForTimeline = () => {};
|
||||
return {
|
||||
calls,
|
||||
github: {
|
||||
paginate: async (fn) => {
|
||||
assert.equal(fn, listEventsForTimeline);
|
||||
const read = reads.shift();
|
||||
assert.ok(read, 'the timeline is read at most twice');
|
||||
if (opts.timelineError === read.name) throw new Error('secondary rate limit');
|
||||
return read.events;
|
||||
},
|
||||
rest: {
|
||||
users: {
|
||||
getAuthenticated: async () => {
|
||||
if (opts.whoamiStatus) {
|
||||
throw Object.assign(new Error('Bad credentials'), { status: opts.whoamiStatus });
|
||||
}
|
||||
return { data: { login: REQUESTER } };
|
||||
},
|
||||
},
|
||||
issues: { listEventsForTimeline },
|
||||
pulls: {
|
||||
requestReviewers: async (args) => {
|
||||
if (opts.fail) throw new Error('Reviews may only be requested from collaborators');
|
||||
calls.push(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function makeCore() {
|
||||
const warnings = [];
|
||||
const infos = [];
|
||||
const failures = [];
|
||||
return {
|
||||
warnings,
|
||||
infos,
|
||||
failures,
|
||||
core: {
|
||||
info: (m) => void infos.push(m),
|
||||
warning: (m) => void warnings.push(m),
|
||||
setFailed: (m) => void failures.push(m),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function run(githubOpts, type = 'User') {
|
||||
const { github, calls } = makeGithub(githubOpts);
|
||||
const { core, infos, warnings, failures } = makeCore();
|
||||
const context = { repo: REPO, payload: { pull_request: { number: 9, user: { type } } } };
|
||||
await requestCopilotReview({ github, context, core });
|
||||
return { calls, infos, warnings, failures };
|
||||
}
|
||||
|
||||
test('a PR event requests a Copilot review', async () => {
|
||||
const { calls, infos, warnings } = await run();
|
||||
assert.deepEqual(calls, [{ ...REPO, pull_number: 9, reviewers: ['copilot-pull-request-reviewer[bot]'] }]);
|
||||
assert.deepEqual(warnings, []);
|
||||
assert.match(infos[0], /Requested a Copilot review on #9/);
|
||||
});
|
||||
|
||||
test('bot PRs are skipped', async () => {
|
||||
const { calls } = await run({}, 'Bot');
|
||||
assert.deepEqual(calls, []);
|
||||
});
|
||||
|
||||
test('a failed request warns instead of failing', async () => {
|
||||
const { warnings, failures } = await run({ fail: true });
|
||||
assert.match(warnings[0], /Could not request a Copilot review on #9/);
|
||||
assert.deepEqual(failures, []);
|
||||
});
|
||||
|
||||
test('an accepted request GitHub never recorded warns', async () => {
|
||||
const { warnings } = await run({ after: [{ id: 1, event: 'labeled' }] });
|
||||
assert.match(warnings[0], /recorded none/);
|
||||
});
|
||||
|
||||
test('a request filed by another account does not confirm ours', async () => {
|
||||
const { warnings } = await run({ after: [copilotRequest(1, 'other'), copilotReview(2, LATER)] });
|
||||
assert.match(warnings[0], /recorded none/);
|
||||
});
|
||||
|
||||
test('an earlier request by the same account does not confirm this one', async () => {
|
||||
const { warnings } = await run({ timeline: [copilotRequest(1, 'ours', OLD), copilotReview(2, LATER)] });
|
||||
assert.match(warnings[0], /recorded none/);
|
||||
});
|
||||
|
||||
test('a Copilot review still pending is logged, not warned', async () => {
|
||||
const { infos, warnings } = await run({ timeline: [copilotReview(1, OLD), copilotRequest(2, 'other')] });
|
||||
assert.deepEqual(warnings, []);
|
||||
assert.match(infos[0], /requested by someone is still pending on #9/);
|
||||
});
|
||||
|
||||
test('a Copilot request that was removed is not pending', async () => {
|
||||
const { warnings } = await run({ timeline: [copilotRequest(1, 'other'), copilotRemoval(2)] });
|
||||
assert.match(warnings[0], /recorded none/);
|
||||
});
|
||||
|
||||
test('a completed review under the bot account login ends the pending request', async () => {
|
||||
const timeline = [
|
||||
copilotRequest(1, 'other', OLD),
|
||||
copilotReview(2, LATER, 'copilot-pull-request-reviewer[bot]'),
|
||||
];
|
||||
const { warnings } = await run({ timeline });
|
||||
assert.match(warnings[0], /recorded none/);
|
||||
});
|
||||
|
||||
test('a failed timeline read after the request warns instead of failing', async () => {
|
||||
const { calls, warnings } = await run({ timelineError: 'after' });
|
||||
assert.equal(calls.length, 1);
|
||||
assert.match(warnings[0], /could not confirm it: secondary rate limit/);
|
||||
});
|
||||
|
||||
test('a failed baseline read still requests the review, unconfirmed', async () => {
|
||||
const { calls, warnings } = await run({ timelineError: 'before' });
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(warnings.length, 1);
|
||||
assert.match(warnings[0], /goes unconfirmed: secondary rate limit/);
|
||||
});
|
||||
|
||||
test('a rejected token fails the run so it gets rotated', async () => {
|
||||
const { calls, failures } = await run({ whoamiStatus: 401 });
|
||||
assert.equal(calls.length, 0);
|
||||
assert.match(failures[0], /COPILOT_REVIEW_TOKEN was rejected/);
|
||||
});
|
||||
|
||||
test('a failed account lookup still requests the review, unconfirmed', async () => {
|
||||
const { calls, warnings, failures } = await run({ whoamiStatus: 502 });
|
||||
assert.equal(calls.length, 1);
|
||||
assert.deepEqual(failures, []);
|
||||
assert.equal(warnings.length, 1);
|
||||
assert.match(warnings[0], /Could not identify the requesting account/);
|
||||
});
|
||||
@@ -74,6 +74,9 @@ workflows:
|
||||
- 'docker/login-action@v4.8.0'
|
||||
- 'docker/setup-buildx-action@v4.4.1'
|
||||
- 'pypa/gh-action-pypi-publish@v1.14.3'
|
||||
'.github/workflows/request-copilot-review.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'actions/github-script@v9.0.0'
|
||||
'.github/workflows/stale.yml': []
|
||||
'.github/workflows/status-check-labels.yml':
|
||||
- 'actions/github-script@v9.0.0'
|
||||
|
||||
@@ -25,3 +25,14 @@ jobs:
|
||||
- name: Run tests
|
||||
working-directory: .github/scripts/auto-label-pr
|
||||
run: npm test
|
||||
|
||||
test-request-copilot-review:
|
||||
name: Test request-copilot-review script
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v7.0.1
|
||||
|
||||
- name: Run tests
|
||||
working-directory: .github/scripts/request-copilot-review
|
||||
run: npm test
|
||||
@@ -0,0 +1,63 @@
|
||||
name: Request Copilot review
|
||||
|
||||
# Requests a Copilot code review when a ready pull request against dev is
|
||||
# opened, reopened or marked ready for review; like the ruleset, not on every
|
||||
# push. The ruleset reviews these too, but it runs the review as the PR author
|
||||
# (or whoever marked it ready), so a contributor without Copilot access gets
|
||||
# none. This runs it as esphome-review-bot, which needs a Copilot seat and
|
||||
# Triage on this repo. COPILOT_REVIEW_TOKEN is an org secret holding its
|
||||
# fine-grained token (Pull requests read and write) and expires: when it is
|
||||
# rejected the run fails, and an org owner signs in as the bot, creates a new
|
||||
# one and replaces the secret.
|
||||
|
||||
on:
|
||||
# Test only: pull_request_target runs from the default branch, so it cannot fire before merge.
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
- ready_for_review
|
||||
pull_request_target:
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
- ready_for_review
|
||||
branches:
|
||||
- dev
|
||||
- copilot-review-test-base
|
||||
|
||||
permissions: {}
|
||||
|
||||
# Overlapping runs could each take the other's request as their own confirmation.
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
request:
|
||||
name: Request Copilot review
|
||||
runs-on: ubuntu-latest
|
||||
# Forks have no review bot token, bot PRs (Dependabot) get no repo secrets,
|
||||
# and drafts are not reviewed.
|
||||
if: >-
|
||||
github.repository == 'esphome/esphome'
|
||||
&& github.event.pull_request.user.type != 'Bot'
|
||||
&& !github.event.pull_request.draft
|
||||
permissions:
|
||||
contents: read # to check out the script below
|
||||
steps:
|
||||
# Checks out the base repo (trusted) only to load the script below,
|
||||
# never the PR's code - this job holds the review bot's token.
|
||||
- name: Checkout scripts
|
||||
uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
sparse-checkout: .github/scripts/request-copilot-review
|
||||
persist-credentials: false
|
||||
|
||||
- name: Request Copilot review
|
||||
uses: actions/github-script@v9.0.0
|
||||
with:
|
||||
github-token: ${{ secrets.COPILOT_REVIEW_TOKEN }}
|
||||
script: |
|
||||
const script = require('./.github/scripts/request-copilot-review/index.js');
|
||||
await script({ github, context, core });
|
||||
Reference in new issue
Block a user