Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multiple projectKeys in jira automations #315

Merged
24 changes: 18 additions & 6 deletions .github/actions/jira-find-marker/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,17 +24,29 @@ 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 }};
if (!pullRequestBody) {
return;
}

const markerRegex = new RegExp(`<!--JIRA_VALIDATE_START:(${{ inputs.projectKey }}-\\d+):do not remove this marker as it will break the jira validation functionality-->[\\s\\S]+<!--JIRA_VALIDATE_END:do not remove this marker as it will break the jira validation functionality-->\\s+---\\s+`);
const markerMatch = pullRequestBody.match(markerRegex);
if (!markerMatch || markerMatch.length < 2) {
const projectKeys = `${{ inputs.projectKeys }}`.split(',').map(env => env.trim()).filter(b => b.length > 0);

let markerMatch = null;
projectKeys.find(projectKey => {
console.log('Searching for Jira marker in PR body for project key:', projectKey);
const markerRegex = new RegExp(`<!--JIRA_VALIDATE_START:(${projectKey}-\\d+):do not remove this marker as it will break the jira validation functionality-->[\\s\\S]+<!--JIRA_VALIDATE_END:do not remove this marker as it will break the jira validation functionality-->\\s+---\\s+`);
const matchResult = pullRequestBody.match(markerRegex);
if (matchResult && matchResult.length >= 2) {
markerMatch = matchResult;
return true;
}
return false;
});

if (!markerMatch) {
console.log('No Jira marker found in PR body');
return;
}
Expand Down
68 changes: 47 additions & 21 deletions .github/actions/jira-validate-reference/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}');
Expand All @@ -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 }}';
Expand All @@ -63,37 +63,63 @@ 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;
});

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 === noJiraIssueBody) {
console.log('Found comment:', JSON.stringify(comment, null, 2));
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
}

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.`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: ${{ github.event.pull_request.number }},
body,
body: noJiraIssueBody,
});
throw new Error(body);
}
Expand All @@ -102,7 +128,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 }};
Expand All @@ -123,13 +149,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@885251a38b81dbb9b6ab0b106dc31ec2f5703764
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: |
Expand Down
Loading