prのタイトルを取得できるcliをgolangで作った
成果
こんな風に使う
function rep(){ if [[ $@ = "d" ]]; then base="develop" elif [[ $@ = "m" ]]; then base="master" else base=$@ fi org=$(basename $(dirname "$PWD")) branch=$(currentBranch) repo=$(basename "$PWD") go-get-repo-info -status open -org $org -branch $branch -base $base -repo $repo }
\nを除去する
\nが入ってきたので除去
var regxNewline = regexp.MustCompile(`\r\n|\r|\n`) //throw panic if fail func convNewline(str, nlcode string) string { return regxNewline.Copy().ReplaceAllString(str, nlcode) }
file読み込み
func useIoutilReadFile(fileName string) string { bytes, err := ioutil.ReadFile(fileName) if err != nil { panic(err) } return string(bytes) }
home directore 取得(~/)
usr, _ := user.Current() f := strings.Replace("~/go-get-repo-info-access-token", "~", usr.HomeDir, 1) accessToken := useIoutilReadFile(f) regexedAccessToken := convNewline(accessToken, "") if accessToken == "" { panic("access-token is blank. create go-get-repo-info/access-token. and write access-token. not \\n") }
go-github
var ( status = flag.String("status", "open", "open|close|all" ) organization = flag.String("org", "kajirikajiri", "ex)kajirikajiri" ) branch = flag.String("branch", "", "ex}feature/issue-700" ) base = flag.String("base", "develop", "ex}develop|master|release" ) sort = flag.String("sort", "created", "created|updated|popularity|long-running" ) direction = flag.String("direction", "desc", "asc|desc" ) repo = flag.String("repo", "go-get-repo-info", "repository name" ) ) flag.Parse() ctx := context.Background() ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: regexedAccessToken}, ) tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) opts := &github.PullRequestListOptions{*status, *organization + ":" + *branch, *base, *sort, *direction, github.ListOptions{Page: 1}} pulls, _, err := client.PullRequests.List(ctx, *organization, *repo, opts) if err != nil { fmt.Print(err) } for _ ,pull := range pulls { fmt.Print(*pull.Title) }
import でつまる
v31の方でimportしたら使えなかった
import "github.com/google/go-github/v31/github" // with go modules enabled (GO111MODULE=on or outside GOPATH) import "github.com/google/go-github/github" // with go modules disabled
pointerの扱いにつまる
fmt.Print(*pull.Title)
*pull.Titleが なかなかわからなかった 公式サイトで確認した
自分で型を調べてなんとかしようとした y0m0r.hateblo.jp