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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"text/tabwriter"
"time"
"github.com/fcuny/world/internal/git"
"github.com/fcuny/world/internal/terminal"
"github.com/fcuny/world/internal/version"
)
const API_URL = "https://api.github.com"
const usage = `Usage:
gha-log
`
type githubActionRun struct {
Workflows []Workflow `json:"workflow_runs"`
}
type Workflow struct {
ID int `json:"id"`
Name string `json:"name"`
Title string `json:"display_title"`
Conclusion string `json:"conclusion"`
RunStartedAt time.Time `json:"run_started_at"`
}
type Unmarshaler interface {
UnmarshalJSON([]byte) error
}
func (w *Workflow) UnmarshalJSON(data []byte) error {
type Alias Workflow
aux := &struct {
RunStartedAt string `json:"run_started_at"`
*Alias
}{
Alias: (*Alias)(w),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
runStartedAt, err := time.Parse(time.RFC3339, aux.RunStartedAt)
if err != nil {
return err
}
w.RunStartedAt = runStartedAt
return nil
}
func main() {
flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) }
var (
tokenFlag string
userFlag string
versionFlag bool
)
flag.StringVar(&tokenFlag, "token", "", "GitHub API token")
flag.StringVar(&tokenFlag, "t", "", "GitHub API token")
flag.StringVar(&userFlag, "user", "fcuny", "GitHub API token")
flag.StringVar(&userFlag, "u", "fcuny", "GitHub API token")
flag.BoolVar(&versionFlag, "version", false, "Print version information")
flag.BoolVar(&versionFlag, "v", false, "Print version information")
if versionFlag {
information := version.VersionAndBuildInfo()
fmt.Println(information)
return
}
flag.Parse()
if tokenFlag == "" {
fmt.Fprintf(os.Stderr, "The API token is not set\n")
os.Exit(1)
}
ctx := context.TODO()
repositoryName, err := git.Root()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get the repository name: %v\n", err)
os.Exit(1)
}
url := fmt.Sprintf("%s/repos/%s/%s/actions/runs", API_URL, userFlag, repositoryName)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create a request: %v\n", err)
os.Exit(1)
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", tokenFlag))
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
client := http.Client{
Timeout: 30 * time.Second,
}
res, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "error making http request: %s\n", err)
os.Exit(1)
}
if res.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "unexpected status code: %d\n", res.StatusCode)
os.Exit(1)
}
var b githubActionRun
if err := json.NewDecoder(res.Body).Decode(&b); err != nil {
fmt.Fprintf(os.Stderr, "error parsing the JSON response: %v\n", err)
os.Exit(1)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
for _, run := range b.Workflows {
status := "✅"
if run.Conclusion != "success" {
status = "❌"
}
linkToAction := terminal.Link(strconv.Itoa(run.ID), fmt.Sprintf("http://github.com/%s/%s/actions/runs/%d/", userFlag, repositoryName, run.ID))
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", linkToAction, run.Name, run.RunStartedAt.Format("2006-01-02 15:04:05"), run.Title, status)
}
w.Flush()
}
|