forked from jehiah/git-open-pull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
158 lines (146 loc) · 4.26 KB
/
settings.go
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
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"strings"
"github.com/jehiah/git-open-pull/internal/input"
)
type Settings struct {
// config: github.user
User string
// config: gitOpenPull.token
Token string
// config: gitOpenPull.baseAccount
BaseAccount string
// config: gitOpenPull.baseRepo
BaseRepo string
// config: gitOpenPull.base
BaseBranch string
// Editor to use for draft PR description (default: vi)
// config: core.editor
Editor string
// Allow maintainers of the upstream repo to modify this branch
// https://help.github.com/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork/
// config: gitOpenPull.maintainersCanModify
MaintainersCanModify bool
// command to pre or post process the commit template
// It is run with the first argument as the template name
PreProcess string
PostProcess string
// callback is called after a PR is created. It's first argument is a filename that contains the PR json
Callback string
}
// LoadSettings extracts the git and gitOpenPull sections from $HOME/.gitconfig and .git/config
func LoadSettings(ctx context.Context) (*Settings, error) {
body, err := RunGit(ctx, "config", "--list")
if err != nil {
return nil, err
}
s := Settings{
BaseBranch: "master",
Editor: "/usr/bin/vi",
}
var defaultBaseRepo, maintainersCanModify string
scanner := bufio.NewScanner(bytes.NewBuffer(body))
for scanner.Scan() {
line := strings.SplitN(strings.TrimSpace(scanner.Text()), "=", 2)
if len(line) != 2 {
return nil, fmt.Errorf("Invalid line %#v", line)
}
switch line[0] {
case "github.user":
s.User = line[1]
case "gitopenpull.token":
s.Token = line[1]
case "gitopenpull.baseaccount":
s.BaseAccount = line[1]
case "gitopenpull.baserepo":
s.BaseRepo = line[1]
case "gitopenpull.base":
s.BaseBranch = line[1]
case "gitopenpull.maintainerscanmodify":
maintainersCanModify = line[1]
switch strings.ToLower(line[1]) {
case "true":
s.MaintainersCanModify = true
}
case "gitopenpull.preprocess":
s.PreProcess = line[1]
case "gitopenpull.postprocess":
s.PostProcess = line[1]
case "gitopenpull.callback":
s.Callback = line[1]
case "core.editor":
s.Editor = line[1]
default:
if strings.HasSuffix(line[0], ".url") && strings.HasSuffix(line[1], ".git") && defaultBaseRepo == "" {
chunks := strings.Split(line[1], "/")
defaultBaseRepo = chunks[len(chunks)-1]
defaultBaseRepo = defaultBaseRepo[:len(defaultBaseRepo)-4]
}
}
}
if maintainersCanModify == "" {
s.MaintainersCanModify = true
}
if err := scanner.Err(); err != nil {
return nil, err
}
// https://github.com/settings/tokens
if s.User == "" {
s.User, err = input.Ask("GitHub username", "")
if err != nil {
return nil, err
}
if s.User == "" {
return nil, errors.New("GitHub username required. Set `git config --global github.user $USER`")
}
_, err = RunGit(ctx, "config", "--global", "github.user", s.User)
if err != nil {
return nil, err
}
}
if s.BaseAccount == "" {
s.BaseAccount, err = input.Ask("destination GitHub username (account to pull code into)", "")
if err != nil {
return nil, err
}
if s.BaseAccount == "" {
return nil, fmt.Errorf("Destination GitHub username required. Set `git config gitOpenPull.baseAccount $USER`")
}
_, err = RunGit(ctx, "config", "gitOpenPull.baseAccount", s.BaseAccount)
if err != nil {
return nil, err
}
}
if s.BaseRepo == "" {
s.BaseRepo, err = input.Ask(fmt.Sprintf("GitHub repository name (ie: github.com/%s/___)", s.BaseAccount), defaultBaseRepo)
if err != nil {
return nil, err
}
if s.BaseRepo == "" {
return nil, fmt.Errorf("GitHub repository name required. Set `git config gitOpenPull.baseAccount $PROJECT`")
}
_, err = RunGit(ctx, "config", "gitOpenPull.baseRepo", s.BaseRepo)
if err != nil {
return nil, err
}
}
if s.Token == "" {
s.Token, err = input.Ask("Github access token (You can genrate a token from https://github.com/settings/tokens)", "")
if err != nil {
return nil, err
}
if s.Token == "" {
return nil, fmt.Errorf("Github token required. Set `git config --global gitOpenPull.token $TOKEN`")
}
_, err = RunGit(ctx, "config", "--global", "gitOpenPull.token", s.Token)
if err != nil {
return nil, err
}
}
return &s, nil
}