Skip to content

Commit

Permalink
Sleeping for 2 minutes on first fail
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrunkat committed Jan 23, 2023
1 parent 62658fa commit a120b2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 17 additions & 12 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
export async function ensureCorrectLinkingAndEstimates(pullRequest: PullRequest, octokit: OctokitType, isDryRun: boolean): Promise<void> {
const pullRequestGraphqlResponse = await queryZenhubGraphql('getIssueInfo', ZENHUB_PR_DETAILS_QUERY, {
repositoryGhId: pullRequest.head.repo?.id,
issueNumber: pullRequest.number,
Expand All @@ -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, {
Expand All @@ -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<void> {
export async function fail(pullRequest: PullRequest, errorMessage: string, octokit: OctokitType, isDryRun = false): Promise<void> {
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);
}
Expand Down
16 changes: 14 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -46,7 +49,16 @@ async function run(): Promise<void> {
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);
Expand Down

0 comments on commit a120b2b

Please sign in to comment.