forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
75 lines (63 loc) · 1.81 KB
/
http.go
File metadata and controls
75 lines (63 loc) · 1.81 KB
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
package view
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
)
var NotFoundError = errors.New("not found")
func fetchRepository(apiClient *api.Client, repo ghrepo.Interface, fields []string) (*api.Repository, error) {
query := fmt.Sprintf(`query RepositoryInfo($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {%s}
}`, api.RepositoryGraphQL(fields))
variables := map[string]interface{}{
"owner": repo.RepoOwner(),
"name": repo.RepoName(),
}
var result struct {
Repository api.Repository
}
if err := apiClient.GraphQL(repo.RepoHost(), query, variables, &result); err != nil {
return nil, err
}
return api.InitRepoHostname(&result.Repository, repo.RepoHost()), nil
}
type RepoReadme struct {
Filename string
Content string
BaseURL string
}
func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string) (*RepoReadme, error) {
apiClient := api.NewClientFromHTTP(client)
var response struct {
Name string
Content string
HTMLURL string `json:"html_url"`
}
err := apiClient.REST(repo.RepoHost(), "GET", getReadmePath(repo, branch), nil, &response)
if err != nil {
var httpError api.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return nil, NotFoundError
}
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(response.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode readme: %w", err)
}
return &RepoReadme{
Filename: response.Name,
Content: string(decoded),
BaseURL: response.HTMLURL,
}, nil
}
func getReadmePath(repo ghrepo.Interface, branch string) string {
path := fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo))
if branch != "" {
path = fmt.Sprintf("%s?ref=%s", path, branch)
}
return path
}