-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (87 loc) · 2.8 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
type Story struct {
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
ExternalID string `json:"external_id, omitempty"`
StoryType string `json:"story_type"`
}
type StoryResponse []struct {
ProjectID int `json:"project_id"`
StoryList []Story `json:"stories"`
}
func main() {
// Part project flags
projectPtr := flag.String("project", "default", "project_id")
keyPtr := flag.String("key", "default", "tracker api key")
jiraPtr := flag.String("jira", "https://issues.apache.org/jira/browse/", "linked jira url")
flag.Parse()
//Print if default flags are used
if *projectPtr == "default" || *keyPtr == "default" {
PrintUsageAndExit()
}
fmt.Println("Fetching project with id", *projectPtr)
// create the url
done_url := fmt.Sprintf("https://www.pivotaltracker.com/services/v5/projects/%s/iterations?scope=done&limit=1&offset=-1", *projectPtr)
current_url := fmt.Sprintf("https://www.pivotaltracker.com/services/v5/projects/%s/iterations?scope=current&offset=2", *projectPtr)
// Make request to tracker
client := &http.Client{}
f, err := os.OpenFile("autobeat.md", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer f.Close()
now := time.Now().Local()
then := now.AddDate(0, 0, -7)
updateStr := fmt.Sprintf("# Update: %s - %s\n", then.Format("2006-01-02"), now.Format("2006-01-02"))
f.WriteString(updateStr)
f.WriteString("## Done\n")
stories := MakeStoryRequest(done_url, keyPtr, client)
buffer := PrintStories(stories, jiraPtr)
f.Write(buffer)
f.WriteString("## Next\n")
stories = MakeStoryRequest(current_url, keyPtr, client)
buffer = PrintStories(stories, jiraPtr)
f.Write(buffer)
fmt.Println("\nUpdate written to ./autobeat.md")
os.Exit(0)
}
func MakeStoryRequest(done_url string, keyPtr *string, client *http.Client) StoryResponse {
req, _ := http.NewRequest("GET", done_url, nil)
req.Header.Set("X-TrackerToken", *keyPtr)
res, _ := client.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
s, _ := ReadStoryResponse(body)
return s
}
func ReadStoryResponse(body []byte) (StoryResponse, error) {
s := StoryResponse{}
err := json.Unmarshal(body, &s)
if err != nil {
fmt.Println("Error parsing tracker reponse", err)
}
return s, err
}
func PrintStories(s StoryResponse, jiraPtr *string) (buffer []byte) {
for i := 0; i < len(s[0].StoryList); i++ {
story := s[0].StoryList[i]
if len(story.ExternalID) == 0 {
writeStr := fmt.Sprintf("* %s [Tracker](%s)\n", story.Name, story.URL)
buffer = append(buffer, writeStr...)
} else {
writeStr := fmt.Sprintf("* %s [JIRA](%s/%s) | [Tracker](%s)\n", story.Name, *jiraPtr, story.ExternalID, story.URL)
buffer = append(buffer, writeStr...)
}
}
return buffer
}