Merge pull request #1403 from cli/readme-markdown

Report HTTP and Markdown errors in `repo view`
This commit is contained in:
Nate Smith 2020-07-21 11:53:29 -05:00 committed by GitHub
commit 71bebd3f54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 34 deletions

View file

@ -134,6 +134,10 @@ type NotFoundError struct {
error
}
func (err *NotFoundError) Unwrap() error {
return err.error
}
func (pr PullRequest) HeadLabel() string {
if pr.IsCrossRepository {
return fmt.Sprintf("%s:%s", pr.HeadRepositoryOwner.Login, pr.HeadRefName)

View file

@ -12,7 +12,6 @@ import (
"time"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
"github.com/shurcooL/githubv4"
)
@ -406,35 +405,35 @@ func RepoCreate(client *Client, input RepoCreateInput) (*Repository, error) {
return initRepoHostname(&response.CreateRepository.Repository, "github.com"), nil
}
func RepositoryReadme(client *Client, fullName string) (string, error) {
type readmeResponse struct {
type RepoReadme struct {
Filename string
Content string
}
func RepositoryReadme(client *Client, repo ghrepo.Interface) (*RepoReadme, error) {
var response struct {
Name string
Content string
}
var readme readmeResponse
err := client.REST("GET", fmt.Sprintf("repos/%s/readme", fullName), nil, &readme)
if err != nil && !strings.HasSuffix(err.Error(), "'Not Found'") {
return "", fmt.Errorf("could not get readme for repo: %w", err)
}
decoded, err := base64.StdEncoding.DecodeString(readme.Content)
err := client.REST("GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response)
if err != nil {
return "", fmt.Errorf("failed to decode readme: %w", err)
}
readmeContent := string(decoded)
if isMarkdownFile(readme.Name) {
readmeContent, err = utils.RenderMarkdown(readmeContent)
if err != nil {
return "", fmt.Errorf("failed to render readme as markdown: %w", err)
var httpError HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return nil, &NotFoundError{err}
}
return nil, err
}
return readmeContent, nil
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),
}, nil
}
type RepoMetadataResult struct {
@ -895,12 +894,3 @@ func RepoMilestones(client *Client, repo ghrepo.Interface) ([]RepoMilestone, err
return milestones, nil
}
func isMarkdownFile(filename string) bool {
// kind of gross, but i'm assuming that 90% of the time the suffix will just be .md. it didn't
// seem worth executing a regex for this given that assumption.
return strings.HasSuffix(filename, ".md") ||
strings.HasSuffix(filename, ".markdown") ||
strings.HasSuffix(filename, ".mdown") ||
strings.HasSuffix(filename, ".mkdown")
}

View file

@ -1,6 +1,7 @@
package command
import (
"errors"
"fmt"
"net/url"
"os"
@ -612,10 +613,23 @@ func repoView(cmd *cobra.Command, args []string) error {
return err
}
readmeContent, _ := api.RepositoryReadme(apiClient, fullName)
readme, err := api.RepositoryReadme(apiClient, toView)
var notFound *api.NotFoundError
if err != nil && !errors.As(err, &notFound) {
return err
}
if readmeContent == "" {
readmeContent = utils.Gray("No README provided")
var readmeContent string
if readme == nil {
readmeContent = utils.Gray("This repository does not have a README")
} else if isMarkdownFile(readme.Filename) {
var err error
readmeContent, err = utils.RenderMarkdown(readme.Content)
if err != nil {
return fmt.Errorf("error rendering markdown: %w", err)
}
} else {
readmeContent = readme.Content
}
description := repo.Description
@ -648,3 +662,12 @@ func repoView(cmd *cobra.Command, args []string) error {
func repoCredits(cmd *cobra.Command, args []string) error {
return credits(cmd, args)
}
func isMarkdownFile(filename string) bool {
// kind of gross, but i'm assuming that 90% of the time the suffix will just be .md. it didn't
// seem worth executing a regex for this given that assumption.
return strings.HasSuffix(filename, ".md") ||
strings.HasSuffix(filename, ".markdown") ||
strings.HasSuffix(filename, ".mdown") ||
strings.HasSuffix(filename, ".mkdown")
}

View file

@ -912,6 +912,9 @@ func TestRepoView_blanks(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.Register(httpmock.GraphQL(`query RepositoryInfo\b`), httpmock.StringResponse("{}"))
http.Register(
httpmock.REST("GET", "repos/OWNER/REPO/readme"),
httpmock.StatusStringResponse(404, `{}`))
output, err := RunCommand("repo view")
if err != nil {
@ -921,6 +924,6 @@ func TestRepoView_blanks(t *testing.T) {
test.ExpectLines(t, output.String(),
"OWNER/REPO",
"No description provided",
"No README provided",
"This repository does not have a README",
"View this repository on GitHub: https://github.com/OWNER/REPO")
}