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

191 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import $ from 'jquery';
const Api = {
groupsPath: '/api/:version/groups.json',
groupPath: '/api/:version/groups/:id.json',
namespacesPath: '/api/:version/namespaces.json',
groupProjectsPath: '/api/:version/groups/:id/projects.json',
projectsPath: '/api/:version/projects.json?simple=true',
labelsPath: '/:namespace_path/:project_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',
commitPath: '/api/:version/projects/:id/repository/commits',
group(groupId, callback) {
const url = Api.buildUrl(Api.groupPath)
.replace(':id', groupId);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
dataType: 'json',
})
.done(group => callback(group));
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);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
data: 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
dataType: 'json',
})
.done(groups => callback(groups));
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);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
2017-08-17 22:00:37 +05:30
data: {
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
dataType: 'json',
}).done(namespaces => callback(namespaces));
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
2017-09-10 17:25:29 +05:30
projects(query, options, callback) {
const url = Api.buildUrl(Api.projectsPath);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
data: Object.assign({
2017-08-17 22:00:37 +05:30
search: query,
per_page: 20,
2017-09-10 17:25:29 +05:30
membership: true,
2017-08-17 22:00:37 +05:30
}, options),
2017-09-10 17:25:29 +05:30
dataType: 'json',
})
.done(projects => callback(projects));
2017-08-17 22:00:37 +05:30
},
2017-09-10 17:25:29 +05:30
newLabel(namespacePath, projectPath, data, callback) {
const url = Api.buildUrl(Api.labelsPath)
.replace(':namespace_path', namespacePath)
.replace(':project_path', projectPath);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
type: 'POST',
data: { label: data },
dataType: 'json',
})
.done(label => callback(label))
.fail(message => callback(message.responseJSON));
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);
2017-08-17 22:00:37 +05:30
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
2017-08-17 22:00:37 +05:30
data: {
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
dataType: 'json',
})
.done(projects => callback(projects));
},
commitMultiple(id, data, callback) {
const url = Api.buildUrl(Api.commitPath)
.replace(':id', id);
return $.ajax({
url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
dataType: 'json',
})
.done(commitData => callback(commitData))
.fail(message => callback(message.responseJSON));
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);
return $.ajax({
2017-09-10 17:25:29 +05:30
url,
data,
})
.done(license => callback(license));
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);
2017-09-10 17:25:29 +05:30
return $.get(url, gitignore => callback(gitignore));
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);
2017-09-10 17:25:29 +05:30
return $.get(url, file => callback(file));
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);
2017-08-17 22:00:37 +05:30
$.get(url, callback);
},
2017-09-10 17:25:29 +05:30
issueTemplate(namespacePath, projectPath, key, type, callback) {
const url = Api.buildUrl(Api.issuableTemplatePath)
2017-08-17 22:00:37 +05:30
.replace(':key', key)
.replace(':type', type)
.replace(':project_path', projectPath)
.replace(':namespace_path', namespacePath);
$.ajax({
2017-09-10 17:25:29 +05:30
url,
dataType: 'json',
})
.done(file => callback(null, file))
.fail(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);
return Api.wrapAjaxCall({
url,
data: Object.assign({
search: query,
per_page: 20,
}, options),
dataType: 'json',
});
},
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);
},
wrapAjaxCall(options) {
return new Promise((resolve, reject) => {
// jQuery 2 is not Promises/A+ compatible (missing catch)
$.ajax(options) // eslint-disable-line promise/catch-or-return
.then(data => resolve(data),
(jqXHR, textStatus, errorThrown) => {
const error = new Error(`${options.url}: ${errorThrown}`);
error.textStatus = textStatus;
reject(error);
},
);
});
},
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;