Reranko05 commented on code in PR #55361: URL: https://github.com/apache/arrow/pull/55361#discussion_r4469048734
########## .github/workflows/ai_disclosure/check.js: ########## @@ -0,0 +1,203 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +const fs = require("fs"); + +const AUTHORSHIP_HEADING = "PR code and description written by:"; +const REVIEW_HEADING = "Reviewed before submission by:"; + +function parseSection(lines, start, end, options) { + const found = new Set(); + const selected = []; + + for (const line of lines.slice(start + 1, end)) { + const match = line.match(/^\s*-\s+\[([ xX])\]\s*(.*?)\s*$/); + + if (!match) { + if (/^\s*-\s+\[/.test(line)) return null; + continue; + } + + const option = match[2]; + + if (!options.includes(option) || found.has(option)) return null; + + found.add(option); + + if (match[1].toLowerCase() === "x") { + selected.push(option); + } + } + + if ( + found.size !== options.length || + selected.length === 0 || + (selected.includes("Not reviewed") && selected.length > 1) + ) { + return null; + } + + return selected; +} + +function normalizeHeading(line) { + return line.trim().replace(/^\*\*(.*?)\*\*$/, "$1"); +} + +function isHeading(line) { + const trimmed = line.trim(); + return /^#{1,6}\s/.test(trimmed) || /^\*\*.+\*\*$/.test(trimmed); +} + +function parseDisclosure(body) { + const lines = body.split(/\r?\n/); + const headings = lines.map(normalizeHeading); + const authorshipStart = headings.indexOf(AUTHORSHIP_HEADING); + const reviewStart = headings.indexOf(REVIEW_HEADING); + + if ( + authorshipStart === -1 || + reviewStart <= authorshipStart || + headings.lastIndexOf(AUTHORSHIP_HEADING) !== authorshipStart || + headings.lastIndexOf(REVIEW_HEADING) !== reviewStart + ) { + return {status: "incomplete"}; + } + + const reviewEnd = lines.findIndex( + (line, index) => index > reviewStart && isHeading(line) + ); + + const authorship = parseSection( + lines, authorshipStart, reviewStart, ["Human", "AI"] + ); + + const review = parseSection( + lines, + reviewStart, + reviewEnd === -1 ? lines.length : reviewEnd, + ["Human", "AI", "Not reviewed"] + ); + + if (!authorship || !review) { + return {status: "incomplete"}; + } + + if (review.includes("Human")) { + return {status: "complete"}; + } + + if (authorship.length === 1 && authorship[0] === "AI") { + return {status: "ai-unreviewed"}; + } + + return {status: "needs-human-review"}; +} + +/** + * Adds a comment to the pull request. + */ +async function createDisclosureComment( + github, context, pullRequestNumber, body +) { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pullRequestNumber, + body + }); +} + +/** + * Converts the pull request to Draft unless it is already a draft. + * + * @param {Object} github + * @param {Object} context + */ +async function convertToDraft(github, context) { + if (context.payload.pull_request.draft) return; + + await github.graphql( + ` + mutation ($pullRequestId: ID!) { + convertPullRequestToDraft( + input: {pullRequestId: $pullRequestId} + ) { + pullRequest { + id + } + } + } + `, + {pullRequestId: context.payload.pull_request.node_id} + ); +} + +module.exports = async ({github, context, core}) => { + const pullRequest = context.payload.pull_request; + const pullRequestNumber = pullRequest.number; + + if ( + pullRequest.state !== "open" || + (pullRequest.draft && context.payload.action !== "ready_for_review") || + pullRequest.user?.type === "Bot" + ) { + core.info(`Skipping #${pullRequestNumber}: not an eligible pull request.`); + return; + } + + const result = parseDisclosure(pullRequest.body || ""); + + core.info(`Disclosure status for #${pullRequestNumber}: ${result.status}.`); + + if (result.status === "complete") { + return; + } + + if (result.status === "ai-unreviewed") { + await createDisclosureComment( + github, + context, + pullRequestNumber, + `This pull request has been automatically closed because its authorship is AI-only and it has no human review. Review Comment: The intention behind the distinction is to handle AI-only contributions differently from contributions that involve human authorship. Close: If the authorship is marked as AI-only and the review is marked as AI or Not reviewed, we close the PR. In this case, there is no declared human involvement in either writing the contribution or reviewing it. Convert to Draft: If the authorship includes Human (either Human or Human + AI), but the review is marked as AI or Not reviewed, we convert the PR to Draft and ask the author to review their contribution before marking it ready again. The contribution has human involvement, so we give the author an opportunity to complete the required review. No action: If the review includes Human, the disclosure is considered complete. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
