Skip to content

Commit 0479204

Browse files
committed
close #247 web command for jira
1 parent 6c2001a commit 0479204

File tree

13 files changed

+193
-164
lines changed

13 files changed

+193
-164
lines changed

cmd/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ Auth supports two services:
2929
func init() {
3030
RootCmd.AddCommand(authCmd)
3131

32-
authCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
32+
authCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
3333
}

cmd/deliver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ func init() {
3838

3939
deliverCmd.Flags().BoolVarP(&options.GlobalFlagShowTestOutput, "showtest", "s", false, "show test output")
4040

41-
deliverCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
41+
deliverCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
4242
}

cmd/issues.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ import (
1313
// issueCmd represents the issue command
1414
var issueCmd = &cobra.Command{
1515
Use: "issues",
16-
Short: "list GitHub issues",
16+
Short: "list issues from Task management system",
1717
Long: fmt.Sprintf(`
18-
%s lists all the open issues on GitHub with its ID.
18+
%s lists the 30 next issues in your management system.
19+
Uses the configuration to decide target
1920
`, ui.Format.Bold("issues")),
2021
PreRun: func(cmd *cobra.Command, args []string) {
2122
cmdperm.RequiredCurDirRepository()
2223
},
2324
Run: func(cmd *cobra.Command, args []string) {
24-
phlow.Issues()
25+
phlow.IssueCaller()
2526
},
2627
}
2728

2829
func init() {
2930
RootCmd.AddCommand(issueCmd)
3031

3132
issueCmd.Flags().BoolVarP(&options.GlobalFlagMine, "mine", "m", false, "only list issues assigned to you")
33+
34+
issueCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
35+
3236
}

cmd/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ If no argument is given, it tries to find the issue of the currently checked out
3737
func init() {
3838
RootCmd.AddCommand(webCmd)
3939

40-
webCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
40+
webCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
4141
}

cmd/workon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ func init() {
3737
RootCmd.AddCommand(workonCmd)
3838

3939
//Target for configuration
40-
workonCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
40+
workonCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
4141

4242
}

cmd/wrapup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ func init() {
2929

3030
wrapupCmd.Flags().StringVar(&options.GlobalFlagForceMessage, "force", "", "use a custom commit message instead")
3131

32-
wrapupCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .gitconfig files")
32+
wrapupCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in .gitconfig")
3333
}

phlow/issues.go

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,83 @@ package phlow
22

33
import (
44
"fmt"
5-
"os"
65
"github.com/praqma/git-phlow/plugins"
7-
"os/exec"
8-
"strings"
96
"github.com/praqma/git-phlow/executor"
10-
"bytes"
11-
"github.com/praqma/git-phlow/ui"
12-
"runtime"
7+
"github.com/praqma/git-phlow/setting"
8+
"github.com/praqma/git-phlow/options"
9+
"github.com/praqma/git-phlow/githandler"
1310
)
1411

15-
func Issues() {
16-
ui.PhlowSpinner.Start("")
17-
issues, err := plugins.GitHub.GetIssues()
18-
if err != nil {
19-
fmt.Println(err)
20-
os.Exit(0)
12+
//IssueCaller ...
13+
//prints issue with given target
14+
func IssueCaller() {
15+
conf := setting.NewProjectStg(options.GlobalFlagTarget)
16+
17+
if conf.Service == "github" {
18+
PrintIssues(conf, FetchGH)
2119
}
22-
ui.PhlowSpinner.Stop()
2320

24-
//Collection Issues to a string
25-
var buffer bytes.Buffer
26-
for _, issue := range issues {
27-
buffer.WriteString(issue.ToString())
21+
if conf.Service == "jira" {
22+
PrintIssues(conf, FetchJ)
2823
}
24+
}
2925

30-
pager := GetPager()
26+
//Fetch ...
27+
//Type for getting issues
28+
type Fetch func(*setting.ProjectSetting) ([]plugins.Stringer, error)
3129

32-
if pager == "" {
33-
fmt.Println(buffer.String())
34-
} else {
35-
IssuesInPager("less", buffer.String())
30+
//Fetch ...
31+
//Fetch for github
32+
func FetchGH(conf *setting.ProjectSetting) ([]plugins.Stringer, error) {
33+
git := githandler.Git{Run: executor.RunGit}
34+
remote, err := git.LSRemote("--get-url", conf.Remote)
35+
if err != nil {
36+
return nil, err
3637
}
37-
}
3838

39-
//GetPager ...
40-
//return the pager if set
41-
func GetPager() string {
42-
pager := os.Getenv("PAGER")
43-
if pager != "" {
44-
return pager
39+
token, err := git.Config("--get", "phlow.token")
40+
if err != nil {
41+
return nil, err
4542
}
4643

47-
if runtime.GOOS == "windows" {
48-
return "more"
44+
oar := githandler.OrgAndRepo(remote)
45+
46+
list, err := plugins.GetIssuesGitHub(conf.IssueApi, oar.Organisation, oar.Repository, token)
47+
if err != nil {
48+
return nil, err
4949
}
50-
return ""
50+
return list, nil
5151
}
5252

53-
func IssuesInPager(pager, text string) error {
54-
cmd := exec.Command(pager)
55-
cmd.Stdin = strings.NewReader(text)
56-
cmd.Stdout = os.Stdout
57-
err := executor.ExecuteCommander(cmd)
53+
//FetchJ
54+
//fetch for Jira
55+
func FetchJ(conf *setting.ProjectSetting) ([]plugins.Stringer, error) {
56+
git := githandler.Git{Run: executor.RunGit}
57+
user, err := git.Config("--get", "phlow.jirauser")
5858
if err != nil {
59-
return err
59+
return nil, err
6060
}
61-
return nil
61+
62+
token, err := git.Config("--get", "phlow.jiratoken")
63+
if err != nil {
64+
return nil, err
65+
}
66+
list, err := plugins.QueryIssues(conf.IssueApi, user, token)
67+
if err != nil {
68+
return nil, err
69+
}
70+
return list, nil
6271
}
6372

64-
//IssueList ...
65-
//List open issues from GitHub
66-
func IssueList() {
73+
func PrintIssues(conf *setting.ProjectSetting, fetch Fetch) {
74+
list, err := fetch(conf)
75+
if err != nil {
76+
fmt.Println(err)
77+
return
78+
}
6779

68-
//ui.PhlowSpinner.Start("")
69-
//
70-
//ui.PhlowSpinner.Stop()
71-
//
72-
////Nested function for finding user issues
73-
//var userIssue = func(issue []plugins.AssigneeIssue) bool {
74-
// user := githandler.ConfigGet("user", "phlow")
75-
// for _, u := range issue {
76-
// if u.Login == user {
77-
// return true
78-
// }
79-
// }
80-
// return false
81-
//}
82-
//
83-
//fmt.Println(ui.Format.MileStone("# Issue"))
84-
//
85-
//for _, issue := range issues {
86-
// assignees := issue.Assignees
87-
// //If mine is true we print on issues assigned to a user
88-
// if options.GlobalFlagMine {
89-
// if userIssue(assignees) {
90-
// printIssue(issue)
91-
// }
92-
// } else {
93-
// printIssue(issue)
94-
// }
95-
//}
80+
for _, iss := range list {
81+
fmt.Println(iss.ToString())
82+
}
9683

9784
}

phlow/issues_test.go

Lines changed: 0 additions & 77 deletions
This file was deleted.

plugins/ghmodels.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ type Assignee struct {
4848
Assignees []string `json:"assignees"`
4949
}
5050

51-
//Stringer ...
52-
//interface for github formats
53-
type Stringer interface {
54-
ToString() string
55-
}
56-
5751
//ToString ...
5852
//Formats issue
59-
func (issue *Issue) ToString() string {
53+
func (issue Issue) ToString() string {
6054
var buffer bytes.Buffer
6155

6256
buffer.WriteString(ui.Format.Bold(strconv.Itoa(issue.Number) + ": "))
@@ -71,7 +65,5 @@ func (issue *Issue) ToString() string {
7165
}
7266
buffer.WriteString(" " + ui.Format.MileStone(issue.Milestone.Title))
7367

74-
buffer.WriteString("\n")
75-
7668
return buffer.String()
7769
}

plugins/github.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,58 @@ func DefaultBranchGitHub(URL, org, repo, token string) (defaultBranch string, er
8787

8888
//GetIssueGitHub ...
8989
//return an issue with from the number of the issue
90+
func GetIssuesGitHub(URL, org, repo, token string) ([]Stringer, error) {
91+
92+
req, _ := http.NewRequest("GET", URL+fmt.Sprintf("/repos/%s/%s/issues", org, repo), nil)
93+
req.Header.Set("Content-Type", "application/json")
94+
q := req.URL.Query()
95+
q.Add("access_token", token)
96+
q.Add("per_page", "30")
97+
req.URL.RawQuery = q.Encode()
98+
99+
if options.GlobalFlagVerbose {
100+
fmt.Println(req.URL)
101+
}
102+
103+
client := http.DefaultClient
104+
105+
res, err := client.Do(req)
106+
if err != nil {
107+
return nil, err
108+
}
109+
defer res.Body.Close()
110+
111+
if res.StatusCode == 401 {
112+
return nil, errors.New("Not Authorized \nVerify that you are authorized by running 'git phlow auth' with the same configuration")
113+
}
114+
115+
if res.StatusCode == 404 && (org == "" || repo == "") {
116+
return nil, errors.New("Could not reach GitHub API - Malformed URL \nVerify 'Remote' field is correct in configuration" +
117+
"\ntry 'git ls-remote --get <Remote from config>' should return: [email protected]:org/repo.git")
118+
}
119+
120+
if res.StatusCode != 200 {
121+
return nil, errors.New("Could not get list of issues ")
122+
}
123+
124+
body, err := ioutil.ReadAll(res.Body)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
re := []Issue{}
130+
if err = json.Unmarshal(body, &re); err != nil {
131+
return nil, err
132+
}
133+
134+
iss := make([]Stringer, len(re))
135+
for i, v := range re {
136+
iss[i] = v
137+
}
138+
139+
return iss, nil
140+
}
141+
90142
func GetIssueGitHub(URL, org, repo, key, token string) (*Issue, error) {
91143

92144
req, _ := http.NewRequest("GET", URL+fmt.Sprintf("/repos/%s/%s/issues/", org, repo)+key, nil)

0 commit comments

Comments
 (0)