2017-05-19 21:22:46 +00:00
|
|
|
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',
|
2017-07-25 13:18:45 +00:00
|
|
|
commitPath: '/api/:version/projects/:id/repository/commits',
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
group(groupId, callback) {
|
|
|
|
const url = Api.buildUrl(Api.groupPath)
|
|
|
|
.replace(':id', groupId);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(group => callback(group));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return groups list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
groups(query, options, callback) {
|
|
|
|
const url = Api.buildUrl(Api.groupsPath);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
data: Object.assign({
|
2017-03-11 07:30:44 +00:00
|
|
|
search: query,
|
2017-05-19 21:22:46 +00:00
|
|
|
per_page: 20,
|
2017-03-11 07:30:44 +00:00
|
|
|
}, options),
|
2017-05-19 21:22:46 +00:00
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(groups => callback(groups));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return namespaces list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
namespaces(query, callback) {
|
|
|
|
const url = Api.buildUrl(Api.namespacesPath);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
2017-03-11 07:30:44 +00:00
|
|
|
data: {
|
|
|
|
search: query,
|
2017-05-19 21:22:46 +00:00
|
|
|
per_page: 20,
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
dataType: 'json',
|
|
|
|
}).done(namespaces => callback(namespaces));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return projects list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
projects(query, options, callback) {
|
|
|
|
const url = Api.buildUrl(Api.projectsPath);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
data: Object.assign({
|
2017-03-11 07:30:44 +00:00
|
|
|
search: query,
|
2017-03-06 05:03:21 +00:00
|
|
|
per_page: 20,
|
2017-05-19 21:22:46 +00:00
|
|
|
membership: true,
|
2017-03-06 05:03:21 +00:00
|
|
|
}, options),
|
2017-05-19 21:22:46 +00:00
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(projects => callback(projects));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
newLabel(namespacePath, projectPath, data, callback) {
|
|
|
|
const url = Api.buildUrl(Api.labelsPath)
|
|
|
|
.replace(':namespace_path', namespacePath)
|
|
|
|
.replace(':project_path', projectPath);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
type: 'POST',
|
|
|
|
data: { label: data },
|
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(label => callback(label))
|
2017-06-15 21:21:53 +00:00
|
|
|
.fail(message => callback(message.responseJSON));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return group projects list. Filtered by query
|
2017-05-19 21:22:46 +00:00
|
|
|
groupProjects(groupId, query, callback) {
|
|
|
|
const url = Api.buildUrl(Api.groupProjectsPath)
|
|
|
|
.replace(':id', groupId);
|
2017-03-11 07:30:44 +00:00
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
2017-03-11 07:30:44 +00:00
|
|
|
data: {
|
|
|
|
search: query,
|
2017-05-19 21:22:46 +00:00
|
|
|
per_page: 20,
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(projects => callback(projects));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
2017-07-25 17:52:46 +00:00
|
|
|
commitMultiple(id, data, callback, token) {
|
2017-07-25 13:18:45 +00:00
|
|
|
// see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
|
|
|
|
const url = Api.buildUrl(Api.commitPath)
|
|
|
|
.replace(':id', id);
|
|
|
|
return $.ajax({
|
|
|
|
url,
|
2017-07-25 17:52:46 +00:00
|
|
|
headers: {
|
|
|
|
'PRIVATE_TOKEN': token,
|
|
|
|
},
|
2017-07-25 13:18:45 +00:00
|
|
|
type: 'POST',
|
|
|
|
data: data,
|
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(commitData => callback(commitData))
|
|
|
|
.fail(message => callback(message.responseJSON));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
// Return text for a specific license
|
2017-05-19 21:22:46 +00:00
|
|
|
licenseText(key, data, callback) {
|
|
|
|
const url = Api.buildUrl(Api.licensePath)
|
2017-03-11 07:30:44 +00:00
|
|
|
.replace(':key', key);
|
|
|
|
return $.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
.done(license => callback(license));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
gitignoreText(key, callback) {
|
|
|
|
const url = Api.buildUrl(Api.gitignorePath)
|
2017-03-11 07:30:44 +00:00
|
|
|
.replace(':key', key);
|
2017-05-19 21:22:46 +00:00
|
|
|
return $.get(url, gitignore => callback(gitignore));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
gitlabCiYml(key, callback) {
|
|
|
|
const url = Api.buildUrl(Api.gitlabCiYmlPath)
|
2017-03-11 07:30:44 +00:00
|
|
|
.replace(':key', key);
|
2017-05-19 21:22:46 +00:00
|
|
|
return $.get(url, file => callback(file));
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
dockerfileYml(key, callback) {
|
|
|
|
const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
|
2017-03-11 07:30:44 +00:00
|
|
|
$.get(url, callback);
|
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
issueTemplate(namespacePath, projectPath, key, type, callback) {
|
|
|
|
const url = Api.buildUrl(Api.issuableTemplatePath)
|
2017-03-11 07:30:44 +00:00
|
|
|
.replace(':key', key)
|
|
|
|
.replace(':type', type)
|
|
|
|
.replace(':project_path', projectPath)
|
|
|
|
.replace(':namespace_path', namespacePath);
|
|
|
|
$.ajax({
|
2017-05-19 21:22:46 +00:00
|
|
|
url,
|
|
|
|
dataType: 'json',
|
|
|
|
})
|
|
|
|
.done(file => callback(null, file))
|
2017-06-15 21:21:53 +00:00
|
|
|
.fail(callback);
|
2017-03-11 07:30:44 +00:00
|
|
|
},
|
2017-05-19 21:22:46 +00:00
|
|
|
|
|
|
|
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-03-11 07:30:44 +00:00
|
|
|
if (gon.relative_url_root != null) {
|
2017-05-19 21:22:46 +00:00
|
|
|
urlRoot = gon.relative_url_root;
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
2017-05-19 21:22:46 +00:00
|
|
|
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-03-11 07:30:44 +00:00
|
|
|
};
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-05-19 21:22:46 +00:00
|
|
|
export default Api;
|