diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 52985da..416218c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -85,7 +85,7 @@ $ gopass insert cogito/test_oauth_token ```console $ gopass insert cogito/github_app_client_id -$ gopass insert gopass show cogito/github_app_installation_id +$ gopass insert cogito/github_app_installation_id $ gopass insert --multiline cogito/github_app_private_key ``` diff --git a/github/commitstatus.go b/github/commitstatus.go index 76f926c..eaa7b45 100644 --- a/github/commitstatus.go +++ b/github/commitstatus.go @@ -145,6 +145,7 @@ func (cs CommitStatus) Add(ctx context.Context, sha, state, targetURL, descripti remaining := resp.Header.Get("X-RateLimit-Remaining") limit := resp.Header.Get("X-RateLimit-Limit") reset := resp.Header.Get("X-RateLimit-Reset") + contentType := resp.Header.Get("Content-Type") cs.log.Debug( "http-request", "method", req.Method, @@ -161,7 +162,18 @@ func (cs CommitStatus) Add(ctx context.Context, sha, state, targetURL, descripti } body, _ := io.ReadAll(resp.Body) - return NewGitHubError(resp, errors.New(strings.TrimSpace(string(body)))) + buffer := body + if strings.Contains(strings.ToLower(contentType), "application/json") { + var foo map[string]any + if err := json.Unmarshal(body, &foo); err != nil { + return fmt.Errorf("normalizing JSON: unmarshal: %s", err) + } + buffer, err = json.Marshal(foo) + if err != nil { + return fmt.Errorf("normalizing JSON: marshal: %s", err) + } + } + return NewGitHubError(resp, errors.New(strings.TrimSpace(string(buffer)))) } if err := cs.target.Retry.Do(Backoff, Classifier, workFn); err != nil { diff --git a/github/commitstatus_test.go b/github/commitstatus_test.go index 9a8dfd7..57ccfb2 100644 --- a/github/commitstatus_test.go +++ b/github/commitstatus_test.go @@ -418,7 +418,7 @@ func TestGitHubStatusFailureIntegration(t *testing.T) { name: "bad token: Unauthorized", token: "bad-token", wantErr: `failed to add state "success" for commit 751affd: 401 Unauthorized -Body: {"message":"Bad credentials","documentation_url":"https://docs.github.com/rest","status":"401"} +Body: {"documentation_url":"https://docs.github.com/rest","message":"Bad credentials","status":"401"} Hint: Either wrong credentials or PAT expired (check your email for expiration notice) Action: POST https://api.github.com/repos/pix4d/cogito-test-read-write/statuses/751affd155db7a00d936ee6e9f483deee69c5922 OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `, @@ -428,7 +428,7 @@ OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `, name: "non existing repo: Not Found", repo: "non-existing-really", wantErr: `failed to add state "success" for commit 751affd: 404 Not Found -Body: {"message":"Not Found","documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","status":"404"} +Body: {"documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","message":"Not Found","status":"404"} Hint: one of the following happened: 1. The repo https://github.com/pix4d/non-existing-really doesn't exist 2. The user who issued the token doesn't have write access to the repo @@ -441,7 +441,7 @@ OAuth: X-Accepted-Oauth-Scopes: repo, X-Oauth-Scopes: repo:status`, name: "non existing SHA: Unprocessable Entity", sha: "e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822", wantErr: `failed to add state "success" for commit e576e3a: 422 Unprocessable Entity -Body: {"message":"No commit found for SHA: e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822","documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","status":"422"} +Body: {"documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","message":"No commit found for SHA: e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822","status":"422"} Hint: none Action: POST https://api.github.com/repos/pix4d/cogito-test-read-write/statuses/e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822 OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: repo:status`,