Home > Mobile >  How to create a pull request after authentication using Github Enterprise API?
How to create a pull request after authentication using Github Enterprise API?

Time:09-24

I have written a shell script to create a pull request with Github Enterprise API.

#!/bin/sh
CURRENT_BRANCH=$(git branch --show-current)
git stash save
git checkout master
git pull
UUID=$(uuidgen)
git checkout -b "${UUID}"
npm version patch
git add package.json
git commit -m "#; update version script test"
git push origin "${UUID}"
curl -u [MY_USER_NAME]:[MY_TOKEN] https://github.[MY_ORGANIZATION].com/api/v3/user
curl \
  -X POST \
  -H "Accept: application/vnd.github.v3 json" \
  https://github.[MY_ORGANIZATION].com/api/v3/repos/Modules/[MY_REPO]/pulls \
  -d "{'head':'${UUID}','base':'master'}"

The log is displayed as below. It fail to create the pull request with the error

"message": "Must authenticate to access this API."

However, there seems no issue with the authentication.

// omit some useless log here
remote:
To github.microstrategy.com:Modules/mstr-web-hierarchy.git
 * [new branch]      23CFA0E3-33D1-489E-9E55-D38F92BB1B99 -> 23CFA0E3-33D1-489E-9E55-D38F92BB1B99
{
  "login": "shizhang",
  "id": 1191,
  // some useless properties here
{
  "message": "Must authenticate to access this API.",
  "documentation_url": "https://docs.github.com/enterprise/3.0/rest"
}

CodePudding user response:

Your second curl does not seem to include any authentication information.

Try adding the same -u [MY_USER_NAME]:[MY_TOKEN] as you used in the first curl.

In other words, the first curl success does not mean other curls would benefit from an authenticated session.


Problems parsing JSON

Try with a simpler JSON, just for testing.
And pay attention to quotes: '${UUID}' would not be expanded, and would remain exactly as '${UUID}'.
See "How to include environment variable in bash line CURL?"

  • Related