Skip to content

Commit

Permalink
feat: migrated all type to gitbeaker
Browse files Browse the repository at this point in the history
BREAKING CHANGE: all types are changed follow gitbeaker standards, if you are using these types you are to migrate them
  • Loading branch information
antoniomuso committed Jun 12, 2023
1 parent 8b586ce commit 8e644e3
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 220 deletions.
2 changes: 1 addition & 1 deletion packages/gitlab/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ createDevApp()
mockedGitlabReqToRes[
`${path}?${new URLSearchParams(query).toString()}`
];
return response || null;
return response || undefined;
};
return {
build: () => cli,
Expand Down
1 change: 1 addition & 0 deletions packages/gitlab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@backstage/test-utils": "^1.0.0",
"@commitlint/cli": "^17.0.0",
"@commitlint/config-conventional": "^17.0.0",
"@gitbeaker/rest": "^39.0.0",
"@nuxtjs/eslint-config-typescript": "^12.0.0",
"@saithodev/semantic-release-backmerge": "^2.0.0",
"@semantic-release/changelog": "^6.0.1",
Expand Down
43 changes: 18 additions & 25 deletions packages/gitlab/src/api/GitlabCIApi.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { createApiRef } from '@backstage/core-plugin-api';
import {
PeopleCardEntityData,
MergeRequest,
PipelineObject,
IssueObject,
ReleaseData,
ProjectDetails,
Languages,
} from '../components/types';
import { PeopleCardEntityData, Languages } from '../components/types';
import type {
IssueSchema,
MergeRequestSchema,
PipelineSchema,
ProjectSchema,
ReleaseSchema,
RepositoryContributorSchema,
UserSchema,
} from '@gitbeaker/rest';

export type PipelineSummary = PipelineObject[];
export type ContributorsSummary = PeopleCardEntityData[];

export type MergeRequestsSummary = MergeRequest[];

export type MergeRequestsStatusSummary = MergeRequest[];
export type ContributorsSummary = (RepositoryContributorSchema &
Partial<UserSchema>)[];

export type LanguagesSummary = Languages;

export type IssuesSummary = IssueObject[];

export type ReleasesSummary = ReleaseData[];

export const GitlabCIApiRef = createApiRef<GitlabCIBuilder>({
id: 'plugin.gitlabci.service',
});
Expand All @@ -33,33 +26,33 @@ export type GitlabCIBuilder = {
export type GitlabCIApi = {
getPipelineSummary(
projectID: string | number
): Promise<PipelineSummary | undefined>;
): Promise<PipelineSchema[] | undefined>;
getContributorsSummary(
projectID: string | number
): Promise<ContributorsSummary | undefined>;
getMergeRequestsSummary(
projectID: string | number
): Promise<MergeRequestsSummary | undefined>;
): Promise<MergeRequestSchema[] | undefined>;
getMergeRequestsStatusSummary(
projectID: string | number,
count: number
): Promise<MergeRequestsStatusSummary | undefined>;
): Promise<MergeRequestSchema[] | undefined>;
getProjectName(projectID: string | number): Promise<string | undefined>;
getLanguagesSummary(
projectID: string | number
): Promise<LanguagesSummary | undefined>;
getProjectDetails(projectSlug: string): Promise<ProjectDetails | undefined>;
getProjectDetails(projectSlug: string): Promise<ProjectSchema | undefined>;
getIssuesSummary(
projectID: string | number
): Promise<IssuesSummary | undefined>;
): Promise<IssueSchema[] | undefined>;
getCodeOwners(
projectID?: string | number,
branch?: string,
filePath?: string
): Promise<PeopleCardEntityData[]>;
getReleasesSummary(
projectID: string | number
): Promise<ReleasesSummary | undefined>;
): Promise<ReleaseSchema[] | undefined>;

