Skip to content

Commit 3881d6e

Browse files
committed
get project
1 parent 0f65194 commit 3881d6e

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

api/queries_project.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
9+
"github.com/cli/cli/internal/ghinstance"
10+
"github.com/cli/cli/internal/ghrepo"
11+
)
12+
13+
func (c Client) GetProject(baseRepo ghrepo.Interface, projectID string) ([]byte, error) {
14+
url := fmt.Sprintf("%sprojects/%s",
15+
ghinstance.RESTPrefix(baseRepo.RepoHost()), projectID)
16+
17+
req, err := http.NewRequest("GET", url, nil)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
// TODO api() { gh api -H 'accept: application/vnd.github.inertia-preview+json' "$@"; }
23+
req.Header.Set("Accept", "application/vnd.github.inertia-preview+json; charset=utf-8")
24+
25+
resp, err := c.http.Do(req)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
if resp.StatusCode == 404 {
31+
return nil, &NotFoundError{errors.New("pull request not found")}
32+
} else if resp.StatusCode != 200 {
33+
return nil, HandleHTTPError(resp)
34+
}
35+
36+
data, err := ioutil.ReadAll(resp.Body)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return data, nil
42+
}

pkg/cmd/project/project.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package project
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"net/http"
57

8+
"github.com/cli/cli/api"
69
"github.com/cli/cli/internal/ghrepo"
710
"github.com/cli/cli/pkg/cmdutil"
811
"github.com/cli/cli/pkg/iostreams"
@@ -29,14 +32,11 @@ func NewCmdProject(f *cmdutil.Factory, runF func(*ProjectOptions) error) *cobra.
2932
Short: "Interact with a GitHub project",
3033
Long: "Enter an interactive UI for viewing and modifying a GitHub project",
3134
Hidden: true,
32-
Args: cobra.MaximumNArgs(1),
35+
Args: cobra.NoArgs,
3336
RunE: func(c *cobra.Command, args []string) error {
3437
// support `-R, --repo` override
3538
opts.BaseRepo = f.BaseRepo
3639

37-
if len(args) > 0 {
38-
opts.Selector = args[0]
39-
}
4040
if runF != nil {
4141
return runF(&opts)
4242
}
@@ -48,5 +48,46 @@ func NewCmdProject(f *cmdutil.Factory, runF func(*ProjectOptions) error) *cobra.
4848
}
4949

5050
func projectRun(opts *ProjectOptions) error {
51+
// TODO interactively ask which project they want since these IDs are not easy to get
52+
projectID := "3514315"
53+
54+
c, err := opts.HttpClient()
55+
if err != nil {
56+
return err
57+
}
58+
client := api.NewClientFromHTTP(c)
59+
60+
baseRepo, err := opts.BaseRepo()
61+
if err != nil {
62+
return err
63+
}
64+
65+
project, err := getProject(client, baseRepo, projectID)
66+
if err != nil {
67+
return err
68+
}
69+
70+
fmt.Printf("DBG %#v\n", project)
71+
5172
return nil
5273
}
74+
75+
type Project struct {
76+
Name string
77+
}
78+
79+
func getProject(client *api.Client, baseRepo ghrepo.Interface, projectID string) (*Project, error) {
80+
data, err := client.GetProject(baseRepo, projectID)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
project := &Project{}
86+
87+
err = json.Unmarshal(data, project)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
return project, nil
93+
}

0 commit comments

Comments
 (0)