diff --git a/.github/workflows/dev_cron/issues_link.js b/.github/workflows/dev_cron/pr_issue_linker.js similarity index 51% rename from .github/workflows/dev_cron/issues_link.js rename to .github/workflows/dev_cron/pr_issue_linker.js index e47ecb50a55a..2fa2c7cd5e1f 100644 --- a/.github/workflows/dev_cron/issues_link.js +++ b/.github/workflows/dev_cron/pr_issue_linker.js @@ -15,7 +15,7 @@ * limitations under the License. */ -function detectISSUESID(title) { +function detectIssueID(title) { if (!title) { return null; } @@ -23,48 +23,46 @@ function detectISSUESID(title) { if (!matched) { return null; } - const issues_number = matched[0].replace(/[^0-9]/ig,""); - return issues_number; + // Currently only consider one GitHub issue is referenced. + const issueID = matched[0].replace(/[^0-9]/ig, ""); + return issueID; } -async function haveComment(github, context, pullRequestNumber, body) { - const options = { +async function appendToPRDescription(github, context, pullRequestNumber, issuesID) { + const issueURL = `https://github.com/apache/incubator-gluten/issues/${issuesID}`; + const issueReference = `#${issuesID}` + + // Fetch the current PR description. + const { data: pullRequest } = await github.rest.pulls.get({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: pullRequestNumber, - page: 1 - }; - while (true) { - const response = await github.rest.issues.listComments(options); - if (response.data.some(comment => comment.body === body)) { - return true; - } - if (!/;\s*rel="next"/.test(response.headers.link || "")) { - break; - } - options.page++; - } - return false; -} + pull_number: pullRequestNumber + }); -async function commentISSUESURL(github, context, pullRequestNumber, issuesID) { - const issuesURL = `https://github.com/apache/incubator-gluten/issues/${issuesID}`; - if (await haveComment(github, context, pullRequestNumber, issuesURL)) { + const currentBody = pullRequest.body || ""; + + // Check if the issues URL or reference is already in the PR description. + if (currentBody.includes(issueURL) || currentBody.includes(issueReference)) { return; } - await github.rest.issues.createComment({ + + // Append the issues URL to the PR description. + const updatedBody = `${currentBody}\n\nRelated issue: ${issueReference}`; + + // Update the PR description. + await github.rest.pulls.update({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: pullRequestNumber, - body: issuesURL + pull_number: pullRequestNumber, + body: updatedBody }); } -module.exports = async ({github, context}) => { +module.exports = async ({ github, context }) => { const pullRequestNumber = context.payload.number; const title = context.payload.pull_request.title; - const issuesID = detectISSUESID(title); + const issuesID = detectIssueID(title); if (issuesID) { - await commentISSUESURL(github, context, pullRequestNumber, issuesID); + await appendToPRDescription(github, context, pullRequestNumber, issuesID); } }; diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml deleted file mode 100644 index e18670d16e5e..000000000000 --- a/.github/workflows/labeler.yml +++ /dev/null @@ -1,29 +0,0 @@ -# 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. - -name: Label Pull Requests -on: pull_request_target - -jobs: - label: - runs-on: ubuntu-latest - permissions: - contents: read - pull-requests: write - steps: - - uses: actions/labeler@v5 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" - sync-labels: true diff --git a/.github/workflows/dev_cron.yml b/.github/workflows/pr_bot.yml similarity index 69% rename from .github/workflows/dev_cron.yml rename to .github/workflows/pr_bot.yml index 834ab8d57938..7f3219fc115e 100644 --- a/.github/workflows/dev_cron.yml +++ b/.github/workflows/pr_bot.yml @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: Dev PR +name: PR Bot on: - pull_request_target: types: - opened @@ -24,20 +23,32 @@ on: - synchronize jobs: + label: + name: label-pull-requests + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + steps: + - uses: actions/labeler@v5 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + sync-labels: true + process: - name: Process + name: link-referenced-issues runs-on: ubuntu-latest - permissions: write-all + permissions: + pull-requests: write steps: - uses: actions/checkout@v4 - - name: Comment Issues link + - name: Link referenced issues if: | - github.event_name == 'pull_request_target' && - (github.event.action == 'opened' || - github.event.action == 'edited') + github.event.action == 'opened' || + github.event.action == 'edited' uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const script = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_cron/issues_link.js`); + const script = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_cron/pr_issue_linker.js`); script({github, context});