getContributorsLink(
projectWebUrl: string | undefined,
Expand Down
116 changes: 57 additions & 59 deletions packages/gitlab/src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import {
PeopleCardEntityData,
MergeRequest,
PipelineObject,
FileOwnership,
ReleaseData,
ProjectDetails,
} from '../components/types';
import { PeopleCardEntityData, FileOwnership } from '../components/types';
import { parseCodeOwners } from '../components/utils';
import { IssueObject } from './../components/types';
import {
ContributorsSummary,
GitlabCIApi,
IssuesSummary,
LanguagesSummary,
MergeRequestsStatusSummary,
MergeRequestsSummary,
PipelineSummary,
ReleasesSummary,
} from './GitlabCIApi';

import type {
GroupSchema,
IssueSchema,
MergeRequestSchema,
PipelineSchema,
ProjectSchema,
ReleaseSchema,
RepositoryContributorSchema,
UserSchema,
} from '@gitbeaker/rest';

export class GitlabCIClient implements GitlabCIApi {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
Expand Down Expand Up @@ -105,16 +103,16 @@ export class GitlabCIClient implements GitlabCIApi {

async getPipelineSummary(
projectID?: string | number
): Promise<PipelineSummary | undefined> {
): Promise<PipelineSchema[] | undefined> {
const [pipelineObjects, projectObj] = await Promise.all([
this.callApi<PipelineObject[]>(
this.callApi<PipelineSchema[]>(
'projects/' + projectID + '/pipelines',
{}
),
this.callApi<Record<string, string>>('projects/' + projectID, {}),
]);
if (pipelineObjects && projectObj) {
pipelineObjects.forEach((element: PipelineObject) => {
pipelineObjects.forEach((element) => {
element.project_name = projectObj.name;
});
}
Expand All @@ -123,13 +121,13 @@ export class GitlabCIClient implements GitlabCIApi {

async getIssuesSummary(
projectId: string | number
): Promise<IssuesSummary | undefined> {
): Promise<IssueSchema[] | undefined> {
const [issuesObject, projectObj] = await Promise.all([
this.callApi<IssueObject[]>(`projects/${projectId}/issues`, {}),
this.callApi<IssueSchema[]>(`projects/${projectId}/issues`, {}),
this.callApi<Record<string, string>>('projects/' + projectId, {}),
]);
if (issuesObject && projectObj) {
issuesObject.forEach((element: IssueObject) => {
issuesObject.forEach((element) => {
element.project_name = projectObj.name;
});
}
Expand All @@ -140,7 +138,7 @@ export class GitlabCIClient implements GitlabCIApi {
async getProjectName(
projectID?: string | number
): Promise<string | undefined> {
const projectObj = await this.callApi<Record<string, string>>(
const projectObj = await this.callApi<ProjectSchema>(
'projects/' + projectID,
{}
);
Expand All @@ -149,36 +147,36 @@ export class GitlabCIClient implements GitlabCIApi {

//TODO: Merge with getUserDetail
private async getUserProfilesData(
contributorsData: PeopleCardEntityData[]
): Promise<PeopleCardEntityData[]> {
for (let i = 0; contributorsData && i < contributorsData.length; i++) {
const userProfile = await this.callApi<Record<string, string>[]>(
'users',
{
search: contributorsData[i].email || '',
contributorsData: RepositoryContributorSchema[]
): Promise<ContributorsSummary> {
return Promise.all(
contributorsData.map(async (contributor) => {
const userProfile = await this.callApi<UserSchema[]>('users', {
search: contributor.email,
without_project_bots: 'true',
}
);
if (userProfile) {
userProfile.forEach((userProfileElement) => {
if (userProfileElement.name == contributorsData[i].name) {
contributorsData[i].avatar_url =
userProfileElement?.avatar_url;
}
});
}
}
return contributorsData;

const user = userProfile?.find(
(v) => v.name === contributor.name
);

if (user) {
return {
...contributor,
...user,
};
}
return contributor;
})
);
}

private async getUserDetail(
username: string
): Promise<PeopleCardEntityData> {
private async getUserDetail(username: string): Promise<UserSchema> {
if (username.startsWith('@')) {
username = username.slice(1);
}
const userDetail = (
await this.callApi<PeopleCardEntityData[]>('users', {
await this.callApi<UserSchema[]>('users', {
username,
})
)?.[0];
Expand All @@ -187,11 +185,11 @@ export class GitlabCIClient implements GitlabCIApi {

return userDetail;
}
private async getGroupDetail(name: string): Promise<PeopleCardEntityData> {
private async getGroupDetail(name: string): Promise<GroupSchema> {
if (name.startsWith('@')) {
name = name.slice(1);
}
const groupDetail = await this.callApi<PeopleCardEntityData>(
const groupDetail = await this.callApi<GroupSchema>(
`groups/${encodeURIComponent(name)}`,
{ with_projects: 'false' }
);
Expand All @@ -203,8 +201,8 @@ export class GitlabCIClient implements GitlabCIApi {

async getMergeRequestsSummary(
projectID?: string | number
): Promise<MergeRequestsSummary | undefined> {
return this.callApi<MergeRequest[]>(
): Promise<MergeRequestSchema[] | undefined> {
return this.callApi<MergeRequestSchema[]>(
'projects/' + projectID + '/merge_requests',
{}
);
Expand All @@ -213,8 +211,8 @@ export class GitlabCIClient implements GitlabCIApi {
async getMergeRequestsStatusSummary(
projectID?: string | number,
count?: number
): Promise<MergeRequestsStatusSummary | undefined> {
return this.callApi<MergeRequest[]>(
): Promise<MergeRequestSchema[] | undefined> {
return this.callApi<MergeRequestSchema[]>(
'projects/' + projectID + '/merge_requests',
{ per_page: (count ?? 20).toString(10) }
);
Expand All @@ -223,10 +221,11 @@ export class GitlabCIClient implements GitlabCIApi {
async getContributorsSummary(
projectID?: string | number
): Promise<ContributorsSummary | undefined> {
const contributorsData = await this.callApi<PeopleCardEntityData[]>(
'projects/' + projectID + '/repository/contributors',
{ sort: 'desc' }
);
const contributorsData = await this.callApi<
RepositoryContributorSchema[]
>('projects/' + projectID + '/repository/contributors', {
sort: 'desc',
});

const updatedContributorsData = await this.getUserProfilesData(
contributorsData!
Expand All @@ -246,19 +245,19 @@ export class GitlabCIClient implements GitlabCIApi {

async getReleasesSummary(
projectID: string | number
): Promise<ReleasesSummary | undefined> {
return this.callApi<ReleaseData[]>(
): Promise<ReleaseSchema[] | undefined> {
return this.callApi<ReleaseSchema[]>(
'projects/' + projectID + '/releases',
{}
);
}

async getProjectDetails(
projectSlug: string
): Promise<ProjectDetails | undefined> {
): Promise<ProjectSchema | undefined> {
if (!projectSlug) return undefined;

return this.callApi<ProjectDetails>(
return this.callApi<ProjectSchema>(
'projects/' + encodeURIComponent(projectSlug),
{}
);
Expand Down Expand Up @@ -292,8 +291,7 @@ export class GitlabCIClient implements GitlabCIApi {
await Promise.allSettled(
uniqueOwners.map(async (owner) => {
try {
const ownerData: PeopleCardEntityData =
await this.getUserDetail(owner);
const ownerData = await this.getUserDetail(owner);
return ownerData;
} catch (error) {
const ownerData: PeopleCardEntityData =
Expand Down
4 changes: 0 additions & 4 deletions packages/gitlab/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
export type {
PipelineSummary,
GitlabCIApi,
MergeRequestsSummary,
MergeRequestsStatusSummary,
LanguagesSummary,
ContributorsSummary,
IssuesSummary,
} from './GitlabCIApi';
export { GitlabCIApiRef } from './GitlabCIApi';
export { GitlabCIClient } from './GitlabCIClient';
Loading

0 comments on commit 8e644e3

Please sign in to comment.