Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: Added branch column and sort by branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Syycorax committed Nov 15, 2022
1 parent bf8135a commit 05d8429
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions gofork.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ type Owner struct {
Login string `json:"login"`
}

type Branches struct {
Name string `json:"name"`
}

type Fork struct {
FullName string `json:"full_name"`
Url string `json:"html_url"`
Status string `json:"status"`
AheadBy int `json:"ahead_by"`
BehindBy int `json:"behind_by"`
Stars int `json:"stargazers_count"`
LastUpdated string `json:"pushed_at"`
FullName string `json:"full_name"`
Url string `json:"html_url"`
Status string `json:"status"`
AheadBy int `json:"ahead_by"`
BehindBy int `json:"behind_by"`
Stars int `json:"stargazers_count"`
LastUpdated string `json:"pushed_at"`
branchNumber int
}

type Auth struct {
Expand All @@ -43,8 +48,9 @@ type Auth struct {

func main() {
var (
forks []Fork
auth Auth
forks []Fork
auth Auth
branches []Branches
)
fail := "[X] "
success := "[✓] "
Expand All @@ -56,7 +62,7 @@ func main() {
branch := parser.String("b", "branch", &argparse.Options{Required: false, Help: "Branch to check", Default: "repo default branch"})
verboseFlag := parser.Flag("v", "verbose", &argparse.Options{Help: "Show deleted and up to date repositories"})
pageInt := parser.Int("p", "page", &argparse.Options{Help: "Page to check (use -1 for all)", Default: 1, Required: false})
sortBy := parser.String("s", "sort", &argparse.Options{Help: "Sort by (stars, ahead, lastUpdated)", Default: "ahead", Required: false})
sortBy := parser.String("s", "sort", &argparse.Options{Help: "Sort by (stars, ahead, lastUpdated, branches)", Default: "ahead", Required: false})
deleteconfigflag := parser.Flag("d", "deleteconfig", &argparse.Options{Help: "Delete the config file"})
err := parser.Parse(os.Args)
if err != nil {
Expand Down Expand Up @@ -157,7 +163,7 @@ func main() {
*pageInt = 1
}

if *sortBy != "stars" && *sortBy != "ahead" && *sortBy != "lastUpdated" {
if *sortBy != "stars" && *sortBy != "ahead" && *sortBy != "lastUpdated" && *sortBy != "branches" {
platformPrint(color.Warn, warning+"The sort option is not valid, sortingHelper by ahead")
*sortBy = "ahead"
}
Expand All @@ -172,13 +178,23 @@ func main() {
resp, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &forks)

for _, fork := range forks {
url = "https://api.github.com/repos/" + fork.FullName + "/compare/" + RepoInfo.Owner.Login + ":" + *branch + "..." + *branch
req, _ = http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "token "+string(auth.Token))
resp, _ = http.DefaultClient.Do(req)
body, _ = ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &fork)
url = "https://api.github.com/repos/" + fork.FullName + "/branches"
req, _ = http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "token "+string(auth.Token))
resp, _ = http.DefaultClient.Do(req)
body, _ = ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &branches)
for range branches {
fork.branchNumber ++
}
if fork.Status == "ahead" {
ahead.PushBack(fork)
} else if fork.Status == "behind" {
Expand All @@ -197,14 +213,15 @@ func main() {
platformPrint(color.Success, success+"sorting by "+*sortBy)
aheadTable := table.NewWriter()
aheadTable.SetOutputMirror(os.Stdout)
aheadTable.AppendHeader(table.Row{"Fork", "Ahead by", "URL", "Stars", "Last updated"})
aheadTable.AppendHeader(table.Row{"Fork", "Ahead by", "URL", "Stars","Branches","Last updated"})
for e := ahead.Front(); e != nil; e = e.Next() {
fork := e.Value.(Fork)
aheadBy := strconv.Itoa(fork.AheadBy)
url := "https://github.com/" + string(fork.FullName)
stars := strconv.Itoa(fork.Stars) + " \033[33m\u2605" + "\033[0m"
branches := strconv.Itoa(fork.branchNumber)
LastUpdated := dateHandler(fork.LastUpdated)
aheadTable.AppendRow([]interface{}{fork.FullName, aheadBy, url, stars, LastUpdated})
aheadTable.AppendRow([]interface{}{fork.FullName, aheadBy, url, stars, branches,LastUpdated})
}
sortingHelper(aheadTable, sortBy)
if ahead.Len() > 0 {
Expand All @@ -216,15 +233,16 @@ func main() {
}
divergeTable := table.NewWriter()
divergeTable.SetOutputMirror(os.Stdout)
divergeTable.AppendHeader(table.Row{"Fork", "Ahead by", "Behind by", "URL", "Stars", "Last Updated"})
divergeTable.AppendHeader(table.Row{"Fork", "Ahead by", "Behind by", "URL", "Stars","Branches", "Last Updated"})
for e := diverge.Front(); e != nil; e = e.Next() {
fork := e.Value.(Fork)
aheadBy := strconv.Itoa(fork.AheadBy)
behindBy := strconv.Itoa(fork.BehindBy)
url := "https://github.com/" + string(fork.FullName)
stars := strconv.Itoa(fork.Stars) + " \033[33m\u2605" + "\033[0m"
branches := strconv.Itoa(fork.branchNumber)
lastCommit := dateHandler(fork.LastUpdated)
divergeTable.AppendRow([]interface{}{fork.FullName, aheadBy, behindBy, url, stars, lastCommit})
divergeTable.AppendRow([]interface{}{fork.FullName, aheadBy, behindBy, url, stars, branches, lastCommit})
}
sortingHelper(divergeTable, sortBy)
if diverge.Len() > 0 {
Expand All @@ -237,14 +255,15 @@ func main() {

behindTable := table.NewWriter()
behindTable.SetOutputMirror(os.Stdout)
behindTable.AppendHeader(table.Row{"Fork", "Behind by", "URL", "Stars", "Last updated"})
behindTable.AppendHeader(table.Row{"Fork", "Behind by", "URL", "Stars", "Branches","Last updated"})
for e := behind.Front(); e != nil; e = e.Next() {
fork := e.Value.(Fork)
behindBy := strconv.Itoa(fork.BehindBy)
url := "https://github.com/" + string(fork.FullName)
stars := strconv.Itoa(fork.Stars) + " \033[33m\u2605" + "\033[0m"
branches := strconv.Itoa(fork.branchNumber)
LastUpdated := dateHandler(fork.LastUpdated)
behindTable.AppendRow([]interface{}{fork.FullName, behindBy, url, stars, LastUpdated})
behindTable.AppendRow([]interface{}{fork.FullName, behindBy, url, stars,branches, LastUpdated})
}
if *sortBy == "ahead" { // if sorting by ahead, the behind table needs to be sorted by behind by
*sortBy = "behind"
Expand All @@ -263,13 +282,14 @@ func main() {

if *verboseFlag {
evenTable := table.NewWriter()
evenTable.AppendHeader(table.Row{"Fork", "URL", "Stars"})
evenTable.AppendHeader(table.Row{"Fork", "URL", "Stars","Branches","Last updated"})
for e := even.Front(); e != nil; e = e.Next() {
fork := e.Value.(Fork)
url := "https://github.com" + string(fork.FullName)
stars := strconv.Itoa(fork.Stars) + " \033[33m\u2605" + "\033[0m"
branches := strconv.Itoa(fork.branchNumber)
fork.LastUpdated = dateHandler(fork.LastUpdated)
evenTable.AppendRow([]interface{}{fork.FullName, url, stars, fork.LastUpdated})
evenTable.AppendRow([]interface{}{fork.FullName, url, stars,branches, fork.LastUpdated})
}
if *sortBy == "stars" || *sortBy == "lastUpdated" {
sortingHelper(evenTable, sortBy)
Expand All @@ -288,11 +308,14 @@ func main() {

deletedTable := table.NewWriter()
deletedTable.SetOutputMirror(os.Stdout)
deletedTable.AppendHeader(table.Row{"Fork", "URL"})
deletedTable.AppendHeader(table.Row{"Fork", "URL","Stars","Branches","Last updated"})
for e := deleted.Front(); e != nil; e = e.Next() {
fork := e.Value.(Fork)
url := "https://github.com" + string(fork.FullName)
deletedTable.AppendRow([]interface{}{fork.FullName, url})
stars := strconv.Itoa(fork.Stars) + " \033[33m\u2605" + "\033[0m"
branches := strconv.Itoa(fork.branchNumber)
fork.LastUpdated = dateHandler(fork.LastUpdated)
deletedTable.AppendRow([]interface{}{fork.FullName, url, stars, branches, fork.LastUpdated})
}
if deleted.Len() > 0 {
platformPrint(color.Question, mitigate+"deleted forks: "+strconv.Itoa(deleted.Len()))
Expand Down Expand Up @@ -434,6 +457,10 @@ func sortingHelper(mytable table.Writer, sortBy *string) table.Writer {
mytable.SortBy([]table.SortBy{
{Name: "Behind by", Mode: table.AscNumeric},
})
} else if *sortBy == "branches" {
mytable.SortBy([]table.SortBy{
{Name: "Branches", Mode: table.DscNumeric},
})
}
return mytable
}
Expand Down

0 comments on commit 05d8429

Please sign in to comment.