Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions pkg/github/pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"slices"
"strings"

"github.com/go-viper/mapstructure/v2"
"github.com/google/go-github/v92/github"
Expand Down Expand Up @@ -877,10 +878,14 @@ func CreatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
TeamReviewers: teamReviewers,
}

// The pull request already exists at this point, so a reviewer failure must still
// tell the caller which pull request was created. Otherwise a retry only reports
// that a pull request for the head branch already exists.
reviewerErrMsg := fmt.Sprintf("pull request #%d was created (%s), but failed to request reviewers", pr.GetNumber(), pr.GetHTMLURL())
_, reviewerResp, err := client.PullRequests.RequestReviewers(ctx, owner, repo, pr.GetNumber(), reviewersRequest)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to request reviewers",
reviewerErrMsg,
reviewerResp,
err,
), nil, nil
Expand All @@ -896,7 +901,7 @@ func CreatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
}
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to request reviewers", reviewerResp, bodyBytes), nil, nil
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, reviewerErrMsg, reviewerResp, bodyBytes), nil, nil
}
}

Expand Down Expand Up @@ -1080,6 +1085,16 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
return utils.NewToolResultError("No update parameters provided."), nil, nil
}

// The steps below are separate API calls. Once one of them has changed the pull
// request, a later failure must say so, or the caller assumes nothing changed.
updated := false
stepErrMsg := func(step string) string {
if updated {
return fmt.Sprintf("pull request #%d was updated, but %s%s", pullNumber, strings.ToLower(step[:1]), step[1:])
}
return step
Comment on lines +1091 to +1095

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2e2a76a. The GraphQL client lookup for the draft step, the client lookup for the reviewer request, the reviewer response-body read and the client lookup for the final read now go through stepErrMsg, so they keep the original message when nothing changed and report the partial update otherwise. Test_UpdatePullRequest_ClientFailureAfterChangeReportsEarlierChanges covers each lookup failing after an earlier change and with no earlier change; the three "after a change" cases fail without the fix. ./script/lint reports 0 issues and go test -race ./... passes.

}

// Handle REST API updates (title, body, state, base, maintainer_can_modify)
if restUpdateNeeded {
client, err := deps.GetClient(ctx)
Expand All @@ -1104,13 +1119,14 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
}
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to update pull request", resp, bodyBytes), nil, nil
}
updated = true
}

// Handle draft status changes using GraphQL
if draftProvided {
gqlClient, err := deps.GetGQLClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub GraphQL client", err), nil, nil
return utils.NewToolResultErrorFromErr(stepErrMsg("failed to get GitHub GraphQL client"), err), nil, nil
}

var prQuery struct {
Expand All @@ -1128,7 +1144,7 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
"prNum": githubv4.Int(pullNumber), // #nosec G115 - pull request numbers are always small positive integers
})
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find pull request", err), nil, nil
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, stepErrMsg("Failed to find pull request"), err), nil, nil
}

currentIsDraft := bool(prQuery.Repository.PullRequest.IsDraft)
Expand All @@ -1149,7 +1165,7 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
PullRequestID: prQuery.Repository.PullRequest.ID,
}, nil)
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to convert pull request to draft", err), nil, nil
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, stepErrMsg("Failed to convert pull request to draft"), err), nil, nil
}
} else {
// Mark as ready for review
Expand All @@ -1166,17 +1182,18 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
PullRequestID: prQuery.Repository.PullRequest.ID,
}, nil)
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to mark pull request ready for review", err), nil, nil
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, stepErrMsg("Failed to mark pull request ready for review"), err), nil, nil
}
}
updated = true
}
}

// Handle reviewer requests
if len(reviewers) > 0 {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
return utils.NewToolResultErrorFromErr(stepErrMsg("failed to get GitHub client"), err), nil, nil
}

userReviewers, teamReviewers := splitPullRequestReviewers(reviewers)
Expand All @@ -1188,7 +1205,7 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
_, resp, err := client.PullRequests.RequestReviewers(ctx, owner, repo, pullNumber, reviewersRequest)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to request reviewers",
stepErrMsg("failed to request reviewers"),
resp,
err,
), nil, nil
Expand All @@ -1202,21 +1219,22 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
return utils.NewToolResultErrorFromErr(stepErrMsg("failed to read response body"), err), nil, nil
}
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to request reviewers", resp, bodyBytes), nil, nil
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, stepErrMsg("failed to request reviewers"), resp, bodyBytes), nil, nil
}
updated = true
}

// Get the final state of the PR to return
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
return utils.NewToolResultErrorFromErr(stepErrMsg("failed to get GitHub client"), err), nil, nil
}

finalPR, resp, err := client.PullRequests.Get(ctx, owner, repo, pullNumber)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "Failed to get pull request", resp, err), nil, nil
return ghErrors.NewGitHubAPIErrorResponse(ctx, stepErrMsg("Failed to get pull request"), resp, err), nil, nil
}
defer func() {
if resp != nil && resp.Body != nil {
Expand Down
Loading