Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support [CodeClimate] responses with multiple data items #7716

Merged
merged 2 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally curious what the response would represent if there were multiple entries that had test reports 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll know when and if a future user complains! 😄

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