From f0f2e16d3f362a6888cbdfbdf86b6d36fe87235d Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Fri, 13 Sep 2024 16:09:45 +0200 Subject: [PATCH] further test pr enforcement --- .github/scripts/jira/changeset-lib.ts | 21 ++++++++++++++++++- .github/scripts/jira/enforce-jira-issue.ts | 14 ++----------- .../jira/enforce-jira-solidity-review.ts | 18 ++++------------ .github/scripts/jira/update-jira-issue.ts | 4 ++-- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/.github/scripts/jira/changeset-lib.ts b/.github/scripts/jira/changeset-lib.ts index e5593e3ba96..6ce12cd2dc7 100644 --- a/.github/scripts/jira/changeset-lib.ts +++ b/.github/scripts/jira/changeset-lib.ts @@ -43,4 +43,23 @@ export function extractChangesetFiles(): string[] { `Changeset to extract issues from: ${parsedChangesetFiles.join(", ")}` ); return parsedChangesetFiles; -} \ No newline at end of file +} + +/** + * Extracts a single changeset file. Intended to be used with https://github.com/dorny/paths-filter with + * the 'csv' output format. + * + * @returns An object with single changeset file. + * @throws {Error} If the required environment variable CHANGESET_FILES is missing. + * @throws {Error} If no changeset file exists. + * @throws {Error} If more than one changeset file exists. + */ +export function extractChangesetFile() { + const changesetFiles = extractChangesetFiles() + if (changesetFiles.length > 1) { + throw new Error(`Found ${changesetFiles.length} changeset files, but only 1 was expected.`) + } + const [changesetFile] = changesetFiles; + + return { changesetFile }; +} diff --git a/.github/scripts/jira/enforce-jira-issue.ts b/.github/scripts/jira/enforce-jira-issue.ts index a74592fa5ee..f375cc34a7e 100644 --- a/.github/scripts/jira/enforce-jira-issue.ts +++ b/.github/scripts/jira/enforce-jira-issue.ts @@ -1,23 +1,13 @@ import * as core from "@actions/core"; import { createJiraClient, EMPTY_PREFIX, parseIssueNumberFrom, doesIssueExist, PR_PREFIX } from "./lib"; -import { appendIssueNumberToChangesetFile, extractChangesetFiles } from "./changeset-lib"; +import { appendIssueNumberToChangesetFile, extractChangesetFile } from "./changeset-lib"; async function main() { const prTitle = process.env.PR_TITLE; const commitMessage = process.env.COMMIT_MESSAGE; const branchName = process.env.BRANCH_NAME; const dryRun = !!process.env.DRY_RUN; - const changesetFiles = extractChangesetFiles(); - - if (changesetFiles.length > 1) { - core.setFailed( - `This PR must add only one changeset, but found ${changesetFiles.length}` - ); - - return - } - - const changesetFile = changesetFiles[0] + const { changesetFile } = extractChangesetFile(); const client = createJiraClient(); // Checks for the Jira issue number and exit if it can't find it diff --git a/.github/scripts/jira/enforce-jira-solidity-review.ts b/.github/scripts/jira/enforce-jira-solidity-review.ts index efad4ed6327..f107e92bc85 100644 --- a/.github/scripts/jira/enforce-jira-solidity-review.ts +++ b/.github/scripts/jira/enforce-jira-solidity-review.ts @@ -3,24 +3,14 @@ import jira from "jira.js"; import axios from "axios"; import { join } from "path"; import { createJiraClient, extractJiraIssueNumbersFrom, getJiraEnvVars, doesIssueExist, PR_PREFIX, SOLIDITY_REVIEW_PREFIX } from "./lib"; -import { appendIssueNumberToChangesetFile, extractChangesetFiles } from "./changeset-lib"; +import { appendIssueNumberToChangesetFile, extractChangesetFile } from "./changeset-lib"; async function main() { core.info('Started linking PR to a Solidity Review issue') const solidityReviewTemplateKey = readSolidityReviewTemplateKey() - const changesetFiles = extractChangesetFiles(); + const { changesetFile } = extractChangesetFile(); - if (changesetFiles.length > 1) { - core.setFailed( - `Solidity Review enforcement only works with 1 changeset per PR, but found ${changesetFiles.length} changesets` - ); - - return - } - - const changesetFile = changesetFiles[0] - - const jiraPRIssues = await extractJiraIssueNumbersFrom(PR_PREFIX, changesetFiles) + const jiraPRIssues = await extractJiraIssueNumbersFrom(PR_PREFIX, [changesetFile]) if (jiraPRIssues.length !== 1) { core.setFailed( `Solidity Review enforcement only works with 1 JIRA issue per PR, but found ${jiraPRIssues.length} issues in changeset file ${changesetFile}` @@ -32,7 +22,7 @@ async function main() { const jiraPRIssue = jiraPRIssues[0] const client = createJiraClient(); - const jiraSolidityIssues = await extractJiraIssueNumbersFrom(SOLIDITY_REVIEW_PREFIX, changesetFiles) + const jiraSolidityIssues = await extractJiraIssueNumbersFrom(SOLIDITY_REVIEW_PREFIX, [changesetFile]) if (jiraSolidityIssues.length > 0) { for (const jiraIssue of jiraSolidityIssues) { const exists = await doesIssueExist(client, jiraIssue, false); diff --git a/.github/scripts/jira/update-jira-issue.ts b/.github/scripts/jira/update-jira-issue.ts index dfe3e251e24..59581b716bc 100644 --- a/.github/scripts/jira/update-jira-issue.ts +++ b/.github/scripts/jira/update-jira-issue.ts @@ -1,6 +1,6 @@ import * as core from "@actions/core"; import jira from "jira.js"; -import { tagsToLabels, createJiraClient, parseIssueNumberFrom, PR_PREFIX } from "./lib"; +import { tagsToLabels, createJiraClient, parseIssueNumberFrom, EMPTY_PREFIX } from "./lib"; function updateJiraIssue( client: jira.Version3Client, @@ -43,7 +43,7 @@ async function main() { const client = createJiraClient(); // Checks for the Jira issue number and exit if it can't find it - const issueNumber = parseIssueNumberFrom(PR_PREFIX, prTitle, commitMessage, branchName); + const issueNumber = parseIssueNumberFrom(EMPTY_PREFIX, prTitle, commitMessage, branchName); if (!issueNumber) { const msg = "No JIRA issue number found in: PR title, commit message, or branch name. Please include the issue ID in one of these.";