Skip to content

Commit

Permalink
Update jira_to_analytics.go
Browse files Browse the repository at this point in the history
Added JSON output
  • Loading branch information
toddconley committed Sep 11, 2015
1 parent 35d0a80 commit b697a3a
Showing 1 changed file with 60 additions and 15 deletions.
75 changes: 60 additions & 15 deletions jira_to_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 1. Parse command-line flags and config file
// 2. Fetch Jira Issue Keys in batches
// 3. Fetch Jira Issues by key in batches, using them to build work items
// 4. Write out CSV, one work item per line
// 4. Write out CSV or JSON, one work item per line

package main

Expand All @@ -25,9 +25,9 @@ func main() {

// parse command-line args
yamlName := flag.String("i", "config.yaml", "set the input config file name")
csvName := flag.String("o", "data.csv", "set the output CSV file name")
outName := flag.String("o", "data.csv", "set the output file name (.csv or .json)")
printVersion := flag.Bool("v", false, "print the version number and exit")
showJQL := flag.Bool("j", false, "display the jql used")
showQuery := flag.Bool("q", false, "display the query used")
flag.Parse()
if flag.NArg() > 0 {
fmt.Println("Unexpected argument \"" + flag.Args()[0] + "\"")
Expand All @@ -38,8 +38,9 @@ func main() {
fmt.Println(version)
os.Exit(0)
}
if !strings.HasSuffix(*csvName, ".csv") {
*csvName = *csvName + ".csv"
if !strings.HasSuffix(*outName, ".csv") && !strings.HasSuffix(*outName, ".json") {
fmt.Println("Error: output file name must end with .csv or .json")
os.Exit(1)
}

// load config and set password
Expand All @@ -56,14 +57,14 @@ func main() {
// collect all the keys
fmt.Println("Fetching issue keys...")
var keys, keyBatch []string
var jql string
var query string
maxKey := ""
for keyBatch, jql, err = getKeys(keyBatchSize, maxKey, config); err == nil && len(keyBatch) > 0; keyBatch, _, err = getKeys(keyBatchSize, maxKey, config) {
for keyBatch, query, err = getKeys(keyBatchSize, maxKey, config); err == nil && len(keyBatch) > 0; keyBatch, _, err = getKeys(keyBatchSize, maxKey, config) {
keys = append(keys, keyBatch...)
maxKey = keyBatch[len(keyBatch)-1]
if len(maxKey) == 0 { // first time only
if *showJQL {
fmt.Println("JQL:", jql)
if *showQuery {
fmt.Println("Query: ", query)
}
fmt.Println("\tLoaded", len(keyBatch), "keys")
} else {
Expand All @@ -81,27 +82,31 @@ func main() {
}
success := false
fmt.Print("\tLoading Issues ", i+1, "-", end, ": ")
for tries := 0; tries < maxTries; i++ {
for tries := 0; tries < maxTries; tries++ {
if itemBatch, err := getItems(keys[i:end], config); err == nil {
items = append(items, itemBatch...)
fmt.Println("ok")
success = true
break
}
if tries < maxTries-1 {
fmt.Print("\tRetrying issues ", i, "-", end-1, ": ")
fmt.Print("\tRetrying issues ", i+1, "-", end, ": ")
time.Sleep(time.Duration(tries*(retryDelay+1)) * time.Second) // delay increases
}
}
if !success {
fmt.Println("Error: Issues", i, "-", end-1, "failed to load")
fmt.Println("Error: Issues", i+1, "-", end, "failed to load")
os.Exit(1)
}
}

// output work items
fmt.Println("Writing", *csvName)
writeCSV(items, config, *csvName)
fmt.Println("Writing", *outName)
if strings.HasSuffix(*outName, ".json") {
writeJSON(items, config, *outName)
} else {
writeCSV(items, config, *outName)
}

// show elapsed time
elapsed := time.Since(start)
Expand Down Expand Up @@ -132,7 +137,7 @@ func writeCSV(items []*Item, config *Config, fileName string) {
counter := 0
for _, item := range items {
if item.HasDate() {
file.WriteString(item.String(config, writeLink))
file.WriteString(item.toCSV(config, writeLink))
file.WriteString("\n")
writeLink = false
counter++
Expand All @@ -146,3 +151,43 @@ func writeCSV(items []*Item, config *Config, fileName string) {
}
fmt.Println(counter, "work items written")
}

func writeJSON(items []*Item, config *Config, fileName string) {

// open the file
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer file.Close()

// write the header line
file.WriteString("[[\"ID\",\"Link\",\"Name\"")
for _, stage := range config.StageNames {
file.WriteString("," + quoteString(stage))
}
for _, attr := range config.Attributes {
file.WriteString("," + quoteString(attr.ColumnName))
}
file.WriteString("]")

// write a line for each work item
writeLink := true
counter := 0
for _, item := range items {
if item.HasDate() {
file.WriteString(",\n")
file.WriteString(item.toJSON(config, writeLink))
writeLink = false
counter++
}
}
file.WriteString("]\n")

// display some stats
skipped := len(items) - counter
if skipped > 0 {
fmt.Println(skipped, "empty work items omitted")
}
fmt.Println(counter, "work items written")
}

0 comments on commit b697a3a

Please sign in to comment.