Skip to content

Commit 1721d30

Browse files
authoredNov 17, 2023
fix: update projectUrl and add more logs (#2955)
* fix: fix projectUrl add more logs * fix: * fix: linting * fix: remove some logs * fix: add tests * fix: remove duplicate test
1 parent d5482d7 commit 1721d30

File tree

2 files changed

+78
-43
lines changed

2 files changed

+78
-43
lines changed
 

‎plugins/coverage/token.js

+47-38
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ module.exports = config => ({
6464
throw boom.notFound(`Pipeline ${pipelineId} does not exist`);
6565
}
6666

67+
logger.info(`looking up pipeline from:${pipelineId}, and found pipeline:${pipeline}`);
68+
6769
tokenConfig.pipelineName = pipeline.name;
6870
}
6971

@@ -82,50 +84,57 @@ module.exports = config => ({
8284

8385
const selfSonar = new CoveragePlugin(selfSonarConfig);
8486
const data = await selfSonar.coveragePlugin.getAccessToken(tokenConfig);
85-
const projectUrl = selfSonar.coveragePlugin.getProjectData(tokenConfig);
86-
87-
if (pipeline && projectUrl) {
88-
try {
89-
const pipelineSonarBadge = {
90-
defaultName: pipelineId,
91-
defaultUri: projectUrl
92-
};
93-
let shouldPipelineUpdate = true;
94-
95-
if (
96-
pipeline.badges &&
97-
pipeline.badges.sonar &&
98-
pipeline.badges.sonar.defaultName === pipelineId &&
99-
pipeline.badges.sonar.defaultUri === projectUrl
100-
) {
101-
shouldPipelineUpdate = false;
102-
}
10387

104-
if (shouldPipelineUpdate) {
105-
if (pipeline.badges) {
106-
pipeline.badges.sonar = pipelineSonarBadge;
107-
} else {
108-
pipeline.badges = {
109-
sonar: pipelineSonarBadge
110-
};
111-
}
112-
113-
await pipeline.update();
114-
logger.info(
115-
`update pipeline:${pipeline.id}'s sonar badge with defaultName:${pipelineId}, defaultUri: ${projectUrl}`
116-
);
88+
return h.response(data);
89+
}
90+
91+
const data = await config.coveragePlugin.getAccessToken(tokenConfig);
92+
const { projectUrl } = config.coveragePlugin.getProjectData(tokenConfig);
93+
94+
if (!pipeline && pipelineId) {
95+
pipeline = await pipelineFactory.get(pipelineId);
96+
97+
logger.info(`looking up again, pipeline:${pipelineId}, and found pipeline: ${pipeline}`);
98+
}
99+
100+
if (pipeline && projectUrl) {
101+
try {
102+
const pipelineSonarBadge = {
103+
defaultName: `${pipelineId}`, // ensure pipelineId is stored as String instead of Integer
104+
defaultUri: projectUrl
105+
};
106+
let shouldPipelineUpdate = true;
107+
108+
if (
109+
pipeline.badges &&
110+
pipeline.badges.sonar &&
111+
pipeline.badges.sonar.defaultName === pipelineId &&
112+
pipeline.badges.sonar.defaultUri === projectUrl
113+
) {
114+
shouldPipelineUpdate = false;
115+
}
116+
117+
if (shouldPipelineUpdate) {
118+
if (pipeline.badges) {
119+
pipeline.badges.sonar = pipelineSonarBadge;
120+
} else {
121+
pipeline.badges = {
122+
sonar: pipelineSonarBadge
123+
};
117124
}
118-
} catch (err) {
119-
logger.error(`Failed to update pipeline:${pipelineId}`, err);
120125

121-
throw err;
126+
await pipeline.update();
122127
}
123-
}
124128

125-
return h.response(data);
126-
}
129+
logger.info(
130+
`update pipeline:${pipeline.id}'s sonar badge with pipeline.badges, ${pipeline.badges}`
131+
);
132+
} catch (err) {
133+
logger.error(`Failed to update pipeline:${pipelineId}`, err);
127134

128-
const data = await config.coveragePlugin.getAccessToken(tokenConfig);
135+
throw err;
136+
}
137+
}
129138

130139
return h.response(data);
131140
}

‎test/plugins/coverage.test.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('coverage plugin test', () => {
3232
sonarGitAppName: 'test'
3333
},
3434
getAccessToken: sinon.stub().resolves('faketoken'),
35-
getProjectData: sinon.stub().resolves({ projectUrl: 'aabbcc', pipelineId: 1 }),
35+
getProjectData: sinon
36+
.stub()
37+
.returns({ projectUrl: 'https://sonar.sd.cd/dashboard?id=pipeline%3A333', pipelineId: 1 }),
3638
getInfo: sinon.stub()
3739
};
3840
jobFactoryMock = {
@@ -90,15 +92,23 @@ describe('coverage plugin test', () => {
9092

9193
describe('GET /coverage/token', () => {
9294
let options;
95+
let pipelineMock;
9396

9497
beforeEach(() => {
98+
pipelineMock = {
99+
id: 333,
100+
name: 'd2lam/test',
101+
update: sinon.stub()
102+
};
103+
95104
options = {
96105
url: '/coverage/token',
97106
auth: {
98107
credentials,
99108
strategy: ['token']
100109
}
101110
};
111+
102112
jobFactoryMock.get.resolves({
103113
permutations: [
104114
{
@@ -108,9 +118,8 @@ describe('coverage plugin test', () => {
108118
name: 'main',
109119
isPR: sinon.stub().returns(false)
110120
});
111-
pipelineFactoryMock.get.resolves({
112-
name: 'd2lam/test'
113-
});
121+
122+
pipelineFactoryMock.get.resolves(pipelineMock);
114123
});
115124

116125
it('returns 200', () => {
@@ -193,6 +202,23 @@ describe('coverage plugin test', () => {
193202
});
194203
});
195204

205+
it('returns 200 with pipeline scope with projectKey to update pipeline projectUrl', () => {
206+
options.url = '/coverage/token?projectKey=pipeline:333';
207+
208+
const expectedBadges = {
209+
sonar: {
210+
defaultName: '333',
211+
defaultUri: 'https://sonar.sd.cd/dashboard?id=pipeline%3A333'
212+
}
213+
};
214+
215+
return server.inject(options).then(reply => {
216+
assert.equal(reply.statusCode, 200);
217+
assert.calledOnce(pipelineMock.update);
218+
assert.deepEqual(pipelineMock.badges, expectedBadges);
219+
});
220+
});
221+
196222
it('returns 200 with coverage scope query param for PR', () => {
197223
jobFactoryMock.get.resolves({
198224
permutations: [{}],
@@ -313,7 +339,7 @@ describe('coverage plugin test', () => {
313339
};
314340
const result = {
315341
coverage: '98.8',
316-
projectUrl: 'https://sonar.sd.cd/dashboard?id=job%3A123'
342+
projectUrl: 'https://sonar.sd.cd/dashboard?id=pipeline%3A333'
317343
};
318344

319345
beforeEach(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.