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

204 lines
5.5 KiB
JavaScript
Raw Normal View History

import _ from 'underscore';
import axios from './lib/utils/axios_utils';
const Api = {
groupsPath: '/api/:version/groups.json',
groupPath: '/api/:version/groups/:id',
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',
groupLabelsPath: '/groups/:namespace_path/-/labels',
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 09:18:45 -04:00
commitPath: '/api/:version/projects/:id/repository/commits',
2017-10-12 17:04:17 -04:00
branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
createBranchPath: '/api/:version/projects/:id/repository/branches',
group(groupId, callback) {
const url = Api.buildUrl(Api.groupPath)
.replace(':id', groupId);
2018-01-25 11:40:31 -05:00
return axios.get(url)
2018-01-29 05:12:32 -05:00
.then(({ data }) => {
callback(data);
return data;
});
},
// Return groups list. Filtered by query
groups(query, options, callback = $.noop) {
const url = Api.buildUrl(Api.groupsPath);
2018-01-25 11:40:31 -05: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-01-25 11:40:31 -05: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-01-25 11:40:31 -05: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) {
const url = Api.buildUrl(Api.projectPath)
.replace(':id', encodeURIComponent(projectPath));
return axios.get(url);
},
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 {
url = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespacePath);
}
2018-01-25 11:40:31 -05: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, callback) {
const url = Api.buildUrl(Api.groupProjectsPath)
.replace(':id', groupId);
2018-01-25 11:40:31 -05:00
return axios.get(url, {
params: {
search: query,
per_page: 20,
},
})
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
2017-07-25 09:18:45 -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
},
branchSingle(id, branch) {
const url = Api.buildUrl(Api.branchSinglePath)
.replace(':id', encodeURIComponent(id))
2017-10-12 17:04:17 -04:00
.replace(':branch', branch);
2018-01-25 11:40:31 -05:00
return axios.get(url);
2017-07-25 09:18:45 -04:00
},
// Return text for a specific license
licenseText(key, data, callback) {
const url = Api.buildUrl(Api.licensePath)
.replace(':key', key);
2018-01-25 11:40:31 -05:00
return axios.get(url, {
params: data,
})
2018-01-25 11:40:31 -05:00
.then(res => callback(res.data));
},
gitignoreText(key, callback) {
const url = Api.buildUrl(Api.gitignorePath)
.replace(':key', key);
2018-01-25 11:40:31 -05:00
return axios.get(url)
.then(({ data }) => callback(data));
},
gitlabCiYml(key, callback) {
const url = Api.buildUrl(Api.gitlabCiYmlPath)
.replace(':key', key);
2018-01-25 11:40:31 -05:00
return axios.get(url)
.then(({ data }) => callback(data));
},
dockerfileYml(key, callback) {
const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
2018-01-25 11:40:31 -05:00
return axios.get(url)
.then(({ data }) => callback(data));
},
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-01-25 11:40:31 -05:00
return axios.get(url)
.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, {
params: Object.assign({
search: query,
per_page: 20,
}, options),
});
},
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;