-
Notifications
You must be signed in to change notification settings - Fork 1
/
webide.go
197 lines (164 loc) · 4.31 KB
/
webide.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"path"
"strings"
"time"
)
func addOrionHeaders(request *http.Request) {
request.Header.Add("Orion-Version", "1")
request.Header.Add("Jazz-Version", "2")
request.Header.Add("Content-Type", "application/json; charset=UTF-8")
request.Header.Add("X-Requested-With", "XMLHttpRequest")
}
type OrionResponse struct {
Result OrionResult
}
type OrionResult struct {
HttpCode int
Message string
JsonData interface{}
}
func waitForOrionResponse(client *Client, resp *http.Response, v interface{}) error {
if resp.StatusCode == 200 {
defer resp.Body.Close()
if v != nil {
b, _ := ioutil.ReadAll(resp.Body)
err := json.Unmarshal(b, v)
return err
}
return nil
} else if resp.StatusCode != 202 {
defer resp.Body.Close()
return errorFromResponse(resp)
}
taskLocation := resp.Header.Get("Location")
taskLocation = path.Join(jazzHubBaseUrl, taskLocation)
taskLocation = strings.Replace(taskLocation, ":/", "://", 1)
for {
resp.Body.Close()
<-time.After(100 * time.Millisecond)
request, err := http.NewRequest("GET", taskLocation, nil)
if err != nil {
return err
}
request.Header.Add("Orion-Version", "1")
resp, err := client.Do(request)
if err != nil {
return err
}
if resp.StatusCode != 200 {
defer resp.Body.Close()
return errorFromResponse(resp)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err == io.EOF {
// Spin and try again
continue
}
orionResp := &OrionResponse{}
orionResp.Result.JsonData = v
err = json.Unmarshal(b, orionResp)
if err != nil {
return err
}
if orionResp.Result.HttpCode != 200 && orionResp.Result.HttpCode != 0 {
requestString := resp.Request.Method + ": " + resp.Request.URL.String() + "\n"
return &JazzError{Msg: orionResp.Result.Message, StatusCode: orionResp.Result.HttpCode, Details: requestString + string(b), Log: orionResp.Result.HttpCode > 499}
}
if orionResp.Result.HttpCode != 0 {
return nil
}
}
}
type InitWebIdeProjectResult struct {
Workspace InitWebIdeWorkspace `json:"workspace"`
}
type InitWebIdeWorkspace struct {
ItemId string `json:"workspaceItemId"`
}
func initWebIdeProject(client *Client, project Project, userName string) (string, error) {
url := path.Join(jazzHubBaseUrl, "/code/jazz/Project/")
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("POST", url, strings.NewReader(`{
"Init": true,
"repositoryUrl": "`+project.CcmBaseUrl+`",
"projectName": "`+project.Name+`",
"uuid": "`+project.ItemId+`",
"user": "`+userName+`",
"deleteSource": true,
"initProject": true,
"initReadme": false
}`))
if err != nil {
return "", err
}
addOrionHeaders(request)
resp, err := client.Do(request)
if err != nil {
return "", err
}
result := &InitWebIdeProjectResult{}
err = waitForOrionResponse(client, resp, result)
if err != nil {
return "", err
}
return result.Workspace.ItemId, nil
}
func loadWorkspace(client *Client, projectName string, workspaceId string) error {
if client.GetJazzId() == "" {
return errors.New("Not logged in")
}
url := path.Join(jazzHubBaseUrl, "/code/jazz/Workspace/", workspaceId, "file", client.GetJazzId()+"-OrionContent", projectName)
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("POST", url, strings.NewReader(`{
"Load": true
}`))
if err != nil {
return err
}
addOrionHeaders(request)
resp, err := client.Do(request)
if err != nil {
return err
}
var result struct{}
err = waitForOrionResponse(client, resp, &result)
if err != nil {
return err
}
return nil
}
func findWebIdeProject(client *Client, project Project) (string, error) {
if client.GetJazzId() == "" {
return "", errors.New("Not logged in")
}
url := path.Join(jazzHubBaseUrl, "/code/file", client.GetJazzId()+"-OrionContent", project.Name) + "?parts=meta"
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
addOrionHeaders(request)
resp, err := client.Do(request)
if err != nil {
return "", err
}
var result struct{}
err = waitForOrionResponse(client, resp, &result)
if err != nil {
jazzError, ok := err.(*JazzError)
if ok {
if jazzError.StatusCode == 404 {
return "", nil
}
}
return "", err
}
return project.Name, nil
}