Skip to content

Commit

Permalink
feat: json出力
Browse files Browse the repository at this point in the history
  • Loading branch information
swfz committed Nov 1, 2022
1 parent 9a78dce commit aafb399
Showing 1 changed file with 61 additions and 12 deletions.
73 changes: 61 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import (
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/repository"
"log"
"strconv"
)

type Run struct {
WorkflowId int `json:"workflow_id"`
JobsUrl string `json:"jobs_url"`
Id int `json:"id"`
WorkflowId int `json:"workflow_id"`
JobsUrl string `json:"jobs_url"`
Id int `json:"id"`
Event string `json:"event"`
DisplayTitle string `json:"display_title"`
HeadBranch string `json:"head_branch"`
HtmlUrl string `json:"html_url"`
Name string `json:"name"`
Path string `json:"path"`
Status string `json:"status"`
}

type WorkflowRuns struct {
Expand All @@ -22,6 +30,7 @@ type WorkflowRuns struct {

type Job struct {
Id int `json:"id"`
Name string `json:"name"`
CheckRunUrl string `json:"check_run_url"`
Conclusion string `json:"conclusion"`
Status string `json:"status"`
Expand All @@ -33,7 +42,7 @@ type WorkflowJobs struct {
Jobs []Job `json:"jobs"`
}

type Annotations []struct {
type Annotation struct {
Path string `json:"path"`
BlobHref string `json:"blob_href"`
Title string `json:"title"`
Expand All @@ -46,6 +55,18 @@ type Annotations []struct {
EndColumn int `json:"end_column"`
}

type Record struct {
Repository string `json:"repository"`
WorkflowName string `json:"workflow_name"`
WorkflowEvent string `json:"workflow_event"`
WorkflowPath string `json:"workflow_path"`
WorkflowUrl string `json:"workflow_url"`
JobName string `json:"job_name"`
JobConclusion string `json:"job_conclusion"`
AnnotationLevel string `json:"annotation_level"`
Message string `json:"message"`
}

func latest(workflowRuns WorkflowRuns) []Run {
var latestRuns []Run
for _, run := range workflowRuns.Runs {
Expand Down Expand Up @@ -94,20 +115,36 @@ func getJobs(client api.RESTClient, repository repository.Repository, run Run) W
return jobs
}

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

jsonStr, _ := json.Marshal(res)
var annotations Annotations
var annotations []Annotation
if err := json.Unmarshal([]byte(jsonStr), &annotations); err != nil {
panic(err)
}

return annotations
}

func toRecord(repository repository.Repository, run Run, job Job, annotation Annotation) Record {
r := Record{
Repository: repository.Owner() + "/" + repository.Name(),
WorkflowName: run.Name,
WorkflowEvent: run.Event,
WorkflowPath: run.Path,
WorkflowUrl: run.HtmlUrl,
JobName: job.Name,
JobConclusion: job.Conclusion,
AnnotationLevel: annotation.AnnotationLevel,
Message: annotation.Message,
}

return r
}

func main() {
client, err := gh.RESTClient(nil)
if err != nil {
Expand All @@ -116,19 +153,31 @@ func main() {
}
currentRepository, _ := gh.CurrentRepository()

//fmt.Printf("%+v\n", workflowRunsRes)

workflowRuns := getRuns(client, currentRepository)
latestRuns := latest(workflowRuns)
fmt.Printf("%+v\n", latestRuns)

var summary []Record

//fmt.Printf("Repository: %s/%s\n", currentRepository.Owner(), currentRepository.Name())
for _, run := range latestRuns {
jobs := getJobs(client, currentRepository, run)
//fmt.Printf("Workflow(%s): %s(%s)\n", run.Event, run.Name, run.Path)

jobs := getJobs(client, currentRepository, run)
for _, job := range jobs.Jobs {
//fmt.Printf("\tJob name: %s, %s\n", job.Name, job.Conclusion)
annotations := getAnnotations(client, currentRepository, job)
fmt.Print("\n===========================================\n")
fmt.Printf("%+v\n", annotations)
for _, annotation := range annotations {
//fmt.Printf("\t\t%s: %s\n", annotation.AnnotationLevel, annotation.Message)
r := toRecord(currentRepository, run, job, annotation)
summary = append(summary, r)
}
}
}

j, err := json.Marshal(summary)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%s", string(j))
}

0 comments on commit aafb399

Please sign in to comment.