Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #205 from parodos-dev/dl/members-api
Browse files Browse the repository at this point in the history
Integrate members API
  • Loading branch information
RichardW98 committed Aug 15, 2023
2 parents f69b51c + 3f7c32d commit 5a81d80
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 157 deletions.
26 changes: 26 additions & 0 deletions plugins/parodos/src/api/fetchAccessRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { AccessRequest, accessRequests } from '../models/project';
import * as urls from '../urls';

export async function fetchAccessRequests(
fetch: FetchApi['fetch'],
baseUrl: string,
filter?: { projectId?: string; username?: string },
): Promise<AccessRequest[]> {
const response = await fetch(`${baseUrl}${urls.Projects}/access/pending`);
const data = await response.json();

if ('error' in data) {
throw new Error(`${data.error}: ${data.path}`);
}

const requests = accessRequests.parse(data);

return filter
? requests.filter(
request =>
(filter.projectId ? request.projectId === filter.projectId : true) &&
(filter.username ? request.username === filter.username : true),
)
: requests;
}
20 changes: 20 additions & 0 deletions plugins/parodos/src/api/fetchProjectMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { projectMembers } from '../models/project';
import * as urls from '../urls';

export async function fetchProjectMembers(
fetch: FetchApi['fetch'],
baseUrl: string,
projectId: string,
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/members`,
);
const data = await response.json();

if ('error' in data) {
throw new Error(`${data.error}: ${data.path}`);
}

return projectMembers.parse(data);
}
22 changes: 22 additions & 0 deletions plugins/parodos/src/api/removeUserFromProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { FetchApi } from '@backstage/core-plugin-api';
import * as urls from '../urls';

export async function removeUserFromProject(
fetch: FetchApi['fetch'],
baseUrl: string,
projectId: string,
users: string[],
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/users`,
{
method: 'DELETE',
body: JSON.stringify(users),
},
);
const data = await response.json();

if ('error' in data) {
throw new Error(`${data.error}: ${data.path}`);
}
}
26 changes: 26 additions & 0 deletions plugins/parodos/src/api/responseOnAccessRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { AccessStatus } from '../models/project';
import * as urls from '../urls';

export async function responseOnAccessRequest(
fetch: FetchApi['fetch'],
baseUrl: string,
requestId: string,
payload: { comment?: string; status: AccessStatus },
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/access/${requestId}/status`,
{
method: 'POST',
body: JSON.stringify({ ...payload, comment: payload.comment || '' }),
},
);
if (response.status !== 204) {
if (response.status === 400) {
const data = await response.json();
throw new Error(`${response.status}: ${data.message}`);
}

throw new Error(`${response.status}`);
}
}
23 changes: 23 additions & 0 deletions plugins/parodos/src/api/updateUserRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { AccessRole } from '../models/project';
import * as urls from '../urls';

export async function updateUserRole(
fetch: FetchApi['fetch'],
baseUrl: string,
projectId: string,
payload: { username: string; roles: AccessRole[] }[],
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/users`,
{
method: 'POST',
body: JSON.stringify(payload),
},
);
const data = await response.json();

if ('error' in data) {
throw new Error(`${data.error}: ${data.path}`);
}
}
Loading

0 comments on commit 5a81d80

Please sign in to comment.