-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (131 loc) · 3.55 KB
/
main.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
154
155
156
157
158
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
const githubAPIURL = "https://api.github.com"
type Repository struct {
Name string `json:"name"`
}
type Commit struct {
Sha string `json:"sha"`
}
type Event struct {
Type string `json:"type"`
Payload struct {
Commits []Commit `json:"commits"`
} `json:"payload"`
}
func getUserInput(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}
func fetchRepos(username string, token string) []Repository {
url := fmt.Sprintf("%s/users/%s/repos", githubAPIURL, username)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "token "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error fetching repositories:", err)
return nil
}
defer resp.Body.Close()
var repos []Repository
json.NewDecoder(resp.Body).Decode(&repos)
return repos
}
func fetchCommits(owner, repo, token string) []Commit {
url := fmt.Sprintf("%s/repos/%s/%s/commits", githubAPIURL, owner, repo)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "token "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error fetching commits:", err)
return nil
}
defer resp.Body.Close()
var commits []Commit
json.NewDecoder(resp.Body).Decode(&commits)
return commits
}
func fetchEvents(owner, repo, token string) []Event {
url := fmt.Sprintf("%s/repos/%s/%s/events", githubAPIURL, owner, repo)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "token "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error fetching events:", err)
return nil
}
defer resp.Body.Close()
var events []Event
json.NewDecoder(resp.Body).Decode(&events)
return events
}
func findDeletedCommits(commits []Commit, events []Event) []string {
currentCommitShas := map[string]bool{}
for _, commit := range commits {
currentCommitShas[commit.Sha] = true
}
var deletedCommits []string
for _, event := range events {
if event.Type == "PushEvent" {
for _, commit := range event.Payload.Commits {
if !currentCommitShas[commit.Sha] {
deletedCommits = append(deletedCommits, commit.Sha)
}
}
}
}
return deletedCommits
}
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("Please set your GitHub token in the GITHUB_TOKEN environment variable.")
return
}
username := getUserInput("Enter the GitHub username: ")
repos := fetchRepos(username, token)
if len(repos) == 0 {
fmt.Println("No repositories found or error fetching repositories.")
return
}
fmt.Println("Repositories found:")
for i, repo := range repos {
fmt.Printf("[%d] %s\n", i+1, repo.Name)
}
repoIndexInput := getUserInput("Choose a repository (enter the number): ")
repoIndex := 0
fmt.Sscanf(repoIndexInput, "%d", &repoIndex)
if repoIndex < 1 || repoIndex > len(repos) {
fmt.Println("Invalid selection.")
return
}
selectedRepo := repos[repoIndex-1].Name
commits := fetchCommits(username, selectedRepo, token)
events := fetchEvents(username, selectedRepo, token)
deletedCommits := findDeletedCommits(commits, events)
if len(deletedCommits) > 0 {
fmt.Println("Deleted commits found:")
for _, sha := range deletedCommits {
fmt.Println(sha)
}
} else {
fmt.Println("No deleted commits found.")
}
}