debian-mirror-gitlab/app/assets/javascripts/api.js

348 lines
9.6 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import _ from 'underscore';
import axios from './lib/utils/axios_utils';
2019-07-31 22:56:46 +05:30
import { joinPaths } from './lib/utils/url_utility';
2017-09-10 17:25:29 +05:30
const Api = {
groupsPath: '/api/:version/groups.json',
2018-03-17 18:26:18 +05:30
groupPath: '/api/:version/groups/:id',
2019-03-02 22:35:43 +05:30
groupMembersPath: '/api/:version/groups/:id/members',
2019-02-15 15:39:39 +05:30
subgroupsPath: '/api/:version/groups/:id/subgroups',
2017-09-10 17:25:29 +05:30
namespacesPath: '/api/:version/namespaces.json',
groupProjectsPath: '/api/:version/groups/:id/projects.json',
2018-03-17 18:26:18 +05:30
projectsPath: '/api/:version/projects.json',
projectPath: '/api/:version/projects/:id',
projectLabelsPath: '/:namespace_path/:project_path/labels',
2019-07-07 11:18:12 +05:30
projectMergeRequestsPath: '/api/:version/projects/:id/merge_requests',
2019-02-15 15:39:39 +05:30
projectMergeRequestPath: '/api/:version/projects/:id/merge_requests/:mrid',
projectMergeRequestChangesPath: '/api/:version/projects/:id/merge_requests/:mrid/changes',
projectMergeRequestVersionsPath: '/api/:version/projects/:id/merge_requests/:mrid/versions',
projectRunnersPath: '/api/:version/projects/:id/runners',
2018-11-08 19:23:39 +05:30
mergeRequestsPath: '/api/:version/merge_requests',
2018-03-27 19:54:05 +05:30
groupLabelsPath: '/groups/:namespace_path/-/labels',
2017-09-10 17:25:29 +05:30
issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key',
2018-12-05 23:21:45 +05:30
projectTemplatePath: '/api/:version/projects/:id/templates/:type/:key',
projectTemplatesPath: '/api/:version/projects/:id/templates/:type',
2017-09-10 17:25:29 +05:30
usersPath: '/api/:version/users.json',
2019-02-15 15:39:39 +05:30
userPath: '/api/:version/users/:id',
userStatusPath: '/api/:version/users/:id/status',
userPostStatusPath: '/api/:version/user/status',
2017-09-10 17:25:29 +05:30
commitPath: '/api/:version/projects/:id/repository/commits',
2019-02-15 15:39:39 +05:30
applySuggestionPath: '/api/:version/suggestions/:id/apply',
2018-11-08 19:23:39 +05:30
commitPipelinesPath: '/:project_id/commit/:sha/pipelines',
2018-03-17 18:26:18 +05:30
branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
createBranchPath: '/api/:version/projects/:id/repository/branches',
2019-02-15 15:39:39 +05:30
releasesPath: '/api/:version/projects/:id/releases',
2017-09-10 17:25:29 +05:30
group(groupId, callback) {
2018-05-09 12:01:36 +05:30
const url = Api.buildUrl(Api.groupPath).replace(':id', groupId);
return axios.get(url).then(({ data }) => {
callback(data);
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
return data;
});
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
groupMembers(id) {
const url = Api.buildUrl(this.groupMembersPath).replace(':id', encodeURIComponent(id));
return axios.get(url);
},
2017-08-17 22:00:37 +05:30
// Return groups list. Filtered by query
2018-03-27 19:54:05 +05:30
groups(query, options, callback = $.noop) {
2017-09-10 17:25:29 +05:30
const url = Api.buildUrl(Api.groupsPath);
2018-05-09 12:01:36 +05:30
return axios
.get(url, {
params: Object.assign(
{
search: query,
per_page: 20,
},
options,
),
})
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
callback(data);
return data;
});
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
// Return namespaces list. Filtered by query
2017-09-10 17:25:29 +05:30
namespaces(query, callback) {
const url = Api.buildUrl(Api.namespacesPath);
2018-05-09 12:01:36 +05:30
return axios
.get(url, {
params: {
search: query,
per_page: 20,
},
})
2018-03-17 18:26:18 +05:30
.then(({ data }) => callback(data));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
// Return projects list. Filtered by query
2018-03-17 18:26:18 +05:30
projects(query, options, callback = _.noop) {
2017-09-10 17:25:29 +05:30
const url = Api.buildUrl(Api.projectsPath);
2018-03-17 18:26:18 +05:30
const defaults = {
search: query,
per_page: 20,
simple: true,
};
if (gon.current_user_id) {
defaults.membership = true;
}
2018-05-09 12:01:36 +05:30
return axios
.get(url, {
params: Object.assign(defaults, options),
})
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
callback(data);
return data;
});
},
// Return single project
project(projectPath) {
2018-05-09 12:01:36 +05:30
const url = Api.buildUrl(Api.projectPath).replace(':id', encodeURIComponent(projectPath));
return axios.get(url);
},
2019-07-07 11:18:12 +05:30
/**
* Get all Merge Requests for a project, eventually filtering based on
* supplied parameters
* @param projectPath
* @param params
* @returns {Promise}
*/
projectMergeRequests(projectPath, params = {}) {
const url = Api.buildUrl(Api.projectMergeRequestsPath).replace(
':id',
encodeURIComponent(projectPath),
);
return axios.get(url, { params });
},
2018-05-09 12:01:36 +05:30
// Return Merge Request for project
2019-02-15 15:39:39 +05:30
projectMergeRequest(projectPath, mergeRequestId, params = {}) {
const url = Api.buildUrl(Api.projectMergeRequestPath)
2018-05-09 12:01:36 +05:30
.replace(':id', encodeURIComponent(projectPath))
.replace(':mrid', mergeRequestId);
2018-11-08 19:23:39 +05:30
return axios.get(url, { params });
},
2019-02-15 15:39:39 +05:30
projectMergeRequestChanges(projectPath, mergeRequestId) {
const url = Api.buildUrl(Api.projectMergeRequestChangesPath)
2018-05-09 12:01:36 +05:30
.replace(':id', encodeURIComponent(projectPath))
.replace(':mrid', mergeRequestId);
2018-03-17 18:26:18 +05:30
return axios.get(url);
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
projectMergeRequestVersions(projectPath, mergeRequestId) {
const url = Api.buildUrl(Api.projectMergeRequestVersionsPath)
2019-01-03 12:48:30 +05:30
.replace(':id', encodeURIComponent(projectPath))
.replace(':mrid', mergeRequestId);
2018-12-23 12:14:25 +05:30
2019-01-03 12:48:30 +05:30
return axios.get(url);
2018-12-23 12:14:25 +05:30
},
2019-02-15 15:39:39 +05:30
projectRunners(projectPath, config = {}) {
const url = Api.buildUrl(Api.projectRunnersPath).replace(
':id',
encodeURIComponent(projectPath),
);
return axios.get(url, config);
},
mergeRequests(params = {}) {
const url = Api.buildUrl(Api.mergeRequestsPath);
return axios.get(url, { params });
},
2017-09-10 17:25:29 +05:30
newLabel(namespacePath, projectPath, data, callback) {
2018-03-17 18:26:18 +05:30
let url;
if (projectPath) {
url = Api.buildUrl(Api.projectLabelsPath)
.replace(':namespace_path', namespacePath)
.replace(':project_path', projectPath);
} else {
url = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespacePath);
}
2018-05-09 12:01:36 +05:30
return axios
.post(url, {
label: data,
})
2018-03-17 18:26:18 +05:30
.then(res => callback(res.data))
.catch(e => callback(e.response.data));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
// Return group projects list. Filtered by query
2018-11-08 19:23:39 +05:30
groupProjects(groupId, query, options, callback) {
2018-05-09 12:01:36 +05:30
const url = Api.buildUrl(Api.groupProjectsPath).replace(':id', groupId);
2018-11-08 19:23:39 +05:30
const defaults = {
search: query,
per_page: 20,
};
2018-05-09 12:01:36 +05:30
return axios
.get(url, {
2018-11-08 19:23:39 +05:30
params: Object.assign({}, defaults, options),
2018-05-09 12:01:36 +05:30
})
2018-03-17 18:26:18 +05:30
.then(({ data }) => callback(data));
2017-09-10 17:25:29 +05:30
},
2018-03-17 18:26:18 +05:30
commitMultiple(id, data) {
// see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
2018-05-09 12:01:36 +05:30
const url = Api.buildUrl(Api.commitPath).replace(':id', encodeURIComponent(id));
2018-03-17 18:26:18 +05:30
return axios.post(url, JSON.stringify(data), {
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
});
},
2019-02-15 15:39:39 +05:30
applySuggestion(id) {
const url = Api.buildUrl(Api.applySuggestionPath).replace(':id', encodeURIComponent(id));
return axios.put(url);
},
2018-11-08 19:23:39 +05:30
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);
},
2018-03-17 18:26:18 +05:30
branchSingle(id, branch) {
const url = Api.buildUrl(Api.branchSinglePath)
.replace(':id', encodeURIComponent(id))
2018-05-09 12:01:36 +05:30
.replace(':branch', encodeURIComponent(branch));
2018-03-17 18:26:18 +05:30
return axios.get(url);
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
projectTemplate(id, type, key, options, callback) {
const url = Api.buildUrl(this.projectTemplatePath)
.replace(':id', encodeURIComponent(id))
.replace(':type', type)
.replace(':key', encodeURIComponent(key));
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
return axios.get(url, { params: options }).then(res => {
if (callback) callback(res.data);
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
return res;
});
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
projectTemplates(id, type, params = {}, callback) {
const url = Api.buildUrl(this.projectTemplatesPath)
.replace(':id', encodeURIComponent(id))
.replace(':type', type);
return axios.get(url, { params }).then(res => {
if (callback) callback(res.data);
return res;
});
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
issueTemplate(namespacePath, projectPath, key, type, callback) {
const url = Api.buildUrl(Api.issuableTemplatePath)
2018-03-17 18:26:18 +05:30
.replace(':key', encodeURIComponent(key))
2017-08-17 22:00:37 +05:30
.replace(':type', type)
.replace(':project_path', projectPath)
.replace(':namespace_path', namespacePath);
2018-05-09 12:01:36 +05:30
return axios
.get(url)
2018-03-17 18:26:18 +05:30
.then(({ data }) => callback(null, data))
.catch(callback);
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
users(query, options) {
const url = Api.buildUrl(this.usersPath);
2018-03-17 18:26:18 +05:30
return axios.get(url, {
2018-05-09 12:01:36 +05:30
params: Object.assign(
{
search: query,
per_page: 20,
},
options,
),
2017-09-10 17:25:29 +05:30
});
},
2019-02-15 15:39:39 +05:30
user(id, options) {
const url = Api.buildUrl(this.userPath).replace(':id', encodeURIComponent(id));
return axios.get(url, {
params: options,
});
},
userStatus(id, options) {
const url = Api.buildUrl(this.userStatusPath).replace(':id', encodeURIComponent(id));
return axios.get(url, {
params: options,
});
},
2018-11-18 11:00:15 +05:30
branches(id, query = '', options = {}) {
const url = Api.buildUrl(this.createBranchPath).replace(':id', encodeURIComponent(id));
return axios.get(url, {
params: {
search: query,
per_page: 20,
...options,
},
});
},
2018-11-08 19:23:39 +05:30
createBranch(id, { ref, branch }) {
const url = Api.buildUrl(this.createBranchPath).replace(':id', encodeURIComponent(id));
return axios.post(url, {
ref,
branch,
});
},
2018-12-05 23:21:45 +05:30
postUserStatus({ emoji, message }) {
2019-02-15 15:39:39 +05:30
const url = Api.buildUrl(this.userPostStatusPath);
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
return axios.put(url, {
emoji,
message,
});
2018-11-20 20:47:30 +05:30
},
2019-02-15 15:39:39 +05:30
releases(id) {
const url = Api.buildUrl(this.releasesPath).replace(':id', encodeURIComponent(id));
return axios.get(url);
},
2017-09-10 17:25:29 +05:30
buildUrl(url) {
2019-07-31 22:56:46 +05:30
return joinPaths(gon.relative_url_root || '', url.replace(':version', gon.api_version));
2017-09-10 17:25:29 +05:30
},
2017-08-17 22:00:37 +05:30
};
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
export default Api;