Skip to content

Commit cdc3cf2

Browse files
authored
Merge pull request #4 from ssulei7/ssulei7/support-json-output
Support JSON output type
2 parents fb47494 + 75af3ed commit cdc3cf2

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

.github/release-drafter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
2727
version-resolver:
2828
major:
2929
labels:
30-
- 'type: breaking'
30+
- 'type: feature'
3131
minor:
3232
labels:
3333
- 'type: enhancement'

cmd/report.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ var reportCmd = &cobra.Command{
2828
log.Fatalf("Error getting number of workflow runs to evaluate: %v", err)
2929
}
3030

31-
// Create the report CSV file
32-
report.CreateInitialReportCSVFile(orgName)
31+
outputType, err := cmd.Flags().GetString("output-type")
32+
if err != nil {
33+
log.Fatalf("Error getting output type: %v", err)
34+
}
35+
36+
report.CreateInitialOutputFile(outputType, orgName)
3337

3438
// Get repositories in the organization
3539
repositories := repository.GetOrgRepositories(orgName)
@@ -90,8 +94,8 @@ var reportCmd = &cobra.Command{
9094
}
9195
}
9296

93-
// Write as a row in a CSV file
94-
report.AddRecordToCSVFile(orgName, repo.FullName, workflow, total/float64(numberOfWorkflowRunsToEvaluate))
97+
// Write a new record to the output file
98+
report.AddRecord(outputType, orgName, repo.FullName, workflow, total/float64(numberOfWorkflowRunsToEvaluate))
9599
}
96100
}
97101
},

cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func init() {
1515
reportCmd.Flags().String("org-name", "", "The name of the GitHub organization")
1616
reportCmd.Flags().StringSlice("runner-labels", []string{}, "The labels that you use for your jobs (can be both user defined and GitHub defined) separated by commas")
1717
reportCmd.Flags().Int("num-workflow-runs-to-evaluate", 1, "The number of workflow runs to evaluate for a workflow")
18+
reportCmd.Flags().String("output-type", "csv", "The type of output to generate (csv or json)")
1819
err := reportCmd.MarkFlagRequired("org-name")
1920
if err != nil {
2021
log.Fatal(err)

internal/report/report.go

+75
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ package report
22

33
import (
44
"encoding/csv"
5+
"encoding/json"
6+
"fmt"
57
"os"
68
"strconv"
79
)
810

11+
type JSONRecordArray []map[string]string
12+
13+
func CreateInitialOutputFile(outputType string, orgName string) {
14+
if outputType == "csv" {
15+
CreateInitialReportCSVFile(orgName)
16+
} else if outputType == "json" {
17+
CreateInitialJSONFile(orgName)
18+
}
19+
}
20+
921
func CreateInitialReportCSVFile(orgName string) {
1022
// Create a new csv file
1123
file, err := os.Create(orgName + "-runner-minute-average-report.csv")
@@ -28,6 +40,27 @@ func CreateInitialReportCSVFile(orgName string) {
2840
writer.Flush()
2941
}
3042

43+
func CreateInitialJSONFile(orgName string) {
44+
// Create a new json file
45+
file, err := os.Create(orgName + "-runner-minute-average-report.json")
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
defer file.Close()
51+
52+
// Write opening bracket and closing bracket to the json file
53+
file.WriteString("[\n]")
54+
}
55+
56+
func AddRecord(outputType string, orgName string, repoName string, workflowName string, averageSelfHostedMinutes float64) {
57+
if outputType == "csv" {
58+
AddRecordToCSVFile(orgName, repoName, workflowName, averageSelfHostedMinutes)
59+
} else if outputType == "json" {
60+
AddRecordToJSONFile(orgName, repoName, workflowName, averageSelfHostedMinutes)
61+
}
62+
}
63+
3164
func AddRecordToCSVFile(orgName string, repoName string, workflowName string, averageSelfHostedMinutes float64) {
3265
// Open the csv file
3366
file, err := os.OpenFile(orgName+"-runner-minute-average-report.csv", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
@@ -49,3 +82,45 @@ func AddRecordToCSVFile(orgName string, repoName string, workflowName string, av
4982
// Flush the csv writer to write any buffered data to the file
5083
writer.Flush()
5184
}
85+
86+
func AddRecordToJSONFile(orgName string, repoName string, workflowName string, averageSelfHostedMinutes float64) {
87+
// Load JSON file as an array of struct
88+
var records JSONRecordArray
89+
90+
// Open the json file
91+
file, err := os.ReadFile(orgName + "-runner-minute-average-report.json")
92+
if err != nil {
93+
panic(err)
94+
}
95+
96+
// Unmarshal the json file into the records array using json.Unmarshal
97+
err = json.Unmarshal(file, &records)
98+
if err != nil {
99+
panic(err)
100+
}
101+
102+
// Print the records array
103+
fmt.Println(records)
104+
105+
// Create a new record
106+
record := map[string]string{
107+
"Repository": repoName,
108+
"Workflow": workflowName,
109+
"Average Runner Minutes": strconv.FormatFloat(averageSelfHostedMinutes, 'f', 2, 64),
110+
}
111+
112+
// Append the new record to the records array
113+
records = append(records, record)
114+
115+
// Marshal the records array into a json string
116+
jsonString, err := json.MarshalIndent(records, "", " ")
117+
if err != nil {
118+
panic(err)
119+
}
120+
121+
// Write the json string to the json file
122+
err = os.WriteFile(orgName+"-runner-minute-average-report.json", jsonString, 0644)
123+
if err != nil {
124+
panic(err)
125+
}
126+
}

0 commit comments

Comments
 (0)