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

204 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import _ from 'underscore';
import axios from './lib/utils/axios_utils';
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',
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',
groupLabelsPath: '/groups/:namespace_path/labels',
2017-09-10 17:25:29 +05:30
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',
commitPath: '/api/:version/projects/:id/repository/commits',
2018-03-17 18:26:18 +05:30
branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
createBranchPath: '/api/:version/projects/:id/repository/branches',
2017-09-10 17:25:29 +05:30
group(groupId, callback) {
const url = Api.buildUrl(Api.groupPath)
.replace(':id', groupId);
2018-03-17 18:26:18 +05:30
return axios.get(url)
.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 groups list. Filtered by query
2017-09-10 17:25:29 +05:30
groups(query, options, callback) {
const url = Api.buildUrl(Api.groupsPath);
2018-03-17 18:26:18 +05:30
return axios.get(url, {
params: Object.assign({
2017-08-17 22:00:37 +05:30
search: query,
2017-09-10 17:25:29 +05:30
per_page: 20,
2017-08-17 22:00:37 +05:30
}, options),
2017-09-10 17:25:29 +05:30
})
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-03-17 18:26:18 +05:30
return axios.get(url, {
params: {
2017-08-17 22:00:37 +05:30
search: query,
2017-09-10 17:25:29 +05:30
per_page: 20,
2017-08-17 22:00:37 +05:30
},
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;
}
return axios.get(url, {
params: Object.assign(defaults, options),
2017-09-10 17:25:29 +05:30
})
2018-03-17 18:26:18 +05:30
.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);
2017-08-17 22:00:37 +05:30
},
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);
}
return axios.post(url, {
label: data,
2017-09-10 17:25:29 +05:30
})
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
2017-09-10 17:25:29 +05:30
groupProjects(groupId, query, callback) {
const url = Api.buildUrl(Api.groupProjectsPath)
.replace(':id', groupId);
2018-03-17 18:26:18 +05:30
return axios.get(url, {
params: {
2017-08-17 22:00:37 +05:30
search: query,
2017-09-10 17:25:29 +05:30
per_page: 20,
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +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
2017-09-10 17:25:29 +05:30
const url = Api.buildUrl(Api.commitPath)
2018-03-17 18:26:18 +05:30
.replace(':id', encodeURIComponent(id));
return axios.post(url, JSON.stringify(data), {
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
});
},
branchSingle(id, branch) {
const url = Api.buildUrl(Api.branchSinglePath)
.replace(':id', encodeURIComponent(id))
.replace(':branch', branch);
return axios.get(url);
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 text for a specific license
2017-09-10 17:25:29 +05:30
licenseText(key, data, callback) {
const url = Api.buildUrl(Api.licensePath)
2017-08-17 22:00:37 +05:30
.replace(':key', key);
2018-03-17 18:26:18 +05:30
return axios.get(url, {
params: data,
2017-09-10 17:25:29 +05:30
})
2018-03-17 18:26:18 +05:30
.then(res => callback(res.data));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
gitignoreText(key, callback) {
const url = Api.buildUrl(Api.gitignorePath)
2017-08-17 22:00:37 +05:30
.replace(':key', key);
2018-03-17 18:26:18 +05:30
return axios.get(url)
.then(({ data }) => callback(data));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
gitlabCiYml(key, callback) {
const url = Api.buildUrl(Api.gitlabCiYmlPath)
2017-08-17 22:00:37 +05:30
.replace(':key', key);
2018-03-17 18:26:18 +05:30
return axios.get(url)
.then(({ data }) => callback(data));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
dockerfileYml(key, callback) {
const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
2018-03-17 18:26:18 +05:30
return axios.get(url)
.then(({ data }) => callback(data));
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-03-17 18:26:18 +05:30
return axios.get(url)
.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, {
params: Object.assign({
2017-09-10 17:25:29 +05:30
search: query,
per_page: 20,
}, options),
});
},
buildUrl(url) {
let urlRoot = '';
2017-08-17 22:00:37 +05:30
if (gon.relative_url_root != null) {
2017-09-10 17:25:29 +05:30
urlRoot = gon.relative_url_root;
2016-09-13 17:45:13 +05:30
}
2017-09-10 17:25:29 +05:30
return urlRoot + url.replace(':version', gon.api_version);
},
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;