Skip to content

Commit

Permalink
moar fixes for [gitlab] auth (#8162)
Browse files Browse the repository at this point in the history
* fix auth in gitlab contributors badge

* fix gitlab auth for badges that use fetchPaginatedArrayData()

* add a test covering fetchPaginatedArrayData auth

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
chris48s and repo-ranger[bot] authored Jul 2, 2022
1 parent 3998055 commit 076ee25
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
14 changes: 8 additions & 6 deletions services/gitlab/gitlab-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ export default class GitLabBase extends BaseJsonService {
}

async fetchPage({ page, requestParams, schema }) {
const { res, buffer } = await this._request({
...requestParams,
...{ options: { searchParams: { page } } },
})
const { res, buffer } = await this._request(
this.authHelper.withBearerAuthHeader({
...requestParams,
...{ options: { searchParams: { page } } },
})
)

const json = this._parseJson(buffer)
const data = this.constructor._validate(json, schema)
Expand All @@ -35,15 +37,15 @@ export default class GitLabBase extends BaseJsonService {
errorMessages,
firstPageOnly = false,
}) {
const requestParams = this.authHelper.withBasicAuth({
const requestParams = {
url,
options: {
headers: { Accept: 'application/json' },
searchParams: { per_page: 100 },
...options,
},
errorMessages,
})
}

const {
res: { headers },
Expand Down
20 changes: 11 additions & 9 deletions services/gitlab/gitlab-contributors.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ export default class GitlabContributors extends GitLabBase {

async handle({ project }, { gitlab_url: baseUrl = 'https://gitlab.com' }) {
// https://docs.gitlab.com/ee/api/repositories.html#contributors
const { res } = await this._request({
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(
project
)}/repository/contributors`,
options: { searchParams: { page: '1', per_page: '1' } },
errorMessages: {
404: 'project not found',
},
})
const { res } = await this._request(
this.authHelper.withBearerAuthHeader({
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(
project
)}/repository/contributors`,
options: { searchParams: { page: '1', per_page: '1' } },
errorMessages: {
404: 'project not found',
},
})
)
const data = this.constructor._validate(res.headers, schema)
// The total number of contributors is in the `x-total` field in the headers.
// https://docs.gitlab.com/ee/api/index.html#other-pagination-headers
Expand Down
11 changes: 11 additions & 0 deletions services/gitlab/gitlab-contributors.tester.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createServiceTester } from '../tester.js'
import { isMetric } from '../test-validators.js'
import { noToken } from '../test-helpers.js'
import _noGitLabToken from './gitlab-contributors.service.js'
export const t = await createServiceTester()
const noGitLabToken = noToken(_noGitLabToken)

t.create('Contributors')
.get('/guoxudong.io/shields-test/licenced-test.json')
Expand Down Expand Up @@ -29,3 +32,11 @@ t.create('Mocking the missing x-total header')
label: 'contributors',
message: 'invalid response data',
})

t.create('Contributors (private repo)')
.skipWhen(noGitLabToken)
.get('/shields-ops-group/test.json')
.expectBadge({
label: 'contributors',
message: isMetric,
})
48 changes: 48 additions & 0 deletions services/gitlab/gitlab-release.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect } from 'chai'
import nock from 'nock'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import GitLabRelease from './gitlab-release.service.js'

describe('GitLabRelease', function () {
describe('auth', function () {
cleanUpNockAfterEach()

const fakeToken = 'abc123'
const config = {
public: {
services: {
gitlab: {
authorizedOrigins: ['https://gitlab.com'],
},
},
},
private: {
gitlab_token: fakeToken,
},
}

it('sends the auth information as configured', async function () {
const scope = nock('https://gitlab.com/')
.get('/api/v4/projects/foo%2Fbar/releases?page=1')
// This ensures that the expected credentials are actually being sent with the HTTP request.
// Without this the request wouldn't match and the test would fail.
.matchHeader('Authorization', `Bearer ${fakeToken}`)
.reply(200, [{ name: '1.9', tag_name: '1.9' }])

expect(
await GitLabRelease.invoke(
defaultContext,
config,
{ project: 'foo/bar' },
{}
)
).to.deep.equal({
label: undefined,
message: 'v1.9',
color: 'blue',
})

scope.done()
})
})
})

0 comments on commit 076ee25

Please sign in to comment.