-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.go
73 lines (65 loc) · 1.91 KB
/
pr.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
package cli
import (
"errors"
"os"
"path/filepath"
"strings"
issuectl "github.com/janekbaraniewski/issuectl/pkg"
"github.com/spf13/cobra"
)
func isSubdir(baseDir, checkDir string) (string, error) {
// Get the relative path from baseDir to checkDir
relativePath, err := filepath.Rel(baseDir, checkDir)
if err != nil {
return "", err
}
// Check if the relative path starts with ".."
isSubdir := !strings.HasPrefix(relativePath, "..")
if !isSubdir {
return "", nil
}
return relativePath, nil
}
func getIssueIDFromParentDirectory(config issuectl.IssuectlConfig) string {
profile := config.GetProfile(config.GetCurrentProfile())
dir, err := os.Getwd()
if err != nil {
return ""
}
isSub, err := isSubdir(profile.WorkDir, dir)
if err != nil || isSub == "" {
return ""
}
issueID := strings.Split(isSub, "/")[0]
return issueID
}
func initOpenPullRequestCommand(rootCmd *cobra.Command) {
openPRCmd := &cobra.Command{
Use: "openpr [issue number] [pr title]",
Short: "Opens a pull request for the specified issue. You can specify title, if left empty default title will be generated from issue title",
Long: `This command opens a pull request for the specified issue in Repository Backend`, // FIXME: Github?? // it requires exactly one argument
RunE: func(cmd *cobra.Command, args []string) error {
config := issuectl.LoadConfig()
var issueID string
var customTitle string
if len(args) == 1 {
issueID = args[0]
} else if len(args) == 0 {
// check if can detect issue from pwd
issueID = getIssueIDFromParentDirectory(config)
} else if len(args) == 2 {
issueID = args[0]
customTitle = args[1]
}
if issueID == "" {
return errors.New("Missing issueID")
}
err := issuectl.OpenPullRequest(issuectl.IssueID(issueID), customTitle)
if err != nil {
return err
}
return nil
},
}
rootCmd.AddCommand(openPRCmd)
}