From 76116811836b93373f4cad43e0fb4ac237d4e4ab Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 4 Jan 2021 06:33:24 +0900 Subject: [PATCH] [Developer] Use pull_request_target for PR JIRA integration --- .../workflows/{dev_cron.yml => dev_jira.yml} | 41 +++++++----------- .../jira_link.js => dev_jira/link.js} | 42 ++++++++----------- .../{dev_cron => dev_jira}/title_check.js | 38 +++++++---------- .../{dev_cron => dev_jira}/title_check.md | 0 .github/workflows/dev_labeler.yml | 3 +- .github/workflows/dev_labeler_pr_rebase.yml | 3 +- 6 files changed, 50 insertions(+), 77 deletions(-) rename .github/workflows/{dev_cron.yml => dev_jira.yml} (56%) rename .github/workflows/{dev_cron/jira_link.js => dev_jira/link.js} (67%) rename .github/workflows/{dev_cron => dev_jira}/title_check.js (66%) rename .github/workflows/{dev_cron => dev_jira}/title_check.md (100%) diff --git a/.github/workflows/dev_cron.yml b/.github/workflows/dev_jira.yml similarity index 56% rename from .github/workflows/dev_cron.yml rename to .github/workflows/dev_jira.yml index 77127185118..6c301a0ce07 100644 --- a/.github/workflows/dev_cron.yml +++ b/.github/workflows/dev_jira.yml @@ -15,49 +15,38 @@ # specific language governing permissions and limitations # under the License. -name: Dev Cron +name: PR JIRA integration on: - push: - paths: - - '.github/workflows/dev_cron.yml' - pull_request: - paths: - - '.github/workflows/dev_cron.yml' - schedule: - - cron: | - */15 * * * * + pull_request_target: + types: + - opened + - edited jobs: - jira-link: - if: github.repository == 'apache/arrow' - name: JIRA link + link: + name: Link runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v2 - name: Comment - uses: actions/github-script@master + uses: actions/github-script@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const fs = require("fs"); - const path = ".github/workflows/dev_cron/jira_link.js"; - const script = fs.readFileSync(path).toString(); - eval(script); + const script = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_jira/link.js`); + script({github, context}); title-check: - if: github.repository == 'apache/arrow' name: Title check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v2 - name: Check - uses: actions/github-script@master + uses: actions/github-script@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const fs = require("fs"); - const path = ".github/workflows/dev_cron/title_check.js"; - const script = fs.readFileSync(path).toString(); - eval(script); + const script = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_jira/title_check.js`); + script({github, context}); diff --git a/.github/workflows/dev_cron/jira_link.js b/.github/workflows/dev_jira/link.js similarity index 67% rename from .github/workflows/dev_cron/jira_link.js rename to .github/workflows/dev_jira/link.js index a9b502a303b..550a9cd396d 100644 --- a/.github/workflows/dev_cron/jira_link.js +++ b/.github/workflows/dev_jira/link.js @@ -15,23 +15,21 @@ // specific language governing permissions and limitations // under the License. -const {owner: owner, repo: repo} = context.repo; - function detectJIRAID(title) { if (!title) { return null; } - const matched = /^(ARROW|PARQUET)-\d+/.exec(title); + const matched = /^(WIP:?\s*)?((ARROW|PARQUET)-\d+)/.exec(title); if (!matched) { return null; } - return matched[0]; + return matched[2]; } -async function haveComment(pullRequestNumber, body) { +async function haveComment(github, context, pullRequestNumber, body) { const options = { - owner: owner, - repo: repo, + owner: context.repo.owner, + repo: context.repo.repo, issue_number: pullRequestNumber, page: 1 }; @@ -48,30 +46,24 @@ async function haveComment(pullRequestNumber, body) { return false; } -async function commentJIRAURL(pullRequestNumber, jiraID) { +async function commentJIRAURL(github, context, pullRequestNumber, jiraID) { const jiraURL = `https://issues.apache.org/jira/browse/${jiraID}`; - if (await haveComment(pullRequestNumber, jiraURL)) { + if (await haveComment(github, context, pullRequestNumber, jiraURL)) { return; } await github.issues.createComment({ - owner: owner, - repo: repo, + owner: context.repo.owner, + repo: context.repo.repo, issue_number: pullRequestNumber, body: jiraURL }); } -(async () => { - const {data: pulls} = await github.pulls.list({ - owner: owner, - repo: repo, - }); - pulls.forEach(async (pull) => { - const pullRequestNumber = pull.number; - const title = pull.title; - const jiraID = detectJIRAID(title); - if (jiraID) { - await commentJIRAURL(pullRequestNumber, jiraID); - } - }); -})(); +module.exports = async ({github, context}) => { + const pullRequestNumber = context.payload.number; + const title = context.payload.pull_request.title; + const jiraID = detectJIRAID(title); + if (jiraID) { + await commentJIRAURL(github, context, pullRequestNumber, jiraID); + } +}; diff --git a/.github/workflows/dev_cron/title_check.js b/.github/workflows/dev_jira/title_check.js similarity index 66% rename from .github/workflows/dev_cron/title_check.js rename to .github/workflows/dev_jira/title_check.js index 7dee25113e0..a6ce92cc756 100644 --- a/.github/workflows/dev_cron/title_check.js +++ b/.github/workflows/dev_jira/title_check.js @@ -15,49 +15,39 @@ // specific language governing permissions and limitations // under the License. -console.log("title-check"); - const fs = require("fs"); -const {owner: owner, repo: repo} = context.repo; - function haveJIRAID(title) { if (!title) { return false; } - return /^(ARROW|PARQUET)-\d+/.test(title); + return /^(WIP:?\s*)?(ARROW|PARQUET)-\d+/.test(title); } -async function commentOpenJIRAIssue(pullRequestNumber) { +async function commentOpenJIRAIssue(github, context, pullRequestNumber) { const {data: comments} = await github.issues.listComments({ - owner: owner, - repo: repo, + owner: context.repo.owner, + repo: context.repo.repo, issue_number: pullRequestNumber, per_page: 1 }); if (comments.length > 0) { return; } - const commentPath = ".github/workflows/dev_cron/title_check.md"; + const commentPath = ".github/workflows/dev_jira/title_check.md"; const comment = fs.readFileSync(commentPath).toString(); await github.issues.createComment({ - owner: owner, - repo: repo, + owner: context.repo.owner, + repo: context.repo.repo, issue_number: pullRequestNumber, body: comment }); } -(async () => { - const {data: pulls} = await github.pulls.list({ - owner: owner, - repo: repo, - }); - pulls.forEach(async (pull) => { - const pullRequestNumber = pull.number; - const title = pull.title; - if (!haveJIRAID(title)) { - await commentOpenJIRAIssue(pullRequestNumber); - } - }); -})(); +module.exports = async ({github, context}) => { + const pullRequestNumber = context.payload.number; + const title = context.payload.pull_request.title; + if (!haveJIRAID(title)) { + await commentOpenJIRAIssue(github, context, pullRequestNumber); + } +}; diff --git a/.github/workflows/dev_cron/title_check.md b/.github/workflows/dev_jira/title_check.md similarity index 100% rename from .github/workflows/dev_cron/title_check.md rename to .github/workflows/dev_jira/title_check.md diff --git a/.github/workflows/dev_labeler.yml b/.github/workflows/dev_labeler.yml index 645ca575cc9..bb4a8dbe2d3 100644 --- a/.github/workflows/dev_labeler.yml +++ b/.github/workflows/dev_labeler.yml @@ -21,7 +21,8 @@ on: types: [opened, reopened] jobs: - assign-rust-labels: + label: + name: Label runs-on: ubuntu-latest steps: - name: Assign Github labels diff --git a/.github/workflows/dev_labeler_pr_rebase.yml b/.github/workflows/dev_labeler_pr_rebase.yml index d9fad682279..a94603386c5 100644 --- a/.github/workflows/dev_labeler_pr_rebase.yml +++ b/.github/workflows/dev_labeler_pr_rebase.yml @@ -22,7 +22,8 @@ on: types: [synchronize] jobs: - main: + label: + name: Label runs-on: ubuntu-latest steps: - name: Checks if PR needs rebase