Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing pull request result in code 422- "Cannot change the base branch of a closed pull request" #3456

Closed
MarcinGinszt opened this issue Jan 30, 2025 · 3 comments

Comments

@MarcinGinszt
Copy link

MarcinGinszt commented Jan 30, 2025

Steps to reproduce:

prs, _, _ := client.PullRequests.List(ctx, owner, repo, nil)
pull := prs[0]
pull.State = github.String("closed")
// pull.Base = nil  <- if this line is added, error does not appear
_, _, err := client.PullRequests.Edit(ctx, owner, repo, *pull.Number, pull)
require.NoError(t, err)

This code successfully closes the PR, but results in error:

received unexpected error:
PATCH https://api.github.com/repos/owner/repo/pulls/1: 422 Validation Failed [{Resource:PullRequest Field:base Code:invalid Message:Cannot change the base branch of a closed pull request.}]

Is that expected behaviour? Do I use the library correctly?

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 30, 2025

Actually, your code worked fine for me without modification, so I would check the settings in your repo if you have permissions to modify the PR in question.

Here's my working example (use of httpdebug is highly recommended):

package main

import (
	"context"
	"log"
	"os"

	dbg "github.com/gmlewis/go-httpdebug/httpdebug"
	"github.com/google/go-github/v68/github"
	"golang.org/x/oauth2"
)

const (
	owner = "gmlewis"
	repo  = "my-test-repo"
)

func main() {
	log.SetFlags(0)
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: os.Getenv("GITHUB_SECRET")},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	tp := dbg.New(dbg.WithTransport(tc.Transport))
	client := github.NewClient(tp.Client())
	ctx := context.Background()

	prs, _, err := client.PullRequests.List(ctx, owner, repo, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("GML: Got %v PRs.", len(prs))

	pull := prs[0]
	pull.State = github.String("closed")
	// pull.Base = nil // <- if this line is added, error does not appear
	if _, _, err := client.PullRequests.Edit(ctx, owner, repo, *pull.Number, pull); err != nil {
		log.Fatal(err)
	}

	log.Printf("Done.")
}

Here's the run:

go run main.go
curl -X GET \
  https://api.github.com/repos/gmlewis/my-test-repo/pulls \
  -H 'Accept: application/vnd.github.v3+json' \
  -H 'User-Agent: go-github/v68.0.0' \
  -H 'X-Github-Api-Version: 2022-11-28'
GML: Got 1 PRs.
curl -X PATCH \
  https://api.github.com/repos/gmlewis/my-test-repo/pulls/10 \
  -H 'Accept: application/vnd.github.v3+json' \
  -H 'Content-Type: application/json' \
  -H 'User-Agent: go-github/v68.0.0' \
  -H 'X-Github-Api-Version: 2022-11-28' \
  -d '{"title":"test PR","body":"Description","state":"closed"}
'
Done.

The PR in question was successfully closed: gmlewis/my-test-repo#10

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 30, 2025

Your error message sounds funny though:

Cannot change the base branch of a closed pull request.

sounds like you were trying to close an already-closed pull request... I'm not sure how that happened.
But the code above looks fine to me.

@gmlewis
Copy link
Collaborator

gmlewis commented Mar 17, 2025

Closing due to inactivity.

@gmlewis gmlewis closed this as completed Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants