Skip to content

Commit

Permalink
feat: working GitHub integration
Browse files Browse the repository at this point in the history
  • Loading branch information
wowu committed Jul 1, 2022
1 parent 861591e commit 64c9512
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 82 deletions.
100 changes: 100 additions & 0 deletions commands/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package commands

import (
"fmt"
"os"
"strings"
"syscall"
"wowu/pro/cfg"
"wowu/pro/github"
"wowu/pro/gitlab"

"github.com/fatih/color"
"golang.org/x/term"
)

func Auth(provider string) {
switch provider {
case "gitlab":
authgitlab()
case "github":
authgithub()
default:
fmt.Println("unknown provider")
os.Exit(1)
}
}

func authgitlab() {
fmt.Println("Generate your token at https://gitlab.com/-/profile/personal_access_tokens?name=PR+opener&scopes=api")
fmt.Println("The only required scope is 'api'")

// Ask for token
fmt.Print("Token: ")
byteToken, err := term.ReadPassword(int(syscall.Stdin))
fmt.Println()
handleError(err)

token := strings.TrimSpace(string(byteToken))

if token == "" {
color.Red("Token is empty. Try again")
os.Exit(1)
}

// Check if token is valid by fetching user info
_, err = gitlab.User(token)
if err != nil {
switch err {
case gitlab.ErrorUnauthorized:
color.Red("Token is invalid. Try again")
os.Exit(1)
default:
fmt.Println(err)
os.Exit(1)
}
}

config := cfg.Get()
config.GitlabToken = token
cfg.Save(config)

color.Green("Saved.")
}

func authgithub() {
fmt.Println("Generate personal access token at https://github.com/settings/tokens/new")
fmt.Println("The only required scope is 'repo'")

// ask for token
fmt.Print("Token: ")
byteToken, err := term.ReadPassword(int(syscall.Stdin))
fmt.Println()
handleError(err)

token := strings.TrimSpace(string(byteToken))

if token == "" {
color.Red("Token is empty. Try again")
os.Exit(1)
}

// Check if token is valid by fetching user info
_, err = github.User(token)
if err != nil {
switch err {
case github.ErrorUnauthorized:
color.Red("Token is invalid. Try again")
os.Exit(1)
default:
fmt.Println(err)
os.Exit(1)
}
}

config := cfg.Get()
config.GitHubToken = token
cfg.Save(config)

color.Green("Saved.")
}
118 changes: 44 additions & 74 deletions commands.go → commands/open.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,22 @@
package main
package commands

import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"wowu/pro/cfg"
"wowu/pro/github"
"wowu/pro/gitlab"

"github.com/fatih/color"
"github.com/go-git/go-git/v5"
giturls "github.com/whilp/git-urls"
"golang.org/x/exp/slices"
"golang.org/x/term"
)

func auth(provider string) {
fmt.Println("Generate your token at https://gitlab.com/-/profile/personal_access_tokens?name=PR+opener&scopes=api")
fmt.Println("The only required scope is 'api'")

// ask for token
fmt.Print("Token: ")
byteToken, err := term.ReadPassword(int(syscall.Stdin))
fmt.Println()
handleError(err)

token := strings.TrimSpace(string(byteToken))

if token == "" {
color.Red("Token is empty. Try again")
os.Exit(1)
}

// Check if token is valid by fetching user info
_, err = gitlab.User(token)
if err != nil {
switch err {
case gitlab.ErrorUnauthorized:
color.Red("Token is invalid. Try again")
os.Exit(1)
default:
fmt.Println(err)
os.Exit(1)
}
}

config := cfg.Get()
config.GitlabToken = token
cfg.Save(config)

color.Green("Saved.")
}

func open(repoPath string, print bool) {
func Open(repoPath string, print bool) {
r, err := git.PlainOpen(repoPath)
handleError(err)

Expand Down Expand Up @@ -91,6 +53,7 @@ func open(repoPath string, print bool) {
fmt.Println("Looks like you are on the main branch. Opening home page.")

homeUrl := fmt.Sprintf("https://%s/%s", gitURL.Host, gitURL.Path)
homeUrl = strings.TrimSuffix(homeUrl, ".git")

if print {
fmt.Println(homeUrl)
Expand All @@ -102,43 +65,28 @@ func open(repoPath string, print bool) {
os.Exit(0)
}

type RemoteType int
const (
Gitlab RemoteType = 0
Github = 1
)
projectPath := strings.TrimSuffix(gitURL.Path, ".git")

var remoteType RemoteType
switch gitURL.Host {
case "gitlab.com":
remoteType = Gitlab
openGitlab(branch, projectPath, print)
case "github.com":
remoteType = Github
openGitHub(branch, projectPath, print)
default:
fmt.Println("Unknown remote type")
os.Exit(1)
}
}

if remoteType == Github {
fmt.Println("Github is not supported yet")
os.Exit(0)
}

path := gitURL.Path

// remove trailing ".git" from path if it exists
if path[len(path)-4:] == ".git" {
path = path[:len(path)-4]
}

func openGitlab(branch string, projectPath string, print bool) {
gitlabToken := cfg.Get().GitlabToken

if gitlabToken == "" {
color.Red("Gitlab token is not set. Run `pro auth gitlab` to set it.")
os.Exit(1)
}

mergeRequests, err := gitlab.MergeRequests(path, gitlabToken)
mergeRequests, err := gitlab.MergeRequests(projectPath, gitlabToken)
handleError(err)

// find merge request for current branch
Expand All @@ -148,14 +96,11 @@ func open(repoPath string, print bool) {

if currentMergeRequestIndex == -1 {
fmt.Println("No open merge request found for current branch")

fmt.Printf("Create pull request at https://gitlab.com/%s/merge_requests/new?merge_request%%5Bsource_branch%%5D=%s\n", path, branch)

fmt.Printf("Create pull request at https://gitlab.com/%s/merge_requests/new?merge_request%%5Bsource_branch%%5D=%s\n", projectPath, branch)
os.Exit(0)
}

currentMergeRequest := mergeRequests[currentMergeRequestIndex]

url := currentMergeRequest.WebUrl

if print {
Expand All @@ -166,6 +111,39 @@ func open(repoPath string, print bool) {
}
}

func openGitHub(branch string, projectPath string, print bool) {
githubToken := cfg.Get().GitHubToken

if githubToken == "" {
color.Red("GitHub token is not set. Run `pro auth github` to set it.")
os.Exit(1)
}

pullRequests, err := github.PullRequests(projectPath, githubToken)
handleError(err)

// find pull request for current branch
currentPullRequestIndex := slices.IndexFunc(pullRequests, func(pr github.PullRequestResponse) bool {
return pr.Head.Ref == branch && pr.State == "open"
})

if currentPullRequestIndex == -1 {
fmt.Println("No open pull request found for current branch")
fmt.Printf("Create pull request at https://github.com/%s/pull/new/%s\n", projectPath, branch)
os.Exit(0)
}

currentPullRequest := pullRequests[currentPullRequestIndex]
url := currentPullRequest.HtmlURL

if print {
fmt.Println(url)
} else {
fmt.Println("Opening " + url)
openBrowser(url)
}
}

func openBrowser(url string) {
var err error

Expand All @@ -185,11 +163,3 @@ func openBrowser(url string) {
os.Exit(1)
}
}

// Print error and exit
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
14 changes: 14 additions & 0 deletions commands/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package commands

import (
"fmt"
"os"
)

// Print error and exit
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Loading

0 comments on commit 64c9512

Please sign in to comment.