From faafd22c1957bdda910aabbf39e421c636fbbff1 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 09:56:50 -0600 Subject: [PATCH 01/11] feat: allow multiple projectKeys in jira automations --- .github/actions/jira-find-marker/action.yaml | 24 ++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/actions/jira-find-marker/action.yaml b/.github/actions/jira-find-marker/action.yaml index eec5d640..d4bbc9d1 100644 --- a/.github/actions/jira-find-marker/action.yaml +++ b/.github/actions/jira-find-marker/action.yaml @@ -6,8 +6,8 @@ inputs: description: The Github PR body required: true type: string - projectKey: - description: The Jira project key + projectKeys: + description: The Jira project keys to look for (comma delimited) required: true type: string @@ -24,7 +24,7 @@ runs: steps: - name: Get Jira Issue Reference From Marker id: getJiraIssueReferenceFromMarker - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | const pullRequestBody = ${{ inputs.pullRequestBody }}; @@ -32,12 +32,18 @@ runs: return; } - const markerRegex = new RegExp(`[\\s\\S]+\\s+---\\s+`); - const markerMatch = pullRequestBody.match(markerRegex); - if (!markerMatch || markerMatch.length < 2) { - console.log('No Jira marker found in PR body'); - return; - } + const projectKeys = `${{ inputs.projectKey }}`.split(',').map(env => env.trim()).filter(b => b.length > 0); + + const markerMatch = projectKeys.map(projectKey => { + console.log('Searching for Jira marker in PR body for project key:', projectKey); + const markerRegex = new RegExp(`[\\s\\S]+\\s+---\\s+`); + const matchResult = pullRequestBody.match(markerRegex); + if (!matchResult || matchResult.length < 2) { + console.log('No Jira marker found in PR body'); + return false; + } + return matchResult; + }).find(match => match); const jiraIssueMarker = markerMatch[0]; console.log('Setting jiraIssueMarker to:', jiraIssueMarker); From ec9eef9d74a4e70ff27fb796ed89678f8f8002f5 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 10:16:03 -0600 Subject: [PATCH 02/11] multi-projectKey in jira ref validator --- .../jira-validate-reference/action.yaml | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index bd017ae8..cb570eb5 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -6,8 +6,8 @@ description: | the PR body (or update the existing summary if it already exists). inputs: - projectKey: - description: The Jira project key + projectKeys: + description: The Jira project keys to look for (comma delimited) required: true type: string skipBranches: @@ -30,7 +30,7 @@ runs: steps: - name: Check If Job Should Run id: checkIfJobShouldRun - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | const skipBranchesRegex = new RegExp('${{ inputs.skipBranches }}'); @@ -45,7 +45,7 @@ runs: - name: Get Pull Request id: getPullRequest if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | const ownerAndRepo = '${{ github.repository }}'; @@ -63,32 +63,40 @@ runs: - name: Parse Jira Issue Reference id: parseIssueRef if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | const prBody = ${{ steps.getPullRequest.outputs.prBody }}; const prTitle = ${{ steps.getPullRequest.outputs.prTitle }}; + const projectKeys = `${{ inputs.projectKeys }}`.split(',').map(env => env.trim()).filter(b => b.length > 0); + if (projectKeys.length === 0) { + throw new Error('No projectKeys provided'); + } + + const issueRegexes = projectKeys.map(projectKey => new RegExp(`${projectKey}-\\d+`)); let jiraIssueRef = null; - const issueRegex = new RegExp(`${{ inputs.projectKey }}-\\d+`); [ { label: 'branch', value: '${{ github.head_ref }}' }, { label: 'title', value: prTitle }, { label: 'body', value: prBody }, ].find(({label, value}) => { - const match = value.match(issueRegex); - if (match) { - jiraIssueRef = match[0]; - console.log(`Found Jira issue reference in PR ${label}: ${jiraIssueRef}`); - return true; - } - return false; + const found = issueRegexes.find(issueRegex => { + const match = value.match(issueRegex); + if (match) { + jiraIssueRef = match[0]; + console.log(`Found Jira issue reference in PR ${label}: ${jiraIssueRef}`); + return true; + } + return false; + }); + return found; }); if (!jiraIssueRef) { const ownerAndRepo = '${{ github.repository }}'; const [owner, repo] = ownerAndRepo.split('/'); - const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of ${{ inputs.projectKey }}-#### (eg: ${{ inputs.projectKey }}-1400) to the branch name, title, or body of your PR.`; + const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of -#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; await github.rest.issues.createComment({ owner, repo, @@ -102,7 +110,7 @@ runs: - name: Fetch Jira Issue Details id: fetchJiraIssueDetails if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | const jiraIssueRef = ${{ steps.parseIssueRef.outputs.result }}; @@ -123,13 +131,13 @@ runs: - name: Find Jira Marker id: findJiraMarker if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@jira-find-marker-v1.0.1 + uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@faafd22c1957bdda910aabbf39e421c636fbbff1 with: pullRequestBody: ${{ steps.getPullRequest.outputs.prBody }} - projectKey: ${{ inputs.projectKey }} + projectKeys: ${{ inputs.projectKeys }} - name: Update PR Body - uses: actions/github-script@v6 + uses: actions/github-script@v7 if: steps.checkIfJobShouldRun.outputs.result == 'true' with: script: | From 9d1ca246f8807d2de647867a62917690d20ab609 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 10:49:05 -0600 Subject: [PATCH 03/11] list comments --- .github/actions/jira-validate-reference/action.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index cb570eb5..623bb26a 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -97,6 +97,19 @@ runs: const ownerAndRepo = '${{ github.repository }}'; const [owner, repo] = ownerAndRepo.split('/'); const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of -#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; + + const { data: comments } = await github.rest.issues.listComments({ + owner, + repo, + issue_number: ${{ github.event.pull_request.number }}, + }) + if (comments.length > 0) { + console.log('comments:', JSON.stringify(comments[0], null, 2)); + } + // const botComment = comments.find(comment => { + // return comment.user.type === 'Bot' && comment.body.includes('${{ matrix.tfmodule }}') + // }) + await github.rest.issues.createComment({ owner, repo, From 848d188a7a94fc2023881381353aae28ced7f7af Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 10:58:33 -0600 Subject: [PATCH 04/11] delete old comment --- .../actions/jira-validate-reference/action.yaml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index 623bb26a..3f484db0 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -103,12 +103,16 @@ runs: repo, issue_number: ${{ github.event.pull_request.number }}, }) - if (comments.length > 0) { - console.log('comments:', JSON.stringify(comments[0], null, 2)); + + const comment = comments.find(comment => comment.body === body }); + if (comment) { + console.log('Found comment:', JSON.stringify(comment, null, 2)); + await github.rest.issues.deleteComment({ + owner, + repo, + comment_id: comment.id, + }); } - // const botComment = comments.find(comment => { - // return comment.user.type === 'Bot' && comment.body.includes('${{ matrix.tfmodule }}') - // }) await github.rest.issues.createComment({ owner, From dc108eebfd35bfd7a0df69ded757142b5eb15855 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 11:01:37 -0600 Subject: [PATCH 05/11] remove all similar commentS --- .../jira-validate-reference/action.yaml | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index 3f484db0..b01089a3 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -96,7 +96,7 @@ runs: if (!jiraIssueRef) { const ownerAndRepo = '${{ github.repository }}'; const [owner, repo] = ownerAndRepo.split('/'); - const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of -#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; + const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of PROJECTKEY-#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; const { data: comments } = await github.rest.issues.listComments({ owner, @@ -104,15 +104,16 @@ runs: issue_number: ${{ github.event.pull_request.number }}, }) - const comment = comments.find(comment => comment.body === body }); - if (comment) { - console.log('Found comment:', JSON.stringify(comment, null, 2)); - await github.rest.issues.deleteComment({ - owner, - repo, - comment_id: comment.id, - }); - } + const comment = comments.forEach(comment => { + if (comment.body === body) { + console.log('Found comment:', JSON.stringify(comment, null, 2)); + await github.rest.issues.deleteComment({ + owner, + repo, + comment_id: comment.id, + }); + } + }); await github.rest.issues.createComment({ owner, From 72a544fedfb85a1dde94097c2c9d02e249674081 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 11:06:25 -0600 Subject: [PATCH 06/11] make await work --- .github/actions/jira-validate-reference/action.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index b01089a3..2e658453 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -104,7 +104,7 @@ runs: issue_number: ${{ github.event.pull_request.number }}, }) - const comment = comments.forEach(comment => { + for (let comment of comments) { if (comment.body === body) { console.log('Found comment:', JSON.stringify(comment, null, 2)); await github.rest.issues.deleteComment({ @@ -113,7 +113,7 @@ runs: comment_id: comment.id, }); } - }); + } await github.rest.issues.createComment({ owner, From b965a92a453d4c304682de1a1dedff5fe3db1e6b Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 11:23:46 -0600 Subject: [PATCH 07/11] clean up comments even when new one will not be added --- .../jira-validate-reference/action.yaml | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index 2e658453..07868fa6 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -93,33 +93,33 @@ runs: return found; }); - if (!jiraIssueRef) { - const ownerAndRepo = '${{ github.repository }}'; - const [owner, repo] = ownerAndRepo.split('/'); - const body = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of PROJECTKEY-#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; + const ownerAndRepo = '${{ github.repository }}'; + const [owner, repo] = ownerAndRepo.split('/'); + const noJiraIssueBody = `No Jira issue reference found in branch, title, or body of PR.\n\nPlease add a reference to a Jira issue in the form of PROJECTKEY-#### (eg: ${projectKeys[0]}-1234) to the branch name, title, or body of your PR.`; - const { data: comments } = await github.rest.issues.listComments({ - owner, - repo, - issue_number: ${{ github.event.pull_request.number }}, - }) - - for (let comment of comments) { - if (comment.body === body) { - console.log('Found comment:', JSON.stringify(comment, null, 2)); - await github.rest.issues.deleteComment({ - owner, - repo, - comment_id: comment.id, - }); - } + const { data: comments } = await github.rest.issues.listComments({ + owner, + repo, + issue_number: ${{ github.event.pull_request.number }}, + }) + + for (let comment of comments) { + if (comment.body === noJiraIssueBody) { + console.log('Found comment:', JSON.stringify(comment, null, 2)); + await github.rest.issues.deleteComment({ + owner, + repo, + comment_id: comment.id, + }); } + } + if (!jiraIssueRef) { await github.rest.issues.createComment({ owner, repo, issue_number: ${{ github.event.pull_request.number }}, - body, + body: noJiraIssueBody, }); throw new Error(body); } From a40d1961edeb85418d5638dd2ea7b29fa52c1dfd Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 12:30:14 -0600 Subject: [PATCH 08/11] fix input reference --- .github/actions/jira-find-marker/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/jira-find-marker/action.yaml b/.github/actions/jira-find-marker/action.yaml index d4bbc9d1..9d3a0125 100644 --- a/.github/actions/jira-find-marker/action.yaml +++ b/.github/actions/jira-find-marker/action.yaml @@ -32,7 +32,7 @@ runs: return; } - const projectKeys = `${{ inputs.projectKey }}`.split(',').map(env => env.trim()).filter(b => b.length > 0); + const projectKeys = `${{ inputs.projectKeys }}`.split(',').map(env => env.trim()).filter(b => b.length > 0); const markerMatch = projectKeys.map(projectKey => { console.log('Searching for Jira marker in PR body for project key:', projectKey); From dd2f27db60dfe586b28adc374f2bab5685489be8 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 12:30:59 -0600 Subject: [PATCH 09/11] use a40d1961edeb85418d5638dd2ea7b29fa52c1dfd in validator --- .github/actions/jira-validate-reference/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index 07868fa6..66c6ddc8 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -149,7 +149,7 @@ runs: - name: Find Jira Marker id: findJiraMarker if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@faafd22c1957bdda910aabbf39e421c636fbbff1 + uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@a40d1961edeb85418d5638dd2ea7b29fa52c1dfd with: pullRequestBody: ${{ steps.getPullRequest.outputs.prBody }} projectKeys: ${{ inputs.projectKeys }} From 885251a38b81dbb9b6ab0b106dc31ec2f5703764 Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 12:37:57 -0600 Subject: [PATCH 10/11] fix match detection --- .github/actions/jira-find-marker/action.yaml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/actions/jira-find-marker/action.yaml b/.github/actions/jira-find-marker/action.yaml index 9d3a0125..b0df2bf7 100644 --- a/.github/actions/jira-find-marker/action.yaml +++ b/.github/actions/jira-find-marker/action.yaml @@ -34,16 +34,22 @@ runs: const projectKeys = `${{ inputs.projectKeys }}`.split(',').map(env => env.trim()).filter(b => b.length > 0); - const markerMatch = projectKeys.map(projectKey => { + let markerMatch = null; + projectKeys.find(projectKey => { console.log('Searching for Jira marker in PR body for project key:', projectKey); const markerRegex = new RegExp(`[\\s\\S]+\\s+---\\s+`); const matchResult = pullRequestBody.match(markerRegex); - if (!matchResult || matchResult.length < 2) { - console.log('No Jira marker found in PR body'); - return false; + if (matchResult && matchResult.length >= 2) { + markerMatch = matchResult; + return true; } - return matchResult; - }).find(match => match); + return false; + }); + + if (!markerMatch) { + console.log('No Jira marker found in PR body'); + return; + } const jiraIssueMarker = markerMatch[0]; console.log('Setting jiraIssueMarker to:', jiraIssueMarker); From 6ef0856d82f4aab88c16e44c5ab058a443a0000a Mon Sep 17 00:00:00 2001 From: Hayden Spitzley Date: Wed, 2 Oct 2024 12:38:40 -0600 Subject: [PATCH 11/11] use 885251a38b81dbb9b6ab0b106dc31ec2f5703764 in validator --- .github/actions/jira-validate-reference/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/jira-validate-reference/action.yaml b/.github/actions/jira-validate-reference/action.yaml index 66c6ddc8..e571bf09 100644 --- a/.github/actions/jira-validate-reference/action.yaml +++ b/.github/actions/jira-validate-reference/action.yaml @@ -149,7 +149,7 @@ runs: - name: Find Jira Marker id: findJiraMarker if: steps.checkIfJobShouldRun.outputs.result == 'true' - uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@a40d1961edeb85418d5638dd2ea7b29fa52c1dfd + uses: chanzuckerberg/github-actions/.github/actions/jira-find-marker@885251a38b81dbb9b6ab0b106dc31ec2f5703764 with: pullRequestBody: ${{ steps.getPullRequest.outputs.prBody }} projectKeys: ${{ inputs.projectKeys }}