From 298ca9988c78f19d2f4e991239de12f1b478de86 Mon Sep 17 00:00:00 2001 From: Gabriel-Ladzaretti <97394622+Gabriel-Ladzaretti@users.noreply.github.com> Date: Sat, 28 May 2022 08:26:55 +0300 Subject: [PATCH] refactor(onboarding): warning when onboarding pr cant be found (#15724) --- .../repository/onboarding/pr/index.spec.ts | 34 +++++++++++++++++++ lib/workers/repository/onboarding/pr/index.ts | 8 +++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/workers/repository/onboarding/pr/index.spec.ts b/lib/workers/repository/onboarding/pr/index.spec.ts index f309b0d2b2..e1f7872797 100644 --- a/lib/workers/repository/onboarding/pr/index.spec.ts +++ b/lib/workers/repository/onboarding/pr/index.spec.ts @@ -1,3 +1,4 @@ +import type { RequestError, Response } from 'got'; import { RenovateConfig, defaultConfig, @@ -203,5 +204,38 @@ describe('workers/repository/onboarding/pr/index', () => { 'DRY-RUN: Would create onboarding PR' ); }); + + describe('ensureOnboardingPr() throws', () => { + const response = partial<Response>({ statusCode: 422 }); + const err = partial<RequestError>({ response }); + + beforeEach(() => { + jest.resetAllMocks(); + GlobalConfig.reset(); + git.deleteBranch.mockResolvedValue(); + }); + + it('throws when trying to create a new PR', async () => { + platform.createPr.mockRejectedValueOnce(err); + await expect( + ensureOnboardingPr(config, packageFiles, branches) + ).toReject(); + expect(git.deleteBranch).toHaveBeenCalledTimes(0); + }); + + it('deletes branch when PR already exists but cannot find it', async () => { + err.response.body = { + errors: [{ message: 'A pull request already exists' }], + }; + platform.createPr.mockRejectedValueOnce(err); + await expect( + ensureOnboardingPr(config, packageFiles, branches) + ).toResolve(); + expect(logger.warn).toHaveBeenCalledWith( + 'Onboarding PR already exists but cannot find it. It was probably created by a different user.' + ); + expect(git.deleteBranch).toHaveBeenCalledTimes(1); + }); + }); }); }); diff --git a/lib/workers/repository/onboarding/pr/index.ts b/lib/workers/repository/onboarding/pr/index.ts index 945d0644de..8952bf5936 100644 --- a/lib/workers/repository/onboarding/pr/index.ts +++ b/lib/workers/repository/onboarding/pr/index.ts @@ -152,14 +152,16 @@ If you need any further assistance then you can also [request help here](${confi logger.info({ pr: pr.displayNumber }, 'Onboarding PR created'); await addParticipants(config, pr); } - } catch (err) /* istanbul ignore next */ { + } catch (err) { if ( - err.statusCode === 422 && + err.response?.statusCode === 422 && err.response?.body?.errors?.[0]?.message?.startsWith( 'A pull request already exists' ) ) { - logger.debug('Onboarding PR already exists but cannot find it'); + logger.warn( + 'Onboarding PR already exists but cannot find it. It was probably created by a different user.' + ); await deleteBranch(config.onboardingBranch); return; } -- GitLab