-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangedetails.go
More file actions
131 lines (107 loc) · 3.26 KB
/
changedetails.go
File metadata and controls
131 lines (107 loc) · 3.26 KB
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
slack "github.com/ashwanthkumar/slack-go-webhook"
resty "gopkg.in/resty.v1"
)
type tcResponseDetails struct {
ID int `json:"id"`
Version string `json:"version"`
Username string `json:"username"`
Date string `json:"date"`
Href string `json:"href"`
WebURL string `json:"webUrl"`
Comment string `json:"comment"`
Files struct {
Count int `json:"count"`
File []struct {
BeforeRevision string `json:"before-revision"`
AfterRevision string `json:"after-revision"`
ChangeType string `json:"changeType"`
File string `json:"file"`
RelativeFile string `json:"relative-file"`
} `json:"file"`
} `json:"files"`
VcsRootInstance struct {
ID string `json:"id"`
VcsRootID string `json:"vcs-root-id"`
Name string `json:"name"`
Href string `json:"href"`
} `json:"vcsRootInstance"`
}
// Details contain user, comment and version of a change
type Details struct {
user string
comment string
version string
}
type tcResponse struct {
Href string `json:"href"`
Change []struct {
ID int `json:"id"`
Version string `json:"version"`
Username string `json:"username"`
Date string `json:"date"`
Href string `json:"href"`
WebURL string `json:"webUrl"`
} `json:"change"`
Count int `json:"count"`
}
// ChangeDetails gathers details about the given changes from the TeamCity REST api
func ChangeDetails(tcServer, changeurl string) (change Details) {
tcChangeurl := fmt.Sprint(tcServer, changeurl)
resp, err := resty.R().
SetResult(tcResponseDetails{}).
SetHeader("Accept", "application/json").
Get(tcChangeurl)
if err != nil {
return
}
respBody := resp.Result().(*tcResponseDetails)
change = Details{respBody.Username, respBody.Comment, respBody.Version}
return change
}
// postChanges posts the collected changes to a slack channel
func postChanges(buildid int) (status string, err error) {
tcServer := fmt.Sprintf("http://%v:%v@%v", os.Getenv("TCUSER"), os.Getenv("TCPASS"), os.Getenv("TCHOST"))
tcPath := fmt.Sprintf("/app/rest/changes?locator=build:(id:%d)", buildid)
tcChangeurl := fmt.Sprint(tcServer, tcPath)
resp, err := resty.R().
SetResult(tcResponse{}).
SetHeader("Accept", "application/json").
Get(tcChangeurl)
if err != nil {
return
}
respBody := resp.Result().(*tcResponse)
changeMessage := make(map[int]Details)
for i := range respBody.Change {
_, changeMessage[i] = changeMessage, ChangeDetails(tcServer, respBody.Change[i].Href)
}
var b strings.Builder
for i := range changeMessage {
fmt.Fprintf(&b, "---\nUser: %v\nChange: %v\nVersion: %v\n\n", changeMessage[i].user, changeMessage[i].comment, changeMessage[i].version)
}
textMessage := fmt.Sprint(b.String())
if err != nil {
return
}
webhookURL := os.Getenv("SLACKWEBHOOKURL")
payload := slack.Payload{
Text: textMessage,
Username: "tcChanges",
}
slack.Send(webhookURL, "", payload)
return resp.Status(), err
}
func main() {
buildid, _ := strconv.Atoi(os.Getenv("TCBUILDID"))
_, err := postChanges(buildid)
if err != nil {
fmt.Println("Something went wrong")
}
fmt.Printf("Ran with BuildID: %v, SlackURL: %v, User: %v, Pass: %v\n", buildid, os.Getenv("SLACKWEBHOOKURL"), os.Getenv("TCUSER"), os.Getenv("TCPASS"))
}