-
Notifications
You must be signed in to change notification settings - Fork 7
/
gitlab.js
139 lines (115 loc) · 4.64 KB
/
gitlab.js
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
133
134
135
136
137
138
139
// SPDX-License-Identifier: MIT
import GLib from 'gi://GLib';
import { gettext as _ } from 'gettext';
import Forge from './forge.js';
import Notification from '../model/notification.js';
import { session } from './../util.js';
export default class GitLab extends Forge {
static name = 'gitlab';
static prettyName = 'GitLab';
static allowInstances = true;
static defaultURL = 'gitlab.com';
static scopes = ['api'];
static get tokenText() {
/* GitLab access token help */
let tokenText = _('To generate a new access token from your instance, go to Preferences → Access Tokens and generate a new token.');
tokenText += '\n\n';
/* GitLab access token help */
tokenText += _('Forge Sparks requires the <i>api</i> scopes granted.');
return tokenText;
}
get authorization() {
return 'Bearer ' + this.token;
}
async getUser() {
try {
const url = this.buildURI('user');
const message = super.createMessage('GET', url);
const bytes = await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null);
const contents = super.readContents(bytes);
console.log(`${url} response resulted in ${message.get_status()}`);
if (message.get_status() == '401') {
throw 'FailedForgeAuth';
} else if (message.get_status() == '403') {
throw 'FailedTokenScopes';
} else if (message.get_status() != '200') {
throw 'Unexpected'
} else if (!('username' in contents) || !('id' in contents)) {
throw 'Unexpected'
}
return [contents.id, contents.username];
} catch (error) {
throw error;
}
}
async getNotifications() {
try {
const url = this.buildURI('todos');
const message = super.createMessage('GET', url);
const bytes = await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null);
const contents = super.readContents(bytes);
console.log(`${url} response resulted in ${message.get_status()}`);
if (message.get_status() == '200') {
/* Show notifications */
let notifications = [];
for (const item of contents) {
const notification = new Notification({
id: super.formatID(item.id),
type: item.target_type,
unread: (item.state === 'pending'),
updatedAt: item.updated_at,
state: item.target.state,
title: item.target.title,
repository: item.project.path_with_namespace,
url: item.target_url,
account_name: this.accountName
});
notifications.push(notification);
}
return notifications;
} else if (message.get_status() == '401') {
/* Auth failed, revoked or expired token */
throw 'FailedForgeAuth';
} else if (message.get_status() == '403') {
/* Unauthorized, token scopes */
throw 'FailedTokenScopes';
} else {
throw 'Unexpected'
}
} catch (e) {
throw e;
}
}
async markAsRead(id = null) {
try {
if (id != null) {
const url = this.buildURI(`todos/${id}/mark_as_done`);
const message = super.createMessage('POST', url);
await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null);
/* If OK */
return message.get_status() == '200';
} else {
const url = this.buildURI('todos/mark_as_done');
const message = super.createMessage('POST', url);
await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null);
/* If No Content */
return message.get_status() == '204';
}
} catch (e) {
throw e;
}
}
/**
* Build a request URI from multiple parts
*
* This is a simplified version of Forge.buildURI with passed instance url
* set as host and api v4 prepended to path
*
* @param {String} path The URI path
* @param {Object.<string, string>} query The URI query
* @returns {String} The resulting URI
*/
buildURI(path, query = {}) {
return Forge.buildURI(this.url, '/api/v4/' + path, query);
}
};