Last active
December 8, 2023 10:50
-
-
Save Guaderxx/2983220db402f22308e367a60e4b3d48 to your computer and use it in GitHub Desktop.
github api cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/url" | |
"os" | |
"strings" | |
"time" | |
"github.com/go-resty/resty/v2" | |
) | |
const IssuesURL = "https://api.github.com/search/issues" | |
type IssuesSearchResult struct { | |
TotaoCount int `json:"total_count"` | |
Items []*Issue | |
} | |
type Issue struct { | |
Number int | |
HTMLURL string `json:"html_url"` | |
Title string | |
State string | |
User *User | |
CreatedAt time.Time `json:"created_at"` | |
Body string | |
} | |
type User struct { | |
Login string | |
HTMLURL string `json:"html_url""` | |
} | |
func SearchIssues(terms []string) IssuesSearchResult { | |
q := url.QueryEscape(strings.Join(terms, " ")) | |
c := resty.New() | |
c.SetTimeout(10 * time.Second) | |
c.SetRetryCount(3) | |
c.SetQueryParams(map[string]string{ | |
"q": q, | |
}) | |
res, err := c.R().Get(IssuesURL) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var sres IssuesSearchResult | |
if err := json.Unmarshal(res.Body(), &sres); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%d issues:\n", sres.TotaoCount) | |
for _, item := range sres.Items { | |
fmt.Printf("#%-5d %9.9s %.55s\n", | |
item.Number, item.User.Login, item.Title) | |
} | |
return sres | |
} | |
func main() { | |
SearchIssues(os.Args[1:]) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"time" | |
"github.com/go-resty/resty/v2" | |
) | |
const UserUrl = "https://api.github.com/user" | |
type User struct { | |
Login string `json:"login"` | |
ID int64 `json:"id"` | |
NodeID string `json:"node_id"` | |
AvatarURL string `json:"avatar_url"` | |
Url string `json:"url"` | |
HtmlUrl string `json:"html_url"` | |
FollowersUrl string `json:"followers_url"` | |
FollowingUrl string `json:"following_url"` | |
GistsUrl string `json:"gists_url"` | |
StarredUrl string `json:"starred_url"` | |
SubscriptionsUrl string `json:"subscriptions_url"` | |
OrganizationsUrl string `json:"organizations_url"` | |
ReposUrl string `json:"repos_url"` | |
EventsUrl string `json:"events_url"` | |
ReceivedEventsUrl string `json:"received_events_url"` | |
Type string `json:"type"` | |
SiteAdmin bool `json:"site_admin"` | |
Name string `json:"name"` | |
Company string `json:"company"` | |
Blog string `json:"blog"` | |
Location string `json:"location"` | |
Email string `json:"email"` | |
Hireable bool `json:"hireable"` | |
Bio string `json:"bio"` | |
TwitterUsername string `json:"twitter_username"` | |
PublicRepos int `json:"public_repos"` | |
PublicGists int `json:"public_gists"` | |
Followers int `json:"followers"` | |
Following int `json:"following"` | |
CreatedAt time.Time `json:"created_at"` | |
UpdatedAt time.Time `json:"updated_at"` | |
PrivateGists int `json:"private_gists"` | |
TotalPrivateRepos int `json:"total_private_repos"` | |
OwnedPrivateRepos int `json:"owned_private_repos"` | |
DiskUsage int `json:"disk_usage"` | |
Collaborators int `json:"collaborators"` | |
TwoFactorAuthentication bool `json:"two_factor_authentication"` | |
Plan UserPlan `json:"plan"` | |
} | |
func (u User) String() string { | |
str := strings.Builder{} | |
str.WriteString("LoginName\t") | |
str.WriteString(u.Login + "\n") | |
str.WriteString("NickName\t") | |
str.WriteString(u.Name + "\n") | |
str.WriteString("Location\t") | |
str.WriteString(u.Location + "\n") | |
str.WriteString("BIO\t") | |
str.WriteString(u.Bio + "\n") | |
return str.String() | |
} | |
type UserPlan struct { | |
Name string `json:"name"` | |
Space int64 `json:"space"` | |
Collaborators int `json:"collaborators"` | |
PrivateRepos int `json:"private_repos"` | |
} | |
func GetUser() User { | |
val := os.Getenv("GITHUB_TOKEN") | |
c := resty.New() | |
c.SetTimeout(10 * time.Second) | |
c.SetRetryCount(3) | |
c.SetHeaders(map[string]string{ | |
"Accept": "application/vnd.github+json", | |
"Authorization": fmt.Sprintf("Bearer %s", val), | |
"X-GitHub-Api-Version": "2022-11-28", | |
}) | |
res, err := c.R().Get(UserUrl) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var suser User | |
if err := json.Unmarshal(res.Body(), &suser); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("user: ", suser) | |
return suser | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment