From a120b2b09a4b5448651d31b8c2ccf34110e11137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Trunk=C3=A1t?= Date: Mon, 23 Jan 2023 16:15:25 +0100 Subject: [PATCH] Sleeping for 2 minutes on first fail --- src/consts.ts | 2 ++ src/helpers.ts | 29 +++++++++++++++++------------ src/main.ts | 16 ++++++++++++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/consts.ts b/src/consts.ts index cd3ff5b..85ba63d 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -9,3 +9,5 @@ export const TEAM_LABEL_PREFIX = 't-'; export const TEAM_NAME_TO_LABEL: { [name: string]: string} = { 'Cash & Community': 't-c&c', }; + +export const DRY_RUN_SLEEP_MINS = 2; diff --git a/src/helpers.ts b/src/helpers.ts index 52ea227..c59561e 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -215,7 +215,7 @@ query getIssueInfo($repositoryGhId: Int!, $issueNumber: Int!) { * - PR either has issue or epic linked or has `adhoc` label * - either PR or linked issue has estimate */ -export async function ensureCorrectLinkingAndEstimates(pullRequest: PullRequest, octokit: OctokitType): Promise { +export async function ensureCorrectLinkingAndEstimates(pullRequest: PullRequest, octokit: OctokitType, isDryRun: boolean): Promise { const pullRequestGraphqlResponse = await queryZenhubGraphql('getIssueInfo', ZENHUB_PR_DETAILS_QUERY, { repositoryGhId: pullRequest.head.repo?.id, issueNumber: pullRequest.number, @@ -230,9 +230,11 @@ export async function ensureCorrectLinkingAndEstimates(pullRequest: PullRequest, !linkedIssue && linkedEpics.length === 0 && !pullRequest.labels.some(({ name }) => name === 'adhoc') - ) await fail(pullRequest, 'Pull request is neither linked to an issue or epic nor labeled as adhoc!', octokit); + ) await fail(pullRequest, 'Pull request is neither linked to an issue or epic nor labeled as adhoc!', octokit, isDryRun); - if (!linkedIssue && !pullRequestEstimate) await fail(pullRequest, 'If issue is not linked to the pull request then estimate the pull request!', octokit); + if (!linkedIssue && !pullRequestEstimate) { + await fail(pullRequest, 'If issue is not linked to the pull request then estimate the pull request!', octokit, isDryRun); + } if (!linkedIssue) return; const issueGraphqlResponse = await queryZenhubGraphql('getIssueInfo', ZENHUB_ISSUE_ESTIMATE_QUERY, { @@ -242,22 +244,25 @@ export async function ensureCorrectLinkingAndEstimates(pullRequest: PullRequest, }); const issueEstimate = issueGraphqlResponse.data.data.issueByInfo.estimate?.value; - if (!pullRequestEstimate && !issueEstimate) await fail(pullRequest, 'None of the pull request and linked issue has estimate', octokit); + if (!pullRequestEstimate && !issueEstimate) await fail(pullRequest, 'None of the pull request and linked issue has estimate', octokit, isDryRun); }; /** * Adds a comment describing what is wrong with the pull request setup and then fails the action. + * Comment is not send if isDryRun=false. */ -export async function fail(pullRequest: PullRequest, errorMessage: string, octokit: OctokitType): Promise { +export async function fail(pullRequest: PullRequest, errorMessage: string, octokit: OctokitType, isDryRun = false): Promise { if (!pullRequest.head.repo) throw new Error('Unknown repo!'); - await octokit.rest.pulls.createReview({ - owner: ORGANIZATION, - repo: pullRequest.head.repo.name, - pull_number: pullRequest.number, - body: `⚠️ [Pull Request Tookit](https://github.com/apify/pull-request-toolkit-action) has failed!\n\n> ${errorMessage}`, - event: 'COMMENT', - }); + if (!isDryRun) { + await octokit.rest.pulls.createReview({ + owner: ORGANIZATION, + repo: pullRequest.head.repo.name, + pull_number: pullRequest.number, + body: `⚠️ [Pull Request Tookit](https://github.com/apify/pull-request-toolkit-action) has failed!\n\n> ${errorMessage}`, + event: 'COMMENT', + }); + } throw new Error(errorMessage); } diff --git a/src/main.ts b/src/main.ts index 6fd1ae7..9e50091 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,7 +8,10 @@ import { addTeamLabel, ensureCorrectLinkingAndEstimates, } from './helpers'; -import { TEAM_LABEL_PREFIX } from './consts'; +import { + TEAM_LABEL_PREFIX, + DRY_RUN_SLEEP_MINS, +} from './consts'; type Assignee = components['schemas']['simple-user']; type Label = components['schemas']['label']; @@ -46,7 +49,16 @@ async function run(): Promise { const teamLabel = pullRequestContext.labels.find((label: Label) => label.name.startsWith(TEAM_LABEL_PREFIX)); if (!teamLabel) await addTeamLabel(github.context, repoOctokit, pullRequest, teamName); - await ensureCorrectLinkingAndEstimates(pullRequest, repoOctokit); + try { + await ensureCorrectLinkingAndEstimates(pullRequest, repoOctokit, true); + } catch (err) { + console.log('Function ensureCorrectLinkingAndEstimates() has failed on dry run'); + console.log(err); + console.log(`Sleeping for ${DRY_RUN_SLEEP_MINS} minutes`); + await new Promise((resolve) => setTimeout(resolve, DRY_RUN_SLEEP_MINS * 60.000)); + + await ensureCorrectLinkingAndEstimates(pullRequest, repoOctokit, false); + } } catch (error) { if (error instanceof Error) { core.error(error);