Skip to content

Commit

Permalink
fix: other branch pipeline link on pipeline header does not include a…
Browse files Browse the repository at this point in the history
…ll of sibling pipelines (#1274)
  • Loading branch information
y-oksaku authored Dec 3, 2024
1 parent 0b0aaa5 commit 3b85b33
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 25 deletions.
65 changes: 42 additions & 23 deletions app/components/pipeline-header/component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { get, computed } from '@ember/object';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import ENV from 'screwdriver-ui/config/environment';

export default Component.extend({
showCollectionModal: false,
Expand Down Expand Up @@ -62,32 +63,50 @@ export default Component.extend({
// Disabling the eslint rule for computed properties as that causes rendering issues and an infinite loop to the API call with the current setup of ember data
// eslint-disable-next-line ember/require-computed-property-dependencies
sameRepoPipeline: computed('pipeline', {
get() {
async get() {
const [scm, repositoryId] = this.pipeline.scmUri.split(':');
const pipelineName = this.pipeline.scmRepo.name;
const sameRepoPipelines = [];

let page = 0;

do {
page += 1;

const siblingPipelines = (
await this.pipelineService.getSiblingPipeline(
this.pipeline.scmRepo.name,
page
)
).toArray();

return this.pipelineService
.getSiblingPipeline(this.pipeline.scmRepo.name)
.then(value =>
value
.toArray()
.filter(pipe => {
const [s, r] = pipe.scmUri.split(':');
sameRepoPipelines.push(...siblingPipelines);

return (
pipe.id !== this.pipeline.id && scm === s && repositoryId === r
);
})
.map((pipe, i) => ({
index: i,
url: `/pipelines/${pipe.id}`,
branchAndRootDir: pipe.scmRepo.rootDir
? `${pipe.scmRepo.branch}:${pipe.scmRepo.rootDir}`
: pipe.scmRepo.branch
}))
.sort((l, r) =>
l.branchAndRootDir.localeCompare(r.branchAndRootDir)
)
);
if (siblingPipelines.length < ENV.APP.NUM_PIPELINES_LISTED) {
break;
}
} while (
sameRepoPipelines[sameRepoPipelines.length - 1].scmRepo.name ===
pipelineName &&
page <= 10
);

return sameRepoPipelines
.filter(pipe => {
const [s, r] = pipe.scmUri.split(':');

return (
pipe.id !== this.pipeline.id && scm === s && repositoryId === r
);
})
.map((pipe, i) => ({
index: i,
url: `/pipelines/${pipe.id}`,
branchAndRootDir: pipe.scmRepo.rootDir
? `${pipe.scmRepo.branch}:${pipe.scmRepo.rootDir}`
: pipe.scmRepo.branch
}))
.sort((l, r) => l.branchAndRootDir.localeCompare(r.branchAndRootDir));
}
}),
actions: {
Expand Down
4 changes: 2 additions & 2 deletions app/pipeline/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default Service.extend({
this.set('buildsLink', buildsLink);
},

getSiblingPipeline(scmRepo) {
getSiblingPipeline(scmRepo, page) {
const pipelineListConfig = {
page: 1,
page,
count: ENV.APP.NUM_PIPELINES_LISTED,
search: scmRepo,
sortBy: 'name',
Expand Down
116 changes: 116 additions & 0 deletions tests/integration/components/pipeline-header/component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,122 @@ module('Integration | Component | pipeline header', function (hooks) {
assert.dom('li.branch-item:nth-child(4)').hasText('link efg:src');
});

test('it renders branch-move when API returns multiple pages', async function (assert) {
const pipelineMock = EmberObject.create({
id: 1,
scmContext: 'github:github.com',
scmUri: 'github.com:123456:master',
scmRepo: {
name: 'batman/batmobile'
}
});

const createSiblingPipelineMock = id => {
return {
id,
scmRepo: {
name: pipelineMock.scmRepo.name,
branch: `branch-${id}`
},
scmUri: pipelineMock.scmUri
};
};

// In test, NUM_PIPELINES_LISTED is 3
const firstPageSiblingPipelines = [
pipelineMock,
createSiblingPipelineMock(2),
createSiblingPipelineMock(3)
];
const secondPageSiblingPipelines = [
createSiblingPipelineMock(4),
createSiblingPipelineMock(5),
createSiblingPipelineMock(6)
];

const pipelineStub = Service.extend({
getSiblingPipeline(_, page) {
if (page === 1) return Promise.resolve(firstPageSiblingPipelines);
if (page === 2) return Promise.resolve(secondPageSiblingPipelines);
if (page === 3) return Promise.resolve([]);

return assert.fails('This line should not run.');
}
});

this.owner.unregister('service:pipeline');
this.owner.register('service:pipeline', pipelineStub);

injectScmServiceStub(this);

this.set('pipelineMock', pipelineMock);
await render(hbs`<PipelineHeader @pipeline={{this.pipelineMock}} />`);

await click('span.branch');
assert.dom('li.branch-item').exists({ count: 5 });
});

test('it renders branch-move when API returns multiple pages with other name pipeline', async function (assert) {
const pipelineMock = EmberObject.create({
id: 1,
scmContext: 'github:github.com',
scmUri: 'github.com:123456:master',
scmRepo: {
name: 'batman/batmobile'
}
});

const createSiblingPipelineMock = id => {
return {
id,
scmRepo: {
name: pipelineMock.scmRepo.name,
branch: `branch-${id}`
},
scmUri: pipelineMock.scmUri
};
};

// In test, NUM_PIPELINES_LISTED is 3
const firstPageSiblingPipelines = [
pipelineMock,
createSiblingPipelineMock(2),
createSiblingPipelineMock(3)
];
const secondPageSiblingPipelines = [
createSiblingPipelineMock(4),
createSiblingPipelineMock(5),
{
id: 6,
scmRepo: {
name: 'batman/batmobile-other',
branch: `main`
},
scmUri: 'github.com:789:master'
}
];

const pipelineStub = Service.extend({
getSiblingPipeline(_, page) {
if (page === 1) return Promise.resolve(firstPageSiblingPipelines);
if (page === 2) return Promise.resolve(secondPageSiblingPipelines);

return assert.fails('This line should not run.');
}
});

this.owner.unregister('service:pipeline');
this.owner.register('service:pipeline', pipelineStub);

injectScmServiceStub(this);

this.set('pipelineMock', pipelineMock);
await render(hbs`<PipelineHeader @pipeline={{this.pipelineMock}} />`);

await click('span.branch');
assert.dom('li.branch-item').exists({ count: 4 });
});

test('it renders link to parent pipeline for child pipeline', async function (assert) {
const pipelineMock = EmberObject.create({
appId: 'batman/batmobile',
Expand Down

0 comments on commit 3b85b33

Please sign in to comment.