This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
153 lines (134 loc) · 3.4 KB
/
api.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/spf13/cobra"
)
type Actor struct {
Login string `json:"login"`
DisplayLogin string `json:"display_login"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url`
AvatarUrl string `json:"avatar_url"`
}
type Repo struct {
Id int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
}
type Author struct {
Email string `json:"email"`
Name string `json:"name"`
}
type Commits struct {
Sha string `json:"sha"`
Author Author `json:"author"`
Message string `json:"message"`
Distinct bool `json:"distinct"`
Url string `json:"url"`
}
type Payload struct {
PushId int `json:"push_id"`
Size int `json:"size"`
DistinctSize int `json:"distinct_size"`
Ref string `json:"ref"`
Head string `json:"head"`
Before string `json:"before"`
Commits []Commits
}
type Org struct {
Id int `json:"id"`
Login string `json:"login"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url"`
AvatarUrl string `json:"avatar_url"`
}
//Eventcoding json
type Event struct {
Id string `json:"id"`
Type string `json:"type"`
Actor Actor `json:"actor"`
Repo Repo `json:"repo"`
Payload Payload `json:"payload"`
Public bool `json:"public"`
CreatedAt string `json:"created_at"`
Organization Org `json:"org"`
}
type Activity struct {
Id string `json:"id"`
Person string `json:"person"`
Repo string `json:"repo"`
CreatedAt string `json:"created_at"`
}
var (
org string
outputfile string
Cmd = &cobra.Command{
Use: "gh-activity --org=[organization]",
Short: "Get Github activity for a given organization.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if org == "" {
fmt.Println("organization is not set")
cmd.Help()
return
}
e, err := getEvents(org)
if err != nil {
fmt.Printf("problem getting events: %v", err)
return
}
err = writeActivity(outputfile, e)
if err != nil {
fmt.Printf("problem writing the activities to file: %v", err)
return
}
},
}
)
func init() {
Cmd.PersistentFlags().StringVar(&org, "org", "", "Github organization")
Cmd.PersistentFlags().StringVar(&outputfile, "out", "activity.json", "file to save activities")
}
func main() {
if err := Cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func getEvents(org string) ([]Event, error) {
// get the data
resp, err := http.Get(fmt.Sprintf("https://api.github.com/users/%s/events", org))
if err != nil {
return nil, fmt.Errorf("error getting the data from github: %v", err)
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
var e []Event
//saving the data to the new format
if err := json.Unmarshal(buf.Bytes(), &e); err != nil {
return nil, fmt.Errorf("problem unmarshalling data: %v", err)
}
return e, nil
}
func writeActivity(outfile string, e []Event) error {
var activities []Activity
for _, a := range e {
act := Activity{
Id: a.Id,
Person: a.Actor.Login,
Repo: a.Repo.Name,
CreatedAt: a.CreatedAt,
}
activities = append(activities, act)
}
b, err := json.Marshal(activities)
if err = ioutil.WriteFile(outfile, b, 0666); err != nil {
return fmt.Errorf("failed to write the file in json format: %v", err)
}
return nil
}