Skip to content

Commit

Permalink
Merge pull request #39 from tomtwinkle/feat/auto-detect-github-reposi…
Browse files Browse the repository at this point in the history
…tory

feat: auto detect github repository from CI
  • Loading branch information
tomtwinkle authored Aug 15, 2023
2 parents 315a72e + 4d51999 commit a54f876
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
6 changes: 1 addition & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ func MakePR(arg *Args) error {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}))
g, err := gh.New(ctx, arg.Token, gh.RemoteConfigParam{
GitDirPath: gitDir,
RemoteName: gitRemoteName,
Logger: logger,
})
g, err := gh.New(ctx, arg.Token, logger)
if err != nil {
return err
}
Expand Down
98 changes: 72 additions & 26 deletions internal/pkg/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -42,12 +43,27 @@ type gh struct {
logger *slog.Logger
}

func New(ctx context.Context, token string, p RemoteConfigParam) (Github, error) {
cnf, err := gitRemoteConfig(p)
type RemoteConfig struct {
Owner string
Repo string
Logger *slog.Logger
}

type gitConfig struct {
Owner string
Repo string
}

func New(ctx context.Context, token string, logger *slog.Logger) (Github, error) {
cnf, err := gitRemoteConfig(logger)
if err != nil {
return nil, err
}
return NewWithConfig(ctx, token, *cnf)
return NewWithConfig(ctx, token, RemoteConfig{
Owner: cnf.Owner,
Repo: cnf.Repo,
Logger: logger,
})
}

func NewWithConfig(ctx context.Context, token string, remoteConfig RemoteConfig) (Github, error) {
Expand All @@ -74,33 +90,35 @@ func NewWithConfig(ctx context.Context, token string, remoteConfig RemoteConfig)
}, nil
}

func newClient(ctx context.Context, token string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

return github.NewClient(tc)
}

type RemoteConfigParam struct {
GitDirPath string
RemoteName string
Logger *slog.Logger
}

type RemoteConfig struct {
Owner string
Repo string
Logger *slog.Logger
func gitRemoteConfig(logger *slog.Logger) (*gitConfig, error) {
cnf, err := gitRemoteConfigDir()
if err != nil {
logger.Info(err.Error())
} else {
return cnf, nil
}
cnf, err = gitRemoteConfigCIEnv()
if err != nil {
logger.Info(err.Error())
} else {
return cnf, nil
}
return nil, errors.New("doesn't detect github repository")
}

func gitRemoteConfig(p RemoteConfigParam) (*RemoteConfig, error) {
r, err := git.PlainOpen(p.GitDirPath)
func gitRemoteConfigDir() (*gitConfig, error) {
const (
gitDir = ".git"
remoteName = "origin"
)
if f, err := os.Stat(gitDir); os.IsNotExist(err) || !f.IsDir() {
return nil, fmt.Errorf("not found %s", gitDir)
}
r, err := git.PlainOpen(gitDir)
if err != nil {
return nil, err
}
remote, err := r.Remote(p.RemoteName)
remote, err := r.Remote(remoteName)
if err != nil {
return nil, err
}
Expand All @@ -114,7 +132,35 @@ func gitRemoteConfig(p RemoteConfigParam) (*RemoteConfig, error) {
ss := strings.Split(url, "/")
owner := ss[0]
repo := ss[1]
return &RemoteConfig{Owner: owner, Repo: repo, Logger: p.Logger}, nil
return &gitConfig{Owner: owner, Repo: repo}, nil
}

func gitRemoteConfigCIEnv() (*gitConfig, error) {
if _, ok := os.LookupEnv("CI"); !ok {
return nil, errors.New("not working CI")
}

if repo, ok := os.LookupEnv("GITHUB_REPOSITORY"); ok {
ss := strings.Split(repo, "/")
return &gitConfig{Owner: ss[0], Repo: ss[1]}, nil
}

if repo, ok := os.LookupEnv("CIRCLE_PROJECT_REPONAME"); ok {
if owner, ok := os.LookupEnv("CIRCLE_PROJECT_USERNAME"); ok {
return &gitConfig{Owner: owner, Repo: repo}, nil
}
}

return nil, errors.New("not found CI env")
}

func newClient(ctx context.Context, token string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

return github.NewClient(tc)
}

type PullRequests []*github.PullRequest
Expand Down

0 comments on commit a54f876

Please sign in to comment.