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

Commit

Permalink
Improve access roles management
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Lazarev <[email protected]>
  • Loading branch information
wKich committed Aug 15, 2023
1 parent cbbcca7 commit 3f7c32d
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 122 deletions.
21 changes: 13 additions & 8 deletions plugins/parodos/src/api/fetchAccessRequests.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { AccessRole } from '../models/project';
import { AccessRequest, accessRequests } from '../models/project';
import * as urls from '../urls';

// TODO There has been no API for this yet
export async function fetchAccessRequests(
fetch: FetchApi['fetch'],
baseUrl: string,
projectId: string,
): Promise<{ username: string; requestId: string; role: AccessRole }[]> {
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/access`,
);
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}`);
}

return data;
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;
}
2 changes: 1 addition & 1 deletion plugins/parodos/src/api/removeUserFromProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function removeUserFromProject(
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/users`,
{
method: 'POST',
method: 'DELETE',
body: JSON.stringify(users),
},
);
Expand Down
13 changes: 8 additions & 5 deletions plugins/parodos/src/api/responseOnAccessRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FetchApi } from '@backstage/core-plugin-api';
import { AccessRole } from '../models/project';
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: AccessRole },
payload: { comment?: string; status: AccessStatus },
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/access/${requestId}/status`,
Expand All @@ -15,9 +15,12 @@ export async function responseOnAccessRequest(
body: JSON.stringify({ ...payload, comment: payload.comment || '' }),
},
);
const data = await response.json();
if (response.status !== 204) {
if (response.status === 400) {
const data = await response.json();
throw new Error(`${response.status}: ${data.message}`);
}

if ('error' in data) {
throw new Error(`${data.error}: ${data.path}`);
throw new Error(`${response.status}`);
}
}
2 changes: 1 addition & 1 deletion plugins/parodos/src/api/updateUserRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function updateUserRole(
fetch: FetchApi['fetch'],
baseUrl: string,
projectId: string,
payload: { username: string; role: AccessRole }[],
payload: { username: string; roles: AccessRole[] }[],
) {
const response = await fetch(
`${baseUrl}${urls.Projects}/${projectId}/users`,
Expand Down
Loading

0 comments on commit 3f7c32d

Please sign in to comment.