Skip to content

Commit

Permalink
Merge pull request #267 from CircleCI-Public/update-ux
Browse files Browse the repository at this point in the history
[CIRCLE-15483]: Catch all 400s from GitHub release api in `update`
  • Loading branch information
Zachary Scott authored Jan 28, 2019
2 parents 5896baa + cb4f8d4 commit 2123983
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
46 changes: 46 additions & 0 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,50 @@ var _ = Describe("Update", func() {
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("When Github returns a 403 error", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
"update", "check",
"--github-api", tempSettings.TestServer.URL(),
)

tempSettings.TestServer.Reset()
tempSettings.TestServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/CircleCI-Public/circleci-cli/releases"),
ghttp.RespondWith(http.StatusForbidden, []byte("Forbidden")),
),
)
})

It("should print a helpful error message & exit 255", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(255))

// TODO: This should exit with error status 1, since 255 is a
// special error status for: "exit status outside of range".
//
// However this may be difficult to change, since all commands that return
// an error after executing cause the program to exit with a non-zero code:
// https://github.com/CircleCI-Public/circleci-cli/blob/5896baa95dad1b66f9c4a5b0a14571717c92aa55/cmd/root.go#L38
stderr := session.Wait().Err.Contents()
Expect(string(stderr)).To(ContainSubstring(`Error: Failed to query the GitHub API for updates.
This is most likely due to GitHub rate-limiting on unauthenticated requests.
To have the circleci-cli make authenticated requests please:
1. Generate a token at https://github.com/settings/tokens
2. Set the token by either adding it to your ~/.gitconfig or
setting the GITHUB_TOKEN environment variable.
Instructions for generating a token can be found at:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
We call the GitHub releases API to look for new releases.
More information about that API can be found here: https://developer.github.com/v3/repos/releases/`))
})
})
})
31 changes: 27 additions & 4 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,34 @@ func latestRelease(opts *Options) error {
opts.Found = found

if err != nil {
if errResponse, ok := err.(*github.ErrorResponse); ok && errResponse.Response.StatusCode == http.StatusUnauthorized {
return errors.Wrap(err, "Your Github token is invalid. Check the [github] section in ~/.gitconfig\n")
}
ghErr, ok := err.(*github.ErrorResponse)

if ok {
if ghErr.Response.StatusCode >= 400 && ghErr.Response.StatusCode < 500 {
return errors.Wrap(err, `Failed to query the GitHub API for updates.
This is most likely due to GitHub rate-limiting on unauthenticated requests.
To have the circleci-cli make authenticated requests please:
1. Generate a token at https://github.com/settings/tokens
2. Set the token by either adding it to your ~/.gitconfig or
setting the GITHUB_TOKEN environment variable.
return errors.Wrap(err, "error finding latest release")
Instructions for generating a token can be found at:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
We call the GitHub releases API to look for new releases.
More information about that API can be found here: https://developer.github.com/v3/repos/releases/
`)
}

if ghErr.Response.StatusCode == http.StatusUnauthorized {
return errors.Wrap(err, "Your GitHub token is invalid. Check the [github] section in ~/.gitconfig\n")
}
} else {
return errors.Wrap(err, "error finding latest release")
}
}

return nil
Expand Down

0 comments on commit 2123983

Please sign in to comment.