Skip to content

Commit

Permalink
Support [CodeClimate] responses with multiple data items (#7716)
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB authored Mar 12, 2022
1 parent 5a31a90 commit facd0d6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
12 changes: 8 additions & 4 deletions services/codeclimate/codeclimate-analysis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ export default class CodeclimateAnalysis extends BaseJsonService {
}

async fetch({ user, repo }) {
const repoInfos = await fetchRepo(this, { user, repo })
const repoInfosWithSnapshot = repoInfos.filter(
repoInfo => repoInfo.relationships.latest_default_branch_snapshot.data
)
if (repoInfosWithSnapshot.length === 0) {
throw new NotFound({ prettyMessage: 'snapshot not found' })
}
const {
id: repoId,
relationships: {
latest_default_branch_snapshot: { data: snapshotInfo },
},
} = await fetchRepo(this, { user, repo })
if (snapshotInfo === null) {
throw new NotFound({ prettyMessage: 'snapshot not found' })
}
} = repoInfosWithSnapshot[0]
const { data } = await this._requestJson({
schema,
url: `https://api.codeclimate.com/v1/repos/${repoId}/snapshots/${snapshotInfo.id}`,
Expand Down
41 changes: 41 additions & 0 deletions services/codeclimate/codeclimate-analysis.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@ t.create('maintainability letter')
message: Joi.equal('A', 'B', 'C', 'D', 'E', 'F'),
})

t.create('issues when outer user repos query returns multiple items')
.get('/issues/angular/angular.json')
.intercept(nock =>
nock('https://api.codeclimate.com', { allowUnmocked: true })
.get('/v1/repos?github_slug=angular%2Fangular')
.reply(200, {
data: [
{
id: '54fd4e6b6956804a10003df4',
relationships: {
latest_default_branch_snapshot: {
data: null,
},
latest_default_branch_test_report: {
data: null,
},
},
},
{
id: '54fd4e6b6956804a10003df3',
relationships: {
latest_default_branch_snapshot: {
data: {
id: '620e2b491b6a72000100ca1d',
type: 'snapshots',
},
},
latest_default_branch_test_report: {
data: null,
},
},
},
],
})
)
.networkOn() // Combined with allowUnmocked: true, this allows the inner snapshots query to go through.
.expectBadge({
label: 'issues',
message: Joi.number().integer().positive(),
})

t.create('maintainability letter for non-existent repo')
.get('/maintainability/unknown/unknown.json')
.expectBadge({
Expand Down
9 changes: 3 additions & 6 deletions services/codeclimate/codeclimate-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const isLetterGrade = Joi.equal('A', 'B', 'C', 'D', 'E', 'F').required()

const repoSchema = Joi.object({
data: Joi.array()
.max(1)
.items(
Joi.object({
id: Joi.string().required(),
Expand All @@ -29,17 +28,15 @@ const repoSchema = Joi.object({
}).required()

async function fetchRepo(serviceInstance, { user, repo }) {
const {
data: [repoInfo],
} = await serviceInstance._requestJson({
const { data: repoInfos } = await serviceInstance._requestJson({
schema: repoSchema,
url: 'https://api.codeclimate.com/v1/repos',
options: { searchParams: { github_slug: `${user}/${repo}` } },
})
if (repoInfo === undefined) {
if (repoInfos.length === 0) {
throw new NotFound({ prettyMessage: 'repo not found' })
}
return repoInfo
return repoInfos
}

export { keywords, isLetterGrade, fetchRepo }
12 changes: 8 additions & 4 deletions services/codeclimate/codeclimate-coverage.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ export default class CodeclimateCoverage extends BaseJsonService {
}

async fetch({ user, repo }) {
const repoInfos = await fetchRepo(this, { user, repo })
const repoInfosWithTestReport = repoInfos.filter(
repoInfo => repoInfo.relationships.latest_default_branch_test_report.data
)
if (repoInfosWithTestReport.length === 0) {
throw new NotFound({ prettyMessage: 'test report not found' })
}
const {
id: repoId,
relationships: {
latest_default_branch_test_report: { data: testReportInfo },
},
} = await fetchRepo(this, { user, repo })
if (testReportInfo === null) {
throw new NotFound({ prettyMessage: 'test report not found' })
}
} = repoInfosWithTestReport[0]
const { data } = await this._requestJson({
schema,
url: `https://api.codeclimate.com/v1/repos/${repoId}/test_reports/${testReportInfo.id}`,
Expand Down
41 changes: 41 additions & 0 deletions services/codeclimate/codeclimate-coverage.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,47 @@ t.create('test coverage letter')
message: Joi.equal('A', 'B', 'C', 'D', 'E', 'F'),
})

t.create('test coverage when outer user repos query returns multiple items')
.get('/coverage/codeclimate/codeclimate.json')
.intercept(nock =>
nock('https://api.codeclimate.com', { allowUnmocked: true })
.get('/v1/repos?github_slug=codeclimate%2Fcodeclimate')
.reply(200, {
data: [
{
id: '558479d6e30ba034120008a8',
relationships: {
latest_default_branch_snapshot: {
data: null,
},
latest_default_branch_test_report: {
data: null,
},
},
},
{
id: '558479d6e30ba034120008a9',
relationships: {
latest_default_branch_snapshot: {
data: null,
},
latest_default_branch_test_report: {
data: {
id: '62110434a7160b00010b4b59',
type: 'test_reports',
},
},
},
},
],
})
)
.networkOn() // Combined with allowUnmocked: true, this allows the inner test reports query to go through.
.expectBadge({
label: 'coverage',
message: isIntegerPercentage,
})

t.create('test coverage percentage for non-existent repo')
.get('/coverage/unknown/unknown.json')
.expectBadge({
Expand Down

0 comments on commit facd0d6

Please sign in to comment.