Skip to content

Commit 68e84fa

Browse files
committed
Add mechanic to check access token before querying projects
1 parent 7db3ea4 commit 68e84fa

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/gtt-report.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ program
5252
.option('--user_columns', 'include user columns in the report')
5353
.option('--quiet', 'only output report')
5454
.option('--verbose', 'show verbose output')
55+
.option('--check_token', 'check the access token')
5556
.option('--show_without_times', 'show issues/merge requests without time records')
5657
.option('-p --proxy <proxy>', 'use a proxy server with the given url')
5758
.parse(process.argv);
@@ -90,7 +91,8 @@ config
9091
.set('proxy', program.proxy)
9192
.set('type', program.type)
9293
.set('subgroups', program.subgroups)
93-
.set('_verbose', program.verbose);
94+
.set('_verbose', program.verbose)
95+
.set('_checkToken', program.check_token);
9496

9597
Cli.quiet = config.get('quiet');
9698
Cli.verbose = config.get('_verbose');
@@ -148,8 +150,9 @@ new Promise(resolve => {
148150
Cli.list(`${Cli.look} Resolving "${projectLabels}"`);
149151
let owner = new Owner(config);
150152

151-
owner
152-
.parallel(projects, (project, done) => {
153+
owner.authorized()
154+
.catch(e => Cli.x(`Invalid access token!`, e))
155+
.then(() => owner.parallel(projects, (project, done) => {
153156
config.set('project', project);
154157

155158
switch (config.get('type')) {
@@ -158,7 +161,7 @@ new Promise(resolve => {
158161
reports.push(report);
159162
report.getProject()
160163
.then(() => done())
161-
.catch(e => done(e));
164+
.catch(e => Cli.x(`Project not found or no access rights "${projectLabels}". Run again with --check_token to see if your access token is invalid!`, e));
162165
break;
163166

164167
case 'group':
@@ -175,7 +178,7 @@ new Promise(resolve => {
175178
.catch(e => done(e));
176179
break;
177180
}
178-
}, 1)
181+
}, 1))
179182
.catch(e => reject(e))
180183
.then(() => {
181184
config.set('project', projects);

src/include/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const defaults = {
3535
showWithoutTimes: false,
3636
_perPage: 100,
3737
_parallel: 10,
38-
_verbose: false
38+
_verbose: false,
39+
_checkToken: false
3940
};
4041

4142
/**

src/models/owner.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ class owner extends Base {
1313
this.users = [];
1414
}
1515

16+
/**
17+
* is authorized?
18+
* @returns {Promise}
19+
*/
20+
authorized() {
21+
if (!this.config.get('_checkToken')) return new Promise(r => r());
22+
23+
return new Promise((resolve, reject) => {
24+
this.get('broadcast_messages')
25+
.then(() => resolve())
26+
.catch(e => {
27+
if (e.statusCode === 403) resolve();
28+
reject(e);
29+
});
30+
});
31+
}
32+
1633
/**
1734
* query and set the group
1835
* @returns {Promise}

0 commit comments

Comments
 (0)