2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2018-01-30 09:10:58 +00:00
|
|
|
import _ from 'underscore';
|
2017-12-21 15:05:47 +00:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
const Api = {
|
|
|
|
groupsPath: '/api/:version/groups.json',
|
2018-01-30 09:10:58 +00:00
|
|
|
groupPath: '/api/:version/groups/:id',
|
2017-05-19 21:22:46 +00:00
|
|
|
namespacesPath: '/api/:version/namespaces.json',
|
|
|
|
groupProjectsPath: '/api/:version/groups/:id/projects.json',
|
2017-09-04 15:41:55 +00:00
|
|
|
projectsPath: '/api/:version/projects.json',
|
2017-12-21 15:05:47 +00:00
|
|
|
projectPath: '/api/:version/projects/:id',
|
2017-09-06 14:17:06 +00:00
|
|
|
projectLabelsPath: '/:namespace_path/:project_path/labels',
|
2018-03-23 10:45:43 +00:00
|
|
|
mergeRequestPath: '/api/:version/projects/:id/merge_requests/:mrid',
|
2018-05-11 09:37:10 +00:00
|
|
|
mergeRequestsPath: '/api/:version/merge_requests',
|
2018-03-25 18:16:20 +00:00
|
|
|
mergeRequestChangesPath: '/api/:version/projects/:id/merge_requests/:mrid/changes',
|
|
|
|
mergeRequestVersionsPath: '/api/:version/projects/:id/merge_requests/:mrid/versions',
|
2018-02-21 14:28:47 +00:00
|
|
|
groupLabelsPath: '/groups/:namespace_path/-/labels',
|
2017-05-19 21:22:46 +00:00
|
|
|
licensePath: '/api/:version/templates/licenses/:key',
|
|
|
|
gitignorePath: '/api/:version/templates/gitignores/:key',
|
|
|
|
gitlabCiYmlPath: '/api/:version/templates/gitlab_ci_ymls/:key',
|
|
|
|
dockerfilePath: '/api/:version/templates/dockerfiles/:key',
|
|
|
|
issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key',
|
|
|
|
usersPath: '/api/:version/users.json',
|
2017-07-25 13:18:45 +00:00
|
|
|
commitPath: '/api/:version/projects/:id/repository/commits',
|
2018-05-25 10:08:53 +00:00
|
|
|
commitPipelinesPath: '/:project_id/commit/:sha/pipelines',
|
2017-10-12 21:04:17 +00:00
|
|
|
branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
|
2017-10-26 10:14:31 +00:00
|
|
|
createBranchPath: '/api/:version/projects/:id/repository/branches',
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
group(groupId, callback) {
|
2018-03-23 10:45:43 +00:00
|
|
|
const url = Api.buildUrl(Api.groupPath).replace(':id', groupId);
|
|
|
|
return axios.get(url).then(({ data }) => {
|
|
|
|
callback(data);
|
2018-01-29 10:12:32 +00:00
|
|
|
|
2018-03-23 10:45:43 +00:00
|
|
|
return data;
|
|
|
|
});
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return groups list. Filtered by query
|
2018-02-21 15:17:03 +00:00
|
|
|
groups(query, options, callback = $.noop) {
|
2017-05-19 21:22:46 +00:00
|
|
|
const url = Api.buildUrl(Api.groupsPath);
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
params: Object.assign(
|
|
|
|
{
|
|
|
|
search: query,
|
|
|
|
per_page: 20,
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
),
|
|
|
|
})
|
2018-01-29 11:42:27 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
callback(data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return namespaces list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
namespaces(query, callback) {
|
|
|
|
const url = Api.buildUrl(Api.namespacesPath);
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
params: {
|
|
|
|
search: query,
|
|
|
|
per_page: 20,
|
|
|
|
},
|
|
|
|
})
|
2018-01-25 16:40:31 +00:00
|
|
|
.then(({ data }) => callback(data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return projects list. Filtered by query
|
2018-01-30 09:10:58 +00:00
|
|
|
projects(query, options, callback = _.noop) {
|
2017-05-19 21:22:46 +00:00
|
|
|
const url = Api.buildUrl(Api.projectsPath);
|
2017-08-22 15:10:49 +00:00
|
|
|
const defaults = {
|
|
|
|
search: query,
|
|
|
|
per_page: 20,
|
2017-09-04 15:41:55 +00:00
|
|
|
simple: true,
|
2017-08-22 15:10:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (gon.current_user_id) {
|
|
|
|
defaults.membership = true;
|
|
|
|
}
|
|
|
|
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
params: Object.assign(defaults, options),
|
|
|
|
})
|
2018-01-29 11:42:27 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
callback(data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-12-21 15:05:47 +00:00
|
|
|
// Return single project
|
|
|
|
project(projectPath) {
|
2018-03-25 18:16:20 +00:00
|
|
|
const url = Api.buildUrl(Api.projectPath).replace(':id', encodeURIComponent(projectPath));
|
2018-03-23 10:45:43 +00:00
|
|
|
|
|
|
|
return axios.get(url);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return Merge Request for project
|
|
|
|
mergeRequest(projectPath, mergeRequestId) {
|
|
|
|
const url = Api.buildUrl(Api.mergeRequestPath)
|
|
|
|
.replace(':id', encodeURIComponent(projectPath))
|
|
|
|
.replace(':mrid', mergeRequestId);
|
|
|
|
|
|
|
|
return axios.get(url);
|
|
|
|
},
|
|
|
|
|
2018-05-11 09:37:10 +00:00
|
|
|
mergeRequests(params = {}) {
|
|
|
|
const url = Api.buildUrl(Api.mergeRequestsPath);
|
|
|
|
|
|
|
|
return axios.get(url, { params });
|
|
|
|
},
|
|
|
|
|
2018-03-23 10:45:43 +00:00
|
|
|
mergeRequestChanges(projectPath, mergeRequestId) {
|
|
|
|
const url = Api.buildUrl(Api.mergeRequestChangesPath)
|
|
|
|
.replace(':id', encodeURIComponent(projectPath))
|
|
|
|
.replace(':mrid', mergeRequestId);
|
2017-12-21 15:05:47 +00:00
|
|
|
|
|
|
|
return axios.get(url);
|
|
|
|
},
|
|
|
|
|
2018-03-25 12:02:09 +00:00
|
|
|
mergeRequestVersions(projectPath, mergeRequestId) {
|
|
|
|
const url = Api.buildUrl(Api.mergeRequestVersionsPath)
|
|
|
|
.replace(':id', encodeURIComponent(projectPath))
|
|
|
|
.replace(':mrid', mergeRequestId);
|
|
|
|
|
|
|
|
return axios.get(url);
|
|
|
|
},
|
|
|
|
|
2017-05-19 21:22:46 +00:00
|
|
|
newLabel(namespacePath, projectPath, data, callback) {
|
2017-09-06 14:17:06 +00:00
|
|
|
let url;
|
|
|
|
|
|
|
|
if (projectPath) {
|
|
|
|
url = Api.buildUrl(Api.projectLabelsPath)
|
|
|
|
.replace(':namespace_path', namespacePath)
|
|
|
|
.replace(':project_path', projectPath);
|
|
|
|
} else {
|
2018-03-25 18:16:20 +00:00
|
|
|
url = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespacePath);
|
2017-09-06 14:17:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios
|
|
|
|
.post(url, {
|
|
|
|
label: data,
|
|
|
|
})
|
2018-01-25 16:40:31 +00:00
|
|
|
.then(res => callback(res.data))
|
|
|
|
.catch(e => callback(e.response.data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return group projects list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
groupProjects(groupId, query, callback) {
|
2018-03-23 10:45:43 +00:00
|
|
|
const url = Api.buildUrl(Api.groupProjectsPath).replace(':id', groupId);
|
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
params: {
|
|
|
|
search: query,
|
|
|
|
per_page: 20,
|
|
|
|
},
|
|
|
|
})
|
2018-01-25 16:40:31 +00:00
|
|
|
.then(({ data }) => callback(data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-08-17 18:05:09 +00:00
|
|
|
commitMultiple(id, data) {
|
|
|
|
// see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
|
2018-03-25 18:16:20 +00:00
|
|
|
const url = Api.buildUrl(Api.commitPath).replace(':id', encodeURIComponent(id));
|
2018-01-25 16:40:31 +00:00
|
|
|
return axios.post(url, JSON.stringify(data), {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
|
|
},
|
2017-08-09 13:24:48 +00:00
|
|
|
});
|
2017-10-12 21:04:17 +00:00
|
|
|
},
|
|
|
|
|
2018-05-25 10:08:53 +00:00
|
|
|
commitPipelines(projectId, sha) {
|
|
|
|
const encodedProjectId = projectId
|
|
|
|
.split('/')
|
|
|
|
.map(fragment => encodeURIComponent(fragment))
|
|
|
|
.join('/');
|
|
|
|
|
|
|
|
const url = Api.buildUrl(Api.commitPipelinesPath)
|
|
|
|
.replace(':project_id', encodedProjectId)
|
|
|
|
.replace(':sha', encodeURIComponent(sha));
|
|
|
|
|
|
|
|
return axios.get(url);
|
|
|
|
},
|
|
|
|
|
2017-10-12 21:04:17 +00:00
|
|
|
branchSingle(id, branch) {
|
|
|
|
const url = Api.buildUrl(Api.branchSinglePath)
|
2017-12-21 15:05:47 +00:00
|
|
|
.replace(':id', encodeURIComponent(id))
|
2018-03-23 10:45:43 +00:00
|
|
|
.replace(':branch', encodeURIComponent(branch));
|
2017-10-12 21:04:17 +00:00
|
|
|
|
2018-01-25 16:40:31 +00:00
|
|
|
return axios.get(url);
|
2017-07-25 13:18:45 +00:00
|
|
|
},
|
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return text for a specific license
|
2017-05-19 21:22:46 +00:00
|
|
|
licenseText(key, data, callback) {
|
2018-03-23 10:45:43 +00:00
|
|
|
const url = Api.buildUrl(Api.licensePath).replace(':key', key);
|
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
params: data,
|
|
|
|
})
|
2018-01-25 16:40:31 +00:00
|
|
|
.then(res => callback(res.data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
gitignoreText(key, callback) {
|
2018-03-23 10:45:43 +00:00
|
|
|
const url = Api.buildUrl(Api.gitignorePath).replace(':key', key);
|
|
|
|
return axios.get(url).then(({ data }) => callback(data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
gitlabCiYml(key, callback) {
|
2018-03-23 10:45:43 +00:00
|
|
|
const url = Api.buildUrl(Api.gitlabCiYmlPath).replace(':key', key);
|
|
|
|
return axios.get(url).then(({ data }) => callback(data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
dockerfileYml(key, callback) {
|
|
|
|
const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios.get(url).then(({ data }) => callback(data));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
issueTemplate(namespacePath, projectPath, key, type, callback) {
|
|
|
|
const url = Api.buildUrl(Api.issuableTemplatePath)
|
2017-11-10 15:02:42 +00:00
|
|
|
.replace(':key', encodeURIComponent(key))
|
2017-03-11 07:30:44 +00:00
|
|
|
.replace(':type', type)
|
|
|
|
.replace(':project_path', projectPath)
|
|
|
|
.replace(':namespace_path', namespacePath);
|
2018-03-23 10:45:43 +00:00
|
|
|
return axios
|
|
|
|
.get(url)
|
2018-01-25 16:40:31 +00:00
|
|
|
.then(({ data }) => callback(null, data))
|
|
|
|
.catch(callback);
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
users(query, options) {
|
|
|
|
const url = Api.buildUrl(this.usersPath);
|
2018-01-25 16:40:31 +00:00
|
|
|
return axios.get(url, {
|
2018-03-23 10:45:43 +00:00
|
|
|
params: Object.assign(
|
|
|
|
{
|
|
|
|
search: query,
|
|
|
|
per_page: 20,
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
),
|
2017-05-19 21:22:46 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
buildUrl(url) {
|
|
|
|
let urlRoot = '';
|
2017-03-11 07:30:44 +00:00
|
|
|
if (gon.relative_url_root != null) {
|
2017-05-19 21:22:46 +00:00
|
|
|
urlRoot = gon.relative_url_root;
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
2017-05-19 21:22:46 +00:00
|
|
|
return urlRoot + url.replace(':version', gon.api_version);
|
|
|
|
},
|
2017-03-11 07:30:44 +00:00
|
|
|
};
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-05-19 21:22:46 +00:00
|
|
|
export default Api;
|