fix: remove custom fallback

Signed-off-by: ElaineDeMattosSilvaB <elaine.de-mattos-silva-bezerra@deutschebahn.com>
This commit is contained in:
ElaineDeMattosSilvaB
2024-05-20 22:24:11 +02:00
parent 2c3f493ee3
commit 03afcf1db5
4 changed files with 123 additions and 29 deletions
@@ -165,7 +165,7 @@ const httpProjectCatalogDynamic = all_projects_response.map(project => {
`${apiBaseUrl}/projects/${path}/repository/files/catalog-info.yaml`,
(req, res, ctx) => {
const branch = req.url.searchParams.get('ref');
if (branch === project.default_branch) {
if (branch === (project.default_branch || 'main' || 'develop')) {
return res(ctx.status(200));
}
return res(ctx.status(404, 'Not Found'));
@@ -190,6 +190,33 @@ export const config_single_integration_branch: MockObject = {
},
};
export const config_single_integration_specific_branch: MockObject = {
integrations: {
gitlab: [
{
host: 'example.com',
apiBaseUrl: 'https://example.com/api/v4',
token: '1234',
},
],
},
catalog: {
providers: {
gitlab: {
'test-id': {
host: 'example.com',
group: 'group1',
branch: 'develop',
skipForkedRepos: false,
schedule: {
frequency: 'PT30M',
timeout: 'PT3M',
},
},
},
},
},
};
export const config_single_integration_group: MockObject = {
integrations: {
gitlab: [
@@ -234,7 +261,7 @@ export const config_fallbackBranch_branch: MockObject = {
'test-id': {
host: 'example.com',
group: 'group1',
fallbackBranch: 'staging',
fallbackBranch: 'main',
skipForkedRepos: false,
schedule: {
frequency: 'PT30M',
@@ -634,7 +661,7 @@ export const all_projects_response: GitLabProject[] = [
web_url: 'https://example.com/group1/test-repo5-staging',
path_with_namespace: 'group1/test-repo5-staging',
},
// diffrent group
// different group
{
id: 6,
description: 'Project Six Description',
@@ -646,6 +673,17 @@ export const all_projects_response: GitLabProject[] = [
web_url: 'https://example.com/group1/test-repo6',
path_with_namespace: 'awesome-group/test-repo6',
},
// no default branch
{
id: 7,
description: 'Project Seven Description',
name: 'test-repo7',
path: 'test-repo7',
archived: false,
last_activity_at: new Date().toString(),
web_url: 'https://example.com/group1/test-repo7',
path_with_namespace: 'group1/test-repo7',
},
];
export const all_users_response: GitLabUser[] = [
@@ -1299,9 +1337,43 @@ export const push_modif_event: EventParams = {
/**
* Expected Backstage entities
*/
export const expected_location_entities: MockObject[] =
// includes only projects that have a default branch (for when the branch and default branch were not set in the config)
export const expected_location_entities_default_branch: MockObject[] =
all_projects_response
.filter(project => project.default_branch)
.map(project => {
const targetUrl = `https://example.com/${project.path_with_namespace}/-/blob/${project.default_branch}/catalog-info.yaml`;
return {
entity: {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Location',
metadata: {
annotations: {
'backstage.io/managed-by-location': `url:${targetUrl}`,
'backstage.io/managed-by-origin-location': `url:${targetUrl}`,
},
name: locationSpecToMetadataName({
target: targetUrl,
type: 'url',
}),
},
spec: {
presence: 'optional',
target: targetUrl,
type: 'url',
},
},
locationKey: 'GitlabDiscoveryEntityProvider:test-id',
};
});
// includes every GitLab project that has a default branch and the fallback declared in the config
export const expected_location_entities_fallback_branch: MockObject[] =
all_projects_response.map(project => {
const targetUrl = `https://example.com/${project.path_with_namespace}/-/blob/${project.default_branch}/catalog-info.yaml`;
const branch = project.default_branch || 'main';
const targetUrl = `https://example.com/${project.path_with_namespace}/-/blob/${branch}/catalog-info.yaml`;
return {
entity: {
@@ -1312,7 +1384,40 @@ export const expected_location_entities: MockObject[] =
'backstage.io/managed-by-location': `url:${targetUrl}`,
'backstage.io/managed-by-origin-location': `url:${targetUrl}`,
},
name: locationSpecToMetadataName({ target: targetUrl, type: 'url' }),
name: locationSpecToMetadataName({
target: targetUrl,
type: 'url',
}),
},
spec: {
presence: 'optional',
target: targetUrl,
type: 'url',
},
},
locationKey: 'GitlabDiscoveryEntityProvider:test-id',
};
});
// includes ONLY the projects with the branch declared in the config
export const expected_location_entities_specific_branch: MockObject[] =
all_projects_response.map(project => {
const branch = 'develop';
const targetUrl = `https://example.com/${project.path_with_namespace}/-/blob/${branch}/catalog-info.yaml`;
return {
entity: {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Location',
metadata: {
annotations: {
'backstage.io/managed-by-location': `url:${targetUrl}`,
'backstage.io/managed-by-origin-location': `url:${targetUrl}`,
},
name: locationSpecToMetadataName({
target: targetUrl,
type: 'url',
}),
},
spec: {
presence: 'optional',
@@ -153,7 +153,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(1);
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(
entities: mock.expected_location_entities_default_branch.filter(
entity =>
!entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
@@ -187,7 +187,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(
entities: mock.expected_location_entities_default_branch.filter(
entity =>
entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
@@ -217,7 +217,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(
entities: mock.expected_location_entities_default_branch.filter(
entity =>
!entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
@@ -229,8 +229,10 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
});
});
it('should filter found projects based on the branch', async () => {
const config = new ConfigReader(mock.config_single_integration_branch);
it('should only ingest projects from specific branch', async () => {
const config = new ConfigReader(
mock.config_single_integration_specific_branch,
);
const schedule = new PersistingTaskRunner();
const entityProviderConnection: EntityProviderConnection = {
applyMutation: jest.fn(),
@@ -251,7 +253,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(
entities: mock.expected_location_entities_specific_branch.filter(
entity =>
entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
@@ -263,7 +265,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
});
});
it('should only include projects with fallback branch', async () => {
it('should include projects from fallback branch', async () => {
const config = new ConfigReader(mock.config_fallbackBranch_branch);
const schedule = new PersistingTaskRunner();
const entityProviderConnection: EntityProviderConnection = {
@@ -275,21 +277,14 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
schedule,
})[0];
const configured_branch =
mock.config_fallbackBranch_branch.catalog.providers.gitlab['test-id']
.fallbackBranch;
await provider.connect(entityProviderConnection);
await provider.refresh(logger);
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(
entities: mock.expected_location_entities_fallback_branch.filter(
entity =>
entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
].includes(configured_branch) &&
!entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
].includes('awesome'),
@@ -319,7 +314,7 @@ describe('GitlabDiscoveryEntityProvider - refresh', () => {
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'full',
entities: mock.expected_location_entities.filter(entity =>
entities: mock.expected_location_entities_default_branch.filter(entity =>
entity.entity.metadata.annotations[
'backstage.io/managed-by-location'
].includes(configured_group),
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { LoggerService } from '@backstage/backend-plugin-api';
import { PluginTaskScheduler, TaskRunner } from '@backstage/backend-tasks';
import { Config } from '@backstage/config';
import { GitLabIntegration, ScmIntegrations } from '@backstage/integration';
@@ -34,7 +35,6 @@ import {
paginated,
readGitlabConfigs,
} from '../lib';
import { LoggerService } from '@backstage/backend-plugin-api';
import * as path from 'path';
@@ -470,14 +470,8 @@ export class GitlabDiscoveryEntityProvider implements EntityProvider {
return false;
}
const customFallbackBranch =
this.config.fallbackBranch !== 'master'
? this.config.fallbackBranch
: undefined;
const project_branch =
this.config.branch ??
customFallbackBranch ??
project.default_branch ??
this.config.fallbackBranch;