forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo.ts
More file actions
119 lines (105 loc) · 3.95 KB
/
repo.ts
File metadata and controls
119 lines (105 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import axios from 'axios';
import { getAxiosConfig } from './auth.js';
import { Repo } from '../../db/types';
import { RepoView } from '../types';
import { getApiV1BaseUrl } from './apiConfig';
import { ServiceResult, getServiceError, errorResult, successResult } from './errors';
const canAddUser = async (repoId: string, user: string, action: string) => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}`);
try {
const response = await axios.get<Repo>(url.toString(), getAxiosConfig());
const repo = response.data;
if (action === 'authorise') {
return !repo.users.canAuthorise.includes(user);
} else {
return !repo.users.canPush.includes(user);
}
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to validate repo permissions');
throw new Error(message);
}
};
class DupUserValidationError extends Error {
constructor(message: string) {
super(message);
this.name = 'The user already has this role...';
}
}
const getRepos = async (
query: Record<string, boolean> = {},
): Promise<ServiceResult<RepoView[]>> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo`);
url.search = new URLSearchParams(query as any).toString();
try {
const response = await axios<RepoView[]>(url.toString(), getAxiosConfig());
const sortedRepos = response.data.sort((a: RepoView, b: RepoView) =>
a.name.localeCompare(b.name),
);
return successResult(sortedRepos);
} catch (error: any) {
return errorResult(error, 'Failed to load repositories');
}
};
const getRepo = async (id: string): Promise<ServiceResult<RepoView>> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${id}`);
try {
const response = await axios<RepoView>(url.toString(), getAxiosConfig());
return successResult(response.data);
} catch (error: any) {
return errorResult(error, 'Failed to load repository');
}
};
const addRepo = async (repo: RepoView): Promise<ServiceResult<RepoView>> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo`);
try {
const response = await axios.post<RepoView>(url.toString(), repo, getAxiosConfig());
return successResult(response.data);
} catch (error: any) {
return errorResult(error, 'Failed to add repository');
}
};
const addUser = async (repoId: string, user: string, action: string): Promise<void> => {
const canAdd = await canAddUser(repoId, user, action);
if (canAdd) {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}`);
const data = { username: user };
try {
await axios.patch(url.toString(), data, getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to add user');
console.log(message);
throw new Error(message);
}
} else {
console.log('Duplicate user can not be added');
throw new DupUserValidationError('Duplicate user can not be added');
}
};
const deleteUser = async (user: string, repoId: string, action: string): Promise<void> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}/${user}`);
try {
await axios.delete(url.toString(), getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to remove user');
console.log(message);
throw new Error(message);
}
};
const deleteRepo = async (repoId: string): Promise<void> => {
const apiV1Base = await getApiV1BaseUrl();
const url = new URL(`${apiV1Base}/repo/${repoId}/delete`);
try {
await axios.delete(url.toString(), getAxiosConfig());
} catch (error: any) {
const { message } = getServiceError(error, 'Failed to delete repository');
console.log(message);
throw new Error(message);
}
};
export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo };