This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.ts
178 lines (150 loc) · 4.88 KB
/
space.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
export class Space {
private _url: string
private _project: string
private _token: string
private _defaultStatusId: string
private _issueStatuses: SpaceIssueStatus[] = []
constructor(url: string, project: string, token: string, defaultStatusId: string) {
this._url = url
this._project = project
this._token = token
this._defaultStatusId = defaultStatusId
}
async init() {
this._issueStatuses = await this.getIssueStatuses()
if (this._issueStatuses.length === 0) {
throw new Error("No issue statuses found!")
}
if (!this._issueStatuses.find(status => status.id === this._defaultStatusId)) {
throw new Error("Default issue status not found!")
}
}
private _constructRequest(endpoint: string, body: string | null = null): AxiosRequestConfig {
return {
url: this._url + '/api/http/projects/id:' + this._project + endpoint,
method: body ? 'POST' : 'GET',
headers: {
'Authorization': 'Bearer ' + this._token,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data: body
}
}
public async createIssue(issue: SpaceIssue): Promise<any> {
let request = this._constructRequest('/planning/issues', JSON.stringify({
"title": issue.title,
"description": issue.description,
"status": this._defaultStatusId
}))
const response:AxiosResponse = await axios.request(request)
if (response.status != 200) {
throw new Error("Creating issue: " + response.statusText)
}
return await response.data
}
public async getIssue(id: string): Promise<any> {
let request = this._constructRequest('/planning/issues/id:' + id)
const response = await axios.request(request)
if (response.status != 200) {
throw new Error("Getting issue: " + response.statusText)
}
return await response.data
}
public async updateIssue(issue: SpaceIssue): Promise<any> {
let request = this._constructRequest('/planning/issues/id:' + issue.id, JSON.stringify({
"title": issue.title,
"description": issue.description,
"status": process.env['SPACE_DEFAULT_STATUS']
}))
const response = await axios.request(request)
if (response.status != 200) {
throw new Error("Updating issue: " + response.statusText)
}
return await response.data
}
private async getIssueStatuses(): Promise<SpaceIssueStatus[]> {
let request = this._constructRequest('/planning/issues/statuses')
const response = await axios.request(request)
if (response.status != 200) {
throw new Error("Getting issue statuses: " + response.statusText)
}
let json = await response.data as any[]
let statuses: SpaceIssueStatus[] = []
for (let status of json) {
statuses.push(new SpaceIssueStatus(status))
}
return statuses
}
public getIssueStatusList(): SpaceIssueStatus[] {
return this._issueStatuses
}
}
export class SpaceIssueStatus {
private _id: string
private _name: string
private _color: string
private _isResolved: boolean
private _isArchived: boolean
constructor(jsonBody: any) {
this._id = jsonBody.id
this._isArchived = jsonBody.archived
this._name = jsonBody.name
this._color = jsonBody.color
this._isResolved = jsonBody.resolved
}
get id(): string {
return this._id
}
get name(): string {
return this._name
}
get color(): string {
return this._color
}
get isResolved(): boolean {
return this._isResolved
}
get isArchived(): boolean {
return this._isArchived
}
}
export class SpaceIssue {
private _id: string | null = null
private _title: string
private _description: string
private _tags: string[] = []
constructor(title: string, description: string) {
this._title = title
this._description = description
}
get id(): string | null {
return this._id
}
get title(): string {
return this._title
}
get description(): string {
return this._description
}
get tags(): string[] {
return this._tags
}
addTag(tag: string) {
this._tags.push(tag)
}
removeTag(tag: string) {
let index = this._tags.indexOf(tag)
if (index > -1) {
this._tags.splice(index, 1)
}
}
toJSONObject(): any {
return {
"title": this._title,
"description": this._description,
"tags": this._tags
}
}
}