forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathowner.js
More file actions
executable file
·130 lines (116 loc) · 3.92 KB
/
Copy pathowner.js
File metadata and controls
executable file
·130 lines (116 loc) · 3.92 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
import GitlabClient from './gitlab-client.js';
import parallel from './parallel.js';
/**
* owner model
*/
class Owner {
constructor(config, client = new GitlabClient(config)) {
this.config = config;
this.client = client;
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.client.get('broadcast_messages')
.then(() => resolve())
.catch(e => {
if (e.statusCode === 403) resolve();
reject(e);
});
});
}
/**
* query and set the group
* @param fullPath group path, e.g. "group/subgroup"
* @returns {Promise}
*/
getGroup(fullPath = this.config.get('project')) {
return new Promise((resolve, reject) => {
this.client.get(`groups`)
.then(groups => {
if (groups.body.length === 0) return reject('Group not found');
groups = groups.body;
let filtered = groups.filter(group => group.full_path === fullPath);
if (filtered.length === 0) return reject('Group not found');
this.groups = this.groups.concat(filtered);
resolve();
})
.catch(e => reject(e));
});
}
/**
* get sub groups
* @returns {Promise}
*/
getSubGroups() {
return new Promise((resolve, reject) => {
this.client.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 parallel(this.groups, async group => {
this.projects = await this.client.all(`groups/${group.id}/projects`);
}, this.config);
}
}
export default Owner;