GitHub API Create Pull Request Validation Failed

Viewed 17

I am having a hard time trying to create a GitHub Pull Request through API calls. So far so now I was able to authenticate (test making API call to other endpoint and all of them return the expected results). My issue is when I try to create a Pull Request, I am using the following as reference: https://docs.github.com/en/rest/pulls/pulls#create-a-pull-request

$Body = @{
           title = "Test";
           body  = "This is a test Pull Request";
           head  = "test";
           base = "master"
         }
Invoke-RestMethod -Method Post -Headers $Headers -Uri "https://api.github.com/repos/owner/my_repo/pulls" -Body ($Body | ConvertTo-Json)

Invoke-RestMethod : {"message":"Validation Failed","errors":[{"resource":"PullRequest","field":"head","code":"invalid"} ],"documentation_url":"https://docs.github.com/rest/reference/pulls#create-a-pull-request"}

The name of the branch is "test" as posted on the example. I also tried the following

$Body = @{
           title = "Test";
           body  = "This is a test Pull Request";
           head  = "my_user:test";
           base = "master"
         }
Invoke-RestMethod -Method Post -Headers $Headers -Uri "https://api.github.com/repos/owner/my_repo/pulls" -Body ($Body | ConvertTo-Json)

But I get the same error. Do you know what I am missing?
Also, is there any way to add an approver to the Pull Request through the API call?

1 Answers

I finally fix it, it was a typo on mi headers. I had

$Headers = @{
              Accept        = "application/vnd.githubjson"; (missing the + between github and json)
              Authorization = "Basic $($Credentials)";
            }

instead of

$Headers = @{
              Accept        = "application/vnd.github+json";
              Authorization = "Basic $($Credentials)";
            }
Related