3 const GitLabGroupService = require('./group-service');
4 const GitLabProjectService = require('./project-service');
5 const GitLabUserService = require('./user-service');
6 const debug = require('debug')('gitter:app:gitlab:gitlab-uri-validator');
8 // If it's a network or permissions problem, throw the error
9 // We don't care about the errors when it does not find a user because it could
10 // be a group or project or vice versa
11 function throwNetworkErrors(err) {
12 if (err.response && err.response.status && err.response.status !== 404) {
18 * Given a uri, is it a valid GitLab group, user, or project
19 * @returns promise of GROUP / USER / PROJECT or null
21 async function validateGitlabUri(user, uri) {
22 debug('validateUri: %s', uri);
25 debug('validateUri -> user: %s', uri);
26 const userService = new GitLabUserService(user);
27 const gitlabUser = await userService.getUserByUsername(uri);
31 uri: gitlabUser.username,
32 description: gitlabUser.name,
33 externalId: parseInt(gitlabUser.id, 10) || undefined
36 throwNetworkErrors(err);
40 debug('validateUri -> group: %s', uri);
41 const groupService = new GitLabGroupService(user);
42 const group = await groupService.getGroup(uri);
47 description: group.name,
48 externalId: parseInt(group.id, 10) || undefined
51 throwNetworkErrors(err);
55 debug('validateUri -> project: %s', uri);
56 const projectService = new GitLabProjectService(user);
57 const project = await projectService.getProject(uri);
62 description: project.name,
63 externalId: parseInt(project.id, 10) || undefined
66 throwNetworkErrors(err);
72 module.exports = validateGitlabUri;