forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partner.go
132 lines (123 loc) · 4.69 KB
/
partner.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
package main
import (
"encoding/xml"
"errors"
"fmt"
"strings"
)
type ForcePartner struct {
Force *Force
}
type TestCoverage struct {
Log string `xml:"Header>DebuggingInfo>debugLog"`
NumberRun int `xml:"Body>runTestsResponse>result>numTestsRun"`
NumberLocations []int `xml:"Body>runTestsResponse>result>codeCoverage>numLocations"`
NumberLocationsNotCovered []int `xml:"Body>runTestsResponse>result>codeCoverage>numLocationsNotCovered"`
Name []string `xml:"Body>runTestsResponse>result>codeCoverage>name"`
SMethodNames []string `xml:"Body>runTestsResponse>result>successes>methodName"`
SClassNames []string `xml:"Body>runTestsResponse>result>successes>name"`
FMethodNames []string `xml:"Body>runTestsResponse>result>failures>methodName"`
FClassNames []string `xml:"Body>runTestsResponse>result>failures>name"`
FMessage []string `xml:"Body>runTestsResponse>result>failures>message"`
FStackTrace []string `xml:"Body>runTestsResponse>result>failures>stackTrace"`
}
func NewForcePartner(force *Force) (partner *ForcePartner) {
partner = &ForcePartner{Force: force}
return
}
func (partner *ForcePartner) CheckStatus(id string) (err error) {
body, err := partner.soapExecute("checkStatus", fmt.Sprintf("<id>%s</id>", id))
if err != nil {
return
}
var status struct {
Done bool `xml:"Body>checkStatusResponse>result>done"`
State string `xml:"Body>checkStatusResponse>result>state"`
Message string `xml:"Body>checkStatusResponse>result>message"`
}
if err = xml.Unmarshal(body, &status); err != nil {
return
}
switch {
case !status.Done:
return partner.CheckStatus(id)
case status.State == "Error":
return errors.New(status.Message)
}
return
}
func (partner *ForcePartner) ExecuteAnonymous(apex string) (output string, err error) {
soap := "<apexcode><![CDATA[%s]]></apexcode>"
body, err := partner.soapExecute("executeAnonymous", fmt.Sprintf(soap, apex))
if err != nil {
return
}
var result struct {
Compiled bool `xml:"Body>executeAnonymousResponse>result>compiled"`
CompileProblem string `xml:"Body>executeAnonymousResponse>result>compileProblem"`
ExceptionMessage string `xml:"Body>executeAnonymousResponse>result>exceptionMessage"`
ExceptionTrace string `xml:"Body>executeAnonymousResponse>result>exceptionStackTrace"`
Log string `xml:"Header>DebuggingInfo>debugLog"`
Success bool `xml:"Body>executeAnonymousResponse>result>success"`
}
if err = xml.Unmarshal(body, &result); err != nil {
return
}
if !result.Compiled {
message := strings.Replace(result.CompileProblem, "%", "%%", -1)
err = errors.New(message)
return
}
output = result.Log
return
}
func (partner *ForcePartner) soapExecuteCore(action, query string) (response []byte, err error) {
login, err := partner.Force.Get(partner.Force.Credentials.Id)
if err != nil {
return
}
version := strings.Replace(apiVersion, "v", "", 1)
url := strings.Replace(login["urls"].(map[string]interface{})["partner"].(string), "{version}", version, 1)
//url = strings.Replace(url, "/u/", "/s/", 1) // seems dirty
soap := NewSoap(url, "urn:partner.soap.sforce.com", partner.Force.Credentials.AccessToken)
soap.Header = "<apex:DebuggingHeader><apex:debugLevel>DEBUGONLY</apex:debugLevel></apex:DebuggingHeader>"
response, err = soap.Execute(action, query)
return
}
func (partner *ForcePartner) RunTests(tests []string, namespace string) (output TestCoverage, err error) {
soap := "<RunTestsRequest>\n"
if strings.EqualFold(tests[0], "all") {
soap += "<allTests>True</allTests>\n"
} else {
for _, element := range tests {
soap += "<classes>" + element + "</classes>\n"
}
}
if namespace != "" {
soap += "<namespace>" + namespace + "</namespace>\n"
}
soap += "</RunTestsRequest>"
body, err := partner.soapExecute("runTests", soap)
if err != nil {
return
}
var result TestCoverage
if err = xml.Unmarshal(body, &result); err != nil {
return
}
output = result
return
}
func (partner *ForcePartner) soapExecute(action, query string) (response []byte, err error) {
login, err := partner.Force.Get(partner.Force.Credentials.Id)
if err != nil {
return
}
version := strings.Replace(apiVersion, "v", "", 1)
url := strings.Replace(login["urls"].(map[string]interface{})["partner"].(string), "{version}", version, 1)
url = strings.Replace(url, "/u/", "/s/", 1) // seems dirty
soap := NewSoap(url, "http://soap.sforce.com/2006/08/apex", partner.Force.Credentials.AccessToken)
soap.Header = "<apex:DebuggingHeader><apex:debugLevel>DEBUGONLY</apex:debugLevel></apex:DebuggingHeader>"
response, err = soap.Execute(action, query)
return
}