gitlab-org--gitlab-foss/app/assets/javascripts/api.js

334 lines
9.1 KiB
JavaScript
Raw Normal View History

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