diff --git a/lib/platform/gitlab/gl-got-wrapper.ts b/lib/platform/gitlab/gl-got-wrapper.ts index c014e2452cdd9ab5beb64d5eea2052b213c8af35..90f5dfe31527a193f244a12c7e55529e30336ea8 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 34a4e331adb83979282481878f594a15f2bdabc3..18ae6192ae03c4e701ff366224ace605d14286e7 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 1e7926b9818ef44328cf624b81e1527a9b60864e..ade9654843a2d1812b84723a0a6f035e4a77855d 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 8a04db76fa22f17afd81869b08ff6d2c278cefbd..eca2179b98145b92e14110a1a3179d5022a436f7 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', })