-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (73 loc) · 1.88 KB
/
main.go
File metadata and controls
85 lines (73 loc) · 1.88 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
type Event struct {
Type string `json:"type"`
Repo struct {
Name string `json:"name"`
} `json:"repo"`
Payload struct {
Action string `json:"action"`
Commits []struct{} `json:"commits"`
RefType string `json:"ref_type"`
} `json:"payload"`
}
func fetchGithubEvents(username string) ([]Event, error) {
url := fmt.Sprintf("https://api.github.com/users/%s/events", username)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if len(body) == 0 {
return nil, fmt.Errorf("no recent activity found")
}
var events []Event
if err = json.Unmarshal(body, &events); err != nil {
return nil, err
}
return events, nil
}
func formatEvent(event Event) string {
switch event.Type {
case "PushEvent":
return fmt.Sprintf("Pushed %d commit(s) to %s", len(event.Payload.Commits), event.Repo.Name)
case "IssuesEvent":
return fmt.Sprintf("%s an issue in %s", strings.ToUpper(string(event.Payload.Action[0]))+event.Payload.Action[1:], event.Repo.Name)
case "WatchEvent":
return fmt.Sprintf("Starred %s", event.Repo.Name)
case "ForkEvent":
return fmt.Sprintf("Forked %s", event.Repo.Name)
case "CreateEvent":
return fmt.Sprintf("Created %s in %s", event.Payload.RefType, event.Repo.Name)
default:
return fmt.Sprintf("%s in %s", strings.TrimSuffix(event.Type, "Event"), event.Repo.Name)
}
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Usage: go run main.go <github-username>")
}
username := os.Args[1]
events, err := fetchGithubEvents(username)
if err != nil {
log.Fatal(err)
}
for _, event := range events {
fmt.Println("-", formatEvent(event))
}
}