mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 20:55:47 +00:00
refactor(gh_fetcher): integreate the gh-cli using its api access to authenticate
Helps reduce rate limits and in some cases automates email fetching if the user has one public on their account
This commit is contained in:
@@ -4,32 +4,88 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GithubProfile struct {
|
type GithubProfile struct {
|
||||||
Login string `json:"login"`
|
Login string `json:"login"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkGHCLI() bool {
|
||||||
|
// Check if the gh command line tool is installed
|
||||||
|
cmd := exec.Command("gh", "auth", "status")
|
||||||
|
out ,err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
if strings.Contains(string(out), "Logged in to") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func useGHCLI(username string) []byte {
|
||||||
|
cmd := exec.Command("gh", "api", fmt.Sprintf("/users/%s", username))
|
||||||
|
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprint("Error fetching github profile",err))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchGithubProfile(username string) User {
|
func FetchGithubProfile(username string) User {
|
||||||
// Fetch the github profile and create a user with everything except the email
|
// Fetch the github profile and create a user with everything except the email
|
||||||
|
|
||||||
url := fmt.Sprintf("https://api.github.com/users/%s", username)
|
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprint("Error fetching github profile: ", err))
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// Parse the response and create a user
|
|
||||||
var profile GithubProfile
|
var profile GithubProfile
|
||||||
err = json.NewDecoder(resp.Body).Decode(&profile)
|
|
||||||
if err != nil {
|
check := checkGHCLI()
|
||||||
panic(fmt.Sprint("Error parsing github profile: ", err))
|
|
||||||
|
if check {
|
||||||
|
// If the gh command line tool is installed, use it to fetch the github profile
|
||||||
|
fmt.Println("Using gh-cli to fetch github profile")
|
||||||
|
data := useGHCLI(username)
|
||||||
|
|
||||||
|
err := json.Unmarshal(data, &profile)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprint("Error parsing github profile: ", err))
|
||||||
|
}
|
||||||
|
if profile.Name == "" {
|
||||||
|
panic(fmt.Sprint("Error: No name found in github profile something went wrong whilst fetching using gh-cli \n(output from cmd:)", string(data)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
fmt.Println("Using http request to fetch github profile")
|
||||||
|
// If the gh command line tool is not installed, use the http request
|
||||||
|
url := fmt.Sprintf("https://api.github.com/users/%s", username)
|
||||||
|
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprint("Error fetching github profile: ", err))
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Parse the response and create a user
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&profile)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprint("Error parsing github profile: ", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if profile.Name == "" {
|
||||||
|
panic(fmt.Sprintf("Error: No name found in github profile something went wrong whilst fetching \n(http response): %s", resp.Status))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Create a user with the github profile
|
// Create a user with the github profile
|
||||||
return User{
|
return User{
|
||||||
Shortname: strings.ToLower(profile.Name[:2]),
|
Shortname: strings.ToLower(profile.Name[:2]),
|
||||||
|
|||||||
Reference in New Issue
Block a user