forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowner.js
More file actions
executable file
·132 lines (118 loc) · 3.89 KB
/
owner.js
File metadata and controls
executable file
·132 lines (118 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const _ = require('underscore');
const Base = require('./base');
/**
* owner model
*/
class owner extends Base {
constructor(config) {
super(config);
this.projects = [];
this.groups = [];
this.users = [];
}
/**
* is authorized?
* @returns {Promise}
*/
authorized() {
if (!this.config.get('_checkToken')) return new Promise(r => r());
return new Promise((resolve, reject) => {
this.get('broadcast_messages')
.then(() => resolve())
.catch(e => {
if (e.statusCode === 403) resolve();
reject(e);
});
});
}
/**
* query and set the group
* @returns {Promise}
*/
getGroup() {
return new Promise((resolve, reject) => {
this.get(`groups/?search=${encodeURIComponent(this.config.get('project'))}`)
.then(group => {
if (group.body.length === 0) return reject('Group not found');
let filtered = group.body.filter(u => u.path === this.config.get('project'));
if (filtered.length === 0) return reject();
this.groups = this.groups.concat(filtered);
resolve();
})
.catch(e => reject(e));
});
}
/**
* get sub groups
* @returns {Promise}
*/
getSubGroups() {
return new Promise((resolve, reject) => {
this.get(`groups`)
.then(groups => {
if (groups.body.length === 0) return resolve();
let filtered = this._filterGroupsByParents(groups.body, this.groups.map(g => g.id));
if (filtered.length === 0) return resolve();
this.groups = this.groups.concat(filtered);
resolve();
})
.catch(e => reject(e));
});
}
_filterGroupsByParents(groups, parents) {
let filtered = groups.filter(group => {
return parents.indexOf(group.parent_id) !== -1;
});
if (filtered.length !== 0) {
filtered = filtered.concat(this._filterGroupsByParents(groups, filtered.map(g => g.id)));
}
return filtered;
}
// /**
// * query and set the user
// * @returns {Promise}
// */
// getUser() {
// return new Promise((resolve, reject) => {
// this.get(`users/?username=${encodeURIComponent(this.config.get('project'))}`)
// .then(user => {
// if (user.body.length === 0) return reject();
// let filtered = user.body.filter(u => u.username === this.config.get('project'));
// if (filtered.length === 0) return reject();
// this.user = filtered[0];
// resolve();
// })
// .catch(e => reject(e));
// });
// }
//
// /**
// * query and set the projects by a user
// * @returns {Promise}
// */
// getProjectsByUser() {
// return new Promise((resolve, reject) => {
// this.get(`users/${this.user.id}/projects`)
// .then(projects => {
// this.projects = this.projects.concat(projects.body);
// resolve();
// })
// .catch(e => reject(e));
// });
// }
/**
* query and set the projects by a user
* @returns {Promise}
*/
getProjectsByGroup() {
return this.parallel(this.groups, (group, done) => {
this.get(`groups/${group.id}/projects`)
.then(projects => {
this.projects = this.projects.concat(projects.body);
done();
})
.catch(e => done(e));
});
}
}
module.exports = owner;