From 4ea03ac0b4b06996df61ea815a6976190a58e08c Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Thu, 13 Jun 2019 07:25:39 +0200 Subject: [PATCH] feat(gitlab): detect gitAuthor as part of initPlatform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitLab self-hosted users now no longer need to manually configure gitAuthor if it matches with the bot account they’re using. --- lib/platform/gitlab/gl-got-wrapper.ts | 1 + lib/platform/gitlab/index.ts | 12 +++++- .../gitlab/__snapshots__/index.spec.ts.snap | 2 + test/platform/gitlab/index.spec.ts | 41 +++++++++++++++---- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/platform/gitlab/gl-got-wrapper.ts b/lib/platform/gitlab/gl-got-wrapper.ts index c014e2452c..90f5dfe315 100644 --- a/lib/platform/gitlab/gl-got-wrapper.ts +++ b/lib/platform/gitlab/gl-got-wrapper.ts @@ -45,6 +45,7 @@ const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; interface IGlGotApi extends IGotApi<{ paginate?: boolean; + token?: string; }> { setBaseUrl(url: string): void; } diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index 34a4e331ad..18ae6192ae 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -22,7 +22,7 @@ const defaults = { endpoint: 'https://gitlab.com/api/v4/', }; -export function initPlatform({ +export async function initPlatform({ endpoint, token, }: { @@ -41,7 +41,15 @@ export function initPlatform({ res.endpoint = defaults.endpoint; logger.info('Using default GitLab endpoint: ' + res.endpoint); } - // TODO: Add a connection check that endpoint/token combination are valid + try { + res.gitAuthor = (await api.get(`user`, { token })).body.email; + } catch (err) { + logger.info( + { err }, + 'Error authenticating with GitLab. Check that your token includes "user" permissions' + ); + throw new Error('Init: Authentication failure'); + } return res; } diff --git a/test/platform/gitlab/__snapshots__/index.spec.ts.snap b/test/platform/gitlab/__snapshots__/index.spec.ts.snap index 1e7926b981..ade9654843 100644 --- a/test/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/test/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -246,12 +246,14 @@ Array [ exports[`platform/gitlab initPlatform() should accept custom endpoint 1`] = ` Object { "endpoint": "https://gitlab.renovatebot.com/", + "gitAuthor": "a@b.com", } `; exports[`platform/gitlab initPlatform() should default to gitlab.com 1`] = ` Object { "endpoint": "https://gitlab.com/api/v4/", + "gitAuthor": "a@b.com", } `; diff --git a/test/platform/gitlab/index.spec.ts b/test/platform/gitlab/index.spec.ts index 8a04db76fa..eca2179b98 100644 --- a/test/platform/gitlab/index.spec.ts +++ b/test/platform/gitlab/index.spec.ts @@ -48,19 +48,44 @@ describe('platform/gitlab', () => { }); describe('initPlatform()', () => { - it(`should throw if no token`, () => { - expect(() => { - gitlab.initPlatform({} as any); - }).toThrow(); + it(`should throw if no token`, async () => { + await expect(gitlab.initPlatform({} as any)).rejects.toThrow(); }); - it(`should default to gitlab.com`, () => { - expect( + it(`should throw if auth fails`, async () => { + // user + api.get.mockImplementationOnce(() => { + throw new Error('403'); + }); + await expect( gitlab.initPlatform({ token: 'some-token' } as any) + ).rejects.toThrow(); + }); + it(`should default to gitlab.com`, async () => { + // user + api.get.mockImplementationOnce( + () => + ({ + body: { + email: 'a@b.com', + }, + } as any) + ); + expect( + await gitlab.initPlatform({ token: 'some-token' } as any) ).toMatchSnapshot(); }); - it(`should accept custom endpoint`, () => { + it(`should accept custom endpoint`, async () => { + // user + api.get.mockImplementationOnce( + () => + ({ + body: { + email: 'a@b.com', + }, + } as any) + ); expect( - gitlab.initPlatform({ + await gitlab.initPlatform({ endpoint: 'https://gitlab.renovatebot.com', token: 'some-token', }) -- GitLab