Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MergeRequestsSummary,
MergeRequestsStatusSummary,
} from './GitlabCIApi';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import {
ContributorData,
MergeRequest,
Expand All @@ -16,28 +16,34 @@ import {
export class GitlabCIClient implements GitlabCIApi {
discoveryApi: DiscoveryApi;
baseUrl: string;
identityApi: IdentityApi;
constructor({
discoveryApi,
baseUrl = 'https://gitlab.com/',
identityApi,
}: {
discoveryApi: DiscoveryApi;
baseUrl?: string;
identityApi: IdentityApi;
}) {
this.discoveryApi = discoveryApi;
this.baseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
this.identityApi = identityApi;
}

private async callApi<T>(
path: string,
query: { [key in string]: any },
query: { [key in string]: any }
): Promise<T | []> {
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/gitlabci`;
const { token } = await this.identityApi.getCredentials();
const response = await fetch(
`${apiUrl}/${path}?${new URLSearchParams(query).toString()}`,
{ headers: { Authorization: `Bearer ${token}` } }
);
if (response.status === 200) {
return (await response.json()) as T;
}
}
return [];
}

Expand Down Expand Up @@ -142,7 +148,7 @@ export class GitlabCIClient implements GitlabCIApi {

async getProjectDetails(projectSlug?: string): Promise<Object | undefined> {
let projectDetails: any;
if(projectSlug){
if (projectSlug) {
projectDetails = await this.callApi<Object>(
'projects/' + encodeURIComponent(projectSlug),
{},
Expand Down
22 changes: 14 additions & 8 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createPlugin,
import {
createPlugin,
createRoutableExtension,
createComponentExtension,
} from '@backstage/core-plugin-api';
} from '@backstage/core-plugin-api';

import {
configApiRef,
createApiFactory,
createRouteRef,
discoveryApiRef,
identityApiRef,
} from '@backstage/core-plugin-api';
import { GitlabCIApiRef, GitlabCIClient } from './api';

Expand All @@ -20,22 +22,26 @@ export const gitlabPlugin = createPlugin({
apis: [
createApiFactory({
api: GitlabCIApiRef,
deps: { configApi: configApiRef, discoveryApi: discoveryApiRef },
factory: ({ configApi, discoveryApi }) =>
deps: {
configApi: configApiRef,
discoveryApi: discoveryApiRef,
identityApi: identityApiRef,
},
factory: ({ configApi, discoveryApi, identityApi }) =>
new GitlabCIClient({
discoveryApi,
baseUrl: configApi.getOptionalString('gitlab.baseUrl'),
identityApi,
}),
}),
],
});

export const EntityGitlabContent = gitlabPlugin.provide(
createRoutableExtension({
component: () =>
import('./Router').then(m => m.Router),
component: () => import('./Router').then((m) => m.Router),
mountPoint: rootRouteRef,
}),
})
);

export const EntityGitlabLanguageCard = gitlabPlugin.provide(
Expand Down Expand Up @@ -86,4 +92,4 @@ export const EntityGitlabPipelinesTable = gitlabPlugin.provide(
import('./components/widgets/index').then((m) => m.PipelinesTable),
},
})
);
);