-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
gitlabrepo.js
117 lines (98 loc) · 3.74 KB
/
gitlabrepo.js
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
import env from '../env/Env.js';
import {Gitlab} from "@gitbeaker/rest";
const repoRootDirectory = () => env.get().config.GITLAB_REPO_ROOT_DIRECTORY || env.get().config.REPO_ROOT_DIRECTORY;
export class GitlabClientWrapper {
static getClient(clientOptions) {
return new Gitlab(clientOptions);
}
}
export const getClient = (accessToken) => {
const clientOptions = {
auth: {
oauthToken: accessToken,
},
};
if (env.get().config.GITLAB_HOST) {
clientOptions.auth.host=env.get().config.GITLAB_HOST;
}
return GitlabClientWrapper.getClient(clientOptions.auth);
};
export const reposAsync = (page, accessToken) => searchAsync(page, accessToken, undefined);
export const getPagination = (paginationInfo, page) => {
const pagination = {page, next: false, prev: false};
if (Number.isInteger(paginationInfo.next)) {
pagination.next = true;
}
if (Number.isInteger(paginationInfo.previous)) {
pagination.prev = true;
}
return pagination;
};
export const searchAsync = async (page, accessToken, searchQuery) => {
const repos = await getClient(accessToken).Projects.all({page: page, membership: true, showExpanded: true, search: searchQuery});
repos.data.map((repo) => {
repo.full_name = repo.path_with_namespace;
return repo;
});
return [repos.data, null, getPagination(repos.paginationInfo)];
};
export const userAsync = (accessToken) => getClient(accessToken).Users.showCurrentUser();
export const branchesAsync = async (repoInfo, accessToken) => {
const client = getClient(accessToken);
const branchResponse = await client.Branches.all(getRepoFullName(repoInfo), {
page: repoInfo.page,
showExpanded: true
});
const branchesResp = [];
branchesResp[0] = branchResponse.data;
branchesResp[1] = null;
branchesResp[2] = getPagination(branchResponse.paginationInfo, repoInfo.page);
return branchesResp;
};
export const modelsAsync = async (branchInfo, accessToken) => {
const models = await getClient(accessToken).Repositories.allRepositoryTrees(getRepoFullName(branchInfo), {path: repoRootDirectory(), ref:branchInfo.branch });
return [models];
};
export const modelAsync = async (modelInfo, accessToken) => {
const model = await getClient(accessToken).RepositoryFiles.show(getRepoFullName(modelInfo), getModelPath(modelInfo), modelInfo.branch);
return [model];
};
export const createAsync = (modelInfo, accessToken) => {
const client = getClient(accessToken);
return client.RepositoryFiles.create(getRepoFullName(modelInfo),
getModelPath(modelInfo),
modelInfo.branch,
getModelContent(modelInfo),
'Created by OWASP Threat Dragon',
);
};
export const updateAsync = (modelInfo, accessToken) => {
const repo = getRepoFullName(modelInfo);
const path = getModelPath(modelInfo);
const modelContent = getModelContent(modelInfo);
return getClient(accessToken).RepositoryFiles.edit(repo,
path,
modelInfo.branch,
modelContent,
'Updated by OWASP Threat Dragon',
);
};
export const deleteAsync = (modelInfo, accessToken) => getClient(accessToken).RepositoryFiles.remove(getRepoFullName(modelInfo),
getModelPath(modelInfo),
modelInfo.branch,
'Deleted by OWASP Threat Dragon',
);
const getRepoFullName = (info) => `${info.organisation}/${info.repo}`;
const getModelPath = (modelInfo) => `${repoRootDirectory()}/${modelInfo.model}/${modelInfo.model}.json`;
const getModelContent = (modelInfo) => JSON.stringify(modelInfo.body, null, ' ');
export default {
branchesAsync,
createAsync,
deleteAsync,
modelAsync,
modelsAsync,
reposAsync,
searchAsync,
updateAsync,
userAsync
};