ghutil

package module
v0.0.0-...-24172c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

README

用 Go 获取 Github 常用信息

hello.go 例子:

package main

import (
	"context"
	"flag"
	"fmt"
	"os"

	"github.com/chai2010/ghutil"
)

var (
	flagUserName = flag.String("u", "chai2010", "set user name")
)

func main() {
	flag.Parse()

	c := ghutil.NewGHClient(&ghutil.GHOptions{
		Token: os.Getenv("GITHUB_ACCESS_TOKEN"),
	})
	repos, err := c.GetRepositories(context.Background(), *flagUserName)
	if err != nil {
		panic(err)
	}

	var starCount int
	var forkCount int
	for _, repo := range repos {
		starCount += *repo.StargazersCount
		forkCount += *repo.ForksCount
	}

	fmt.Printf("%s/{*}:StargazersCount: %d\n", *flagUserName, starCount)
	fmt.Printf("%s/{*}:ForksCount:      %d\n", *flagUserName, forkCount)
}

设置好 GITHUB_ACCESS_TOKEN 环境变量, 然后执行:

$ go run hello.go -u=chai2010
chai2010/{*}:StargazersCount: 25283
chai2010/{*}:ForksCount:      3978

$ go run hello.go -u=KusionStack
KusionStack/{*}:StargazersCount: 1206
KusionStack/{*}:ForksCount:      200

Documentation

Overview

Github 辅助函数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GHClient

type GHClient struct {
	// contains filtered or unexported fields
}

Github 客户端

func NewGHClient

func NewGHClient(opt *GHOptions) *GHClient

func (*GHClient) GetRepoInfo

func (p *GHClient) GetRepoInfo(ctx context.Context, userName, repoName string) (repo *github.Repository, err error)

获取仓库信息

func (*GHClient) GetRepoLanguages

func (p *GHClient) GetRepoLanguages(repos []*github.Repository, max int) []string

获取仓库编程语言类型

func (*GHClient) GetRepoStargazers

func (p *GHClient) GetRepoStargazers(ctx context.Context, userName, repoName string) (allStargazers []*github.Stargazer, err error)

获取仓库的关注信息

func (*GHClient) GetRepositories

func (p *GHClient) GetRepositories(ctx context.Context, userName string) (allRepos []*github.Repository, err error)

获取用户的仓库列表

func (*GHClient) GetStarredRepos

func (p *GHClient) GetStarredRepos(ctx context.Context, userName string) (allRepos []*github.StarredRepository, err error)

用户点赞仓库

func (*GHClient) GetUserInfo

func (p *GHClient) GetUserInfo(ctx context.Context, userName string) (user *github.User, err error)

获取用户信息

type GHOptions

type GHOptions struct {
	Token         string // GITHUB_ACCESS_TOKEN
	CacheFilename string // 缓存文件
}

链接选项

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL