Skip to content

Commit aafb399

Browse files
committed
feat: json出力
1 parent 9a78dce commit aafb399

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

main.go

+61-12
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ import (
66
"github.com/cli/go-gh"
77
"github.com/cli/go-gh/pkg/api"
88
"github.com/cli/go-gh/pkg/repository"
9+
"log"
910
"strconv"
1011
)
1112

1213
type Run struct {
13-
WorkflowId int `json:"workflow_id"`
14-
JobsUrl string `json:"jobs_url"`
15-
Id int `json:"id"`
14+
WorkflowId int `json:"workflow_id"`
15+
JobsUrl string `json:"jobs_url"`
16+
Id int `json:"id"`
17+
Event string `json:"event"`
18+
DisplayTitle string `json:"display_title"`
19+
HeadBranch string `json:"head_branch"`
20+
HtmlUrl string `json:"html_url"`
21+
Name string `json:"name"`
22+
Path string `json:"path"`
23+
Status string `json:"status"`
1624
}
1725

1826
type WorkflowRuns struct {
@@ -22,6 +30,7 @@ type WorkflowRuns struct {
2230

2331
type Job struct {
2432
Id int `json:"id"`
33+
Name string `json:"name"`
2534
CheckRunUrl string `json:"check_run_url"`
2635
Conclusion string `json:"conclusion"`
2736
Status string `json:"status"`
@@ -33,7 +42,7 @@ type WorkflowJobs struct {
3342
Jobs []Job `json:"jobs"`
3443
}
3544

36-
type Annotations []struct {
45+
type Annotation struct {
3746
Path string `json:"path"`
3847
BlobHref string `json:"blob_href"`
3948
Title string `json:"title"`
@@ -46,6 +55,18 @@ type Annotations []struct {
4655
EndColumn int `json:"end_column"`
4756
}
4857

58+
type Record struct {
59+
Repository string `json:"repository"`
60+
WorkflowName string `json:"workflow_name"`
61+
WorkflowEvent string `json:"workflow_event"`
62+
WorkflowPath string `json:"workflow_path"`
63+
WorkflowUrl string `json:"workflow_url"`
64+
JobName string `json:"job_name"`
65+
JobConclusion string `json:"job_conclusion"`
66+
AnnotationLevel string `json:"annotation_level"`
67+
Message string `json:"message"`
68+
}
69+
4970
func latest(workflowRuns WorkflowRuns) []Run {
5071
var latestRuns []Run
5172
for _, run := range workflowRuns.Runs {
@@ -94,20 +115,36 @@ func getJobs(client api.RESTClient, repository repository.Repository, run Run) W
94115
return jobs
95116
}
96117

97-
func getAnnotations(client api.RESTClient, repository repository.Repository, job Job) Annotations {
118+
func getAnnotations(client api.RESTClient, repository repository.Repository, job Job) []Annotation {
98119
var res []interface{}
99120
path := "repos/" + repository.Owner() + "/" + repository.Name() + "/check-runs/" + strconv.Itoa(job.Id) + "/annotations"
100121
client.Get(path, &res)
101122

102123
jsonStr, _ := json.Marshal(res)
103-
var annotations Annotations
124+
var annotations []Annotation
104125
if err := json.Unmarshal([]byte(jsonStr), &annotations); err != nil {
105126
panic(err)
106127
}
107128

108129
return annotations
109130
}
110131

132+
func toRecord(repository repository.Repository, run Run, job Job, annotation Annotation) Record {
133+
r := Record{
134+
Repository: repository.Owner() + "/" + repository.Name(),
135+
WorkflowName: run.Name,
136+
WorkflowEvent: run.Event,
137+
WorkflowPath: run.Path,
138+
WorkflowUrl: run.HtmlUrl,
139+
JobName: job.Name,
140+
JobConclusion: job.Conclusion,
141+
AnnotationLevel: annotation.AnnotationLevel,
142+
Message: annotation.Message,
143+
}
144+
145+
return r
146+
}
147+
111148
func main() {
112149
client, err := gh.RESTClient(nil)
113150
if err != nil {
@@ -116,19 +153,31 @@ func main() {
116153
}
117154
currentRepository, _ := gh.CurrentRepository()
118155

119-
//fmt.Printf("%+v\n", workflowRunsRes)
120-
121156
workflowRuns := getRuns(client, currentRepository)
122157
latestRuns := latest(workflowRuns)
123-
fmt.Printf("%+v\n", latestRuns)
124158

159+
var summary []Record
160+
161+
//fmt.Printf("Repository: %s/%s\n", currentRepository.Owner(), currentRepository.Name())
125162
for _, run := range latestRuns {
126-
jobs := getJobs(client, currentRepository, run)
163+
//fmt.Printf("Workflow(%s): %s(%s)\n", run.Event, run.Name, run.Path)
127164

165+
jobs := getJobs(client, currentRepository, run)
128166
for _, job := range jobs.Jobs {
167+
//fmt.Printf("\tJob name: %s, %s\n", job.Name, job.Conclusion)
129168
annotations := getAnnotations(client, currentRepository, job)
130-
fmt.Print("\n===========================================\n")
131-
fmt.Printf("%+v\n", annotations)
169+
for _, annotation := range annotations {
170+
//fmt.Printf("\t\t%s: %s\n", annotation.AnnotationLevel, annotation.Message)
171+
r := toRecord(currentRepository, run, job, annotation)
172+
summary = append(summary, r)
173+
}
132174
}
133175
}
176+
177+
j, err := json.Marshal(summary)
178+
if err != nil {
179+
log.Fatal(err)
180+
}
181+
182+
fmt.Printf("%s", string(j))
134183
}

0 commit comments

Comments
 (0)