summaryrefslogtreecommitdiffstats
path: root/git.go
blob: ab5094ecd7315698fa80b2f87672a95320cebeea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main

import (
	"fmt"
	"net/url"
	"os"
	"strings"
	"time"

	"github.com/go-git/go-git/v5"
	"github.com/muesli/gitty/vcs"
	"github.com/muesli/gitty/vcs/github"
	"github.com/muesli/gitty/vcs/gitlab"
)

type Client interface {
	Issues(owner string, name string) ([]vcs.Issue, error)
	PullRequests(owner string, name string) ([]vcs.PullRequest, error)
	Repository(owner string, name string) (vcs.Repo, error)
	Repositories(owner string) ([]vcs.Repo, error)
	Branches(owner string, name string) ([]vcs.Branch, error)
	History(owner string, name string, max int, since time.Time) ([]vcs.Commit, error)

	GetUsername() (string, error)
	IssueURL(owner string, name string, number int) string
}

func tokenForHost(host string) string {
	token := os.Getenv("GITTY_TOKENS")

	tokens := strings.Split(token, ";")
	for _, t := range tokens {
		if !strings.Contains(t, "=") {
			continue
		}

		s := strings.Split(t, "=")
		k, v := s[0], s[1]
		if !strings.EqualFold(k, host) {
			continue
		}

		return strings.TrimSpace(v)
	}

	// fallback for old tokens
	if host == "github.com" {
		token = os.Getenv("GITTY_TOKEN")
		if len(token) > 0 {
			return token
		}
		token = os.Getenv("GITHUB_TOKEN")
		if len(token) > 0 {
			return token
		}
	}

	return ""
}

func guessClient(host string) (Client, error) {
	token := tokenForHost(host)
	if len(token) == 0 {
		return nil, fmt.Errorf("please set a GITTY_TOKENS env var for host " + host)
	}

	if strings.EqualFold(host, "github.com") {
		return github.NewClient(token)
	}
	if strings.EqualFold(host, "gitlab.com") {
		return gitlab.NewClient(host, token, true)
	}
	if strings.Contains(host, "invent.kde.org") {
		return gitlab.NewClient(host, token, true)
	}

	client, err := gitlab.NewClient(host, token, false)
	if err == nil {
		return client, nil
	}

	return nil, fmt.Errorf("not a recognized git provider")
}

func originURL(path string) (string, error) {
	r, err := git.PlainOpen(path)
	if err != nil {
		return "", err
	}

	remotes, err := r.Remotes()
	if err != nil {
		return "", err
	}

	var u string
	var rn string
	for _, v := range remotes {
		if (v.Config().Name == "origin" && rn != "origin") ||
			rn == "" {
			rn = v.Config().Name
			u = v.Config().URLs[0]
		}
	}

	if u == "" {
		return "", fmt.Errorf("no remote found")
	}

	return cleanupURL(u)
}

func cleanupURL(arg string) (string, error) {
	if strings.Contains(arg, "://") {
		arg = strings.Split(arg, "://")[1]
	}
	arg = strings.ReplaceAll(arg, ":", "/")
	arg = "https://" + arg

	u, err := url.Parse(arg)
	if err != nil {
		return "", err
	}

	u.Path = strings.TrimSuffix(u.Path, ".git")
	u.User = nil
	u.Scheme = "https"

	return u.String(), nil
}

// parseRepo returns host, owner and repository name from a given path or URL.
func parseRepo(arg string) (string, string, string, error) {
	u, err := originURL(arg)
	if err != nil {
		// not a local repo
		u, err = cleanupURL(arg)
		if err != nil {
			return "", "", "", err
		}
	}

	p := strings.Split(u, "/")
	if len(p) < 5 {
		return "", "", "", fmt.Errorf("does not look like a valid path or URL")
	}

	host, owner, name := p[2], p[3], p[4]
	return host, owner, name, nil
}