Skip to content

Commit 188eeff

Browse files
author
Aien Saidi
committed
feat(git):
- improve git communication - fix authentication issues - better information on task selection
1 parent bd038b8 commit 188eeff

5 files changed

Lines changed: 54 additions & 48 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ jobs:
2020
github_token: ${{ secrets.GITHUB_TOKEN }}
2121
goos: ${{ matrix.goos }}
2222
goarch: ${{ matrix.goarch }}
23+
ldflags: -X "main.ClientId=${{ secrets.CLIENT_ID }}" -X "main.ClientSecret=${{ secrets.CLIENT_SECRET }}"

git.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package main
22

33
import (
44
"fmt"
5+
"log"
6+
"os/exec"
7+
"strings"
8+
59
"github.com/ddliu/go-httpclient"
610
"github.com/iancoleman/strcase"
711
"github.com/manifoldco/promptui"
812
"github.com/urfave/cli/v2"
9-
"log"
10-
"os/exec"
11-
"strings"
1213
)
1314

1415
type Task struct {
@@ -63,21 +64,8 @@ var GitCommand = &cli.Command{
6364
return err
6465
}
6566

66-
tokenRes, err := httpclient.Post("https://quire.io/oauth/token", map[string]string{
67-
"grant_type": "authorization_code",
68-
"code": conf.QuireAuthToken,
69-
"client_id": conf.ClientId,
70-
"client_secret": conf.ClientSecret,
71-
})
72-
defer tokenRes.Body.Close()
73-
74-
bodyString, err := ReadBody(tokenRes)
75-
if err != nil {
76-
return err
77-
}
78-
7967
httpclient.Defaults(httpclient.Map{
80-
"Authorization": fmt.Sprintf("%s %s", bodyString["token_type"], bodyString["access_token"]),
68+
"Authorization": fmt.Sprintf("%s %s", conf.TokenType, conf.AccessToken),
8169
})
8270

8371
projRes, err := httpclient.Get(fmt.Sprintf("https://quire.io/api/task/list/id/%s", c.String("projectId")))
@@ -149,8 +137,7 @@ var GitCommand = &cli.Command{
149137
{{ "Name:" }} {{ .Name }}
150138
{{ "Id:" }} {{ .Id }}
151139
{{ "Status:" }} {{ .Status }}%
152-
{{ "Assigned to:" }} {{ .ListAssignees }}
153-
{{ "Description:" }} {{ .Description }}`,
140+
{{ "Assigned to:" }} {{ .ListAssignees }}`,
154141
}
155142

156143
prompt := promptui.Select{
@@ -175,16 +162,18 @@ var GitCommand = &cli.Command{
175162

176163
branchName := fmt.Sprintf("%s/%.0f-%s", strcase.ToKebab(task.ListAssignees()), task.Id, strcase.ToKebab(task.Name))
177164

178-
log.Printf("creating branch %q based on %q", branchName, c.String("projectId"))
165+
log.Printf("creating branch %q based on %q", branchName, c.String("from"))
179166

180-
err = exec.Command("git", "checkout", "-b", branchName, c.String("projectId")).Run()
167+
err = exec.Command("git", "checkout", "-b", branchName, c.String("from")).Run()
181168
if err != nil {
169+
log.Fatalln(err)
182170
return err
183171
}
184172

185173
log.Println("updating task status")
186174
_, err = httpclient.PutJson(fmt.Sprintf("https://quire.io/api/task/id/%s/%.0f", c.String("projectId"), task.Id), `{"status": 15}`)
187175
if err != nil {
176+
log.Fatalln(err)
188177
return err
189178
}
190179

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module riant.jetbrains.space/p/services/code/lo-quire-cli
1+
module lo.jetbrains.space/p/services/code/lo-quire-cli
22

33
go 1.14
44

main.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
6-
"github.com/ddliu/go-httpclient"
7-
"github.com/urfave/cli/v2"
87
"log"
98
"net/http"
9+
urllib "net/url"
1010
"os"
1111
"os/exec"
1212
"runtime"
13+
14+
"github.com/ddliu/go-httpclient"
15+
"github.com/urfave/cli/v2"
1316
)
1417

1518
type Configuration struct {
16-
QuireAuthToken string
17-
ClientId string
18-
ClientSecret string
19+
AccessToken string `json:"access_token"`
20+
TokenType string `json:"token_type"`
21+
RefreshToken string `json:"refresh_token"`
1922
}
2023

24+
var ClientId string
25+
var ClientSecret string
26+
2127
func main() {
2228

2329
httpclient.Defaults(httpclient.Map{
@@ -33,27 +39,17 @@ func main() {
3339
Email: "aien@lo.agency",
3440
},
3541
},
36-
Version: "0.0.1",
42+
Version: "0.0.2",
3743
Commands: []*cli.Command{
3844
GitCommand,
3945
{
4046
Name: "authorize",
4147
Usage: "Authorize the app to use Quire Boards",
42-
Flags: []cli.Flag{
43-
&cli.StringFlag{
44-
Name: "clientId",
45-
Usage: "Quire client id",
46-
},
47-
&cli.StringFlag{
48-
Name: "clientSecret",
49-
Usage: "Quire client secret",
50-
},
51-
},
5248
Action: func(c *cli.Context) error {
5349
var err error
5450
var cmd *exec.Cmd
5551

56-
var url = fmt.Sprintf("https://quire.io/oauth?client_id=%s&redirect_uri=http://localhost:1992", c.String("clientId"))
52+
var url = fmt.Sprintf("https://quire.io/oauth?client_id=%s&redirect_uri=http://localhost:1992", ClientId)
5753

5854
switch runtime.GOOS {
5955
case "linux":
@@ -86,13 +82,31 @@ func main() {
8682
// we only want the single item.
8783
key := keys[0]
8884

89-
configuration := Configuration{
90-
QuireAuthToken: key,
91-
ClientSecret: c.String("clientSecret"),
92-
ClientId: c.String("clientId"),
85+
formData := urllib.Values{
86+
"grant_type": {"authorization_code"},
87+
"code": {key},
88+
"client_id": {ClientId},
89+
"client_secret": {ClientSecret},
90+
}
91+
92+
resp, err := http.PostForm("https://quire.io/oauth/token", formData)
93+
if err != nil {
94+
log.Fatalln(err)
9395
}
9496

95-
err := SaveConfig(configuration)
97+
var result Configuration
98+
99+
/*
100+
{
101+
"access_token":"ACCESS_TOKEN",
102+
"token_type":"bearer",
103+
"expires_in":2592000,
104+
"refresh_token":"REFRESH_TOKEN"
105+
}
106+
*/
107+
json.NewDecoder(resp.Body).Decode(&result)
108+
109+
err = SaveConfig(result)
96110
if err != nil {
97111
w.WriteHeader(http.StatusInternalServerError)
98112
_, _ = w.Write([]byte(err.Error()))

util.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package main
22

33
import (
44
"encoding/json"
5-
"github.com/ddliu/go-httpclient"
65
"io/ioutil"
76
"os/user"
87
"path"
8+
9+
"github.com/ddliu/go-httpclient"
910
)
1011

12+
const ConfigFileName = ".quirecli"
13+
1114
func ReadBody(res *httpclient.Response) (map[string]interface{}, error) {
1215
bodyBytes, err := ioutil.ReadAll(res.Body)
1316
if err != nil {
@@ -49,7 +52,7 @@ func LoadConfig() (*Configuration, error) {
4952
return nil, err
5053
}
5154

52-
file, err := ioutil.ReadFile(path.Join(u.HomeDir, ".gitlo"))
55+
file, err := ioutil.ReadFile(path.Join(u.HomeDir, ConfigFileName))
5356
if err != nil {
5457
return nil, err
5558
}
@@ -75,6 +78,5 @@ func SaveConfig(configuration Configuration) error {
7578
return err
7679
}
7780

78-
return ioutil.WriteFile(path.Join(u.HomeDir, ".gitlo"), file, 0644)
81+
return ioutil.WriteFile(path.Join(u.HomeDir, ConfigFileName), file, 0644)
7982
}
80-

0 commit comments

Comments
 (0)