How to create pull request using command line?

jeff_lu October 9, 2014

Hi,

I'm getting 401 error when trying to create a pull request via command line

curl -u user:password -X POST -H "Content-Type: application/json" https://host:8443/projects/myproject/repos/myrepo/pull-requests/\?create\&sourceBranch\=refs/heads/bugfix/feature-branch\&targetBranch\=refs/heads/develop

What am I doing wrong?

4 answers

1 vote
TimP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 9, 2014

Thanks for the update Jeff,

You need to pass a JSON payload containing the source and destination refs as the request body, rather than specifying them with -d. e.g. 

The example from the documentation is: 

{
    "title": "Talking Nerdy",
    "description": "It’s a kludge, but put the tuple from the database in the cache.",
    "state": "OPEN",
    "open": true,
    "closed": false,
    "fromRef": {
        "id": "refs/heads/feature-ABC-123",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "toRef": {
        "id": "refs/heads/master",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "reviewers": [
        {
            "user": {
                "name": "charlie"
            }
        }
    ]
}

I'm surprised you're getting a 401 though. If you've got the password wrong too many times, you might have triggered CAPTCHA on your account. Are you able to log in and create pull requests via the UI using the user you're authenticating as?

If all else fails, I'd add the -v flag to your command and see if the response has any more information, and enable debug logging on the Stash server to see if you can get some insight into what's going wrong.

cheers,

Tim

jeff_lu October 10, 2014

Tim, I must not be doing this correctly. I'm still getting the same error. After so many tries it does trigger CAPTCHA, then I just have to login again from the web. Try again it will give me the same 401 error. curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -u user:password -v \ https://host:8443/rest/api/3.3/projects/myproject/repos/myrepo/pull-requests/ \ -d '{ "title": "Testing", "description": "Create pull request test", "state": "OPEN", "open": true, "closed": false, "fromRef": { "id": "refs/heads/bugfix/feature-123", "repository": { "slug": "myrepo", "name": null, "project": { "key": "myproject" } } }, "toRef": { "id": "refs/heads/develop", "repository": { "slug": "myrepo", "name": null, "project": { "key": "myproject" } } }, "reviewers": [ { "user": { "name": "team" } } ] }' * Adding handle: conn: 0x7fad3300c600 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fad3300c600) send_pipe: 1, recv_pipe: 0 * About to connect() to host port 8443 (#0) * Trying 192.168.253.52... * Connected to host (192.168.253.52) port 8443 (#0) * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 * Server certificate: *.host * Server certificate: GlobalSign Organization Validation CA - SHA256 - G2 * Server certificate: GlobalSign Root CA * Server auth using Basic with user 'user' > POST /rest/api/3.3/projects/myproject/repos/myrepo/pull-requests/ HTTP/1.1 > Authorization: Basic amx1OmpLbGNvbQ== > User-Agent: curl/7.30.0 > Host: host:8443 > Accept: application/json > Content-Type: application/json > Content-Length: 745 > * upload completely sent off: 745 out of 745 bytes < HTTP/1.1 401 Unauthorized HTTP/1.1 401 Unauthorized * Server Apache-Coyote/1.1 is not blacklisted < Server: Apache-Coyote/1.1 Server: Apache-Coyote/1.1 < X-AREQUESTID: @MG75UCx1242x179700x0 X-AREQUESTID: @MG75UCx1242x179700x0 < X-ASEN: SEN-3350037 X-ASEN: SEN-3350037 < Set-Cookie: _atl_stash_remember_me=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/ Set-Cookie: _atl_stash_remember_me=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/ * Authentication problem. Ignoring this. < WWW-Authenticate: Basic realm="Atlassian Stash" WWW-Authenticate: Basic realm="Atlassian Stash" < Content-Type: application/json;charset=UTF-8 Content-Type: application/json;charset=UTF-8 < Content-Length: 130 Content-Length: 130 < Date: Sat, 11 Oct 2014 00:42:21 GMT Date: Sat, 11 Oct 2014 00:42:21 GMT < * Connection #0 to host host left intact {"errors":[{"context":null,"message":"Authentication failed. Please check your credentials and try again.","exceptionName":null

0 votes
ThiagoBomfim
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 9, 2014

Hi Jeff,

On a side note, you will be pleased to see the small (but great) improvement release on Stash 3.3. Please refer to the section "Pull request URLs are displayed in the console after pushing" of:

Best regards,
Thiago Bomfim
Atlassian Support

0 votes
jeff_lu October 9, 2014

Sorry, I paste the wrong command.

This is what I was doing:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -u user:password \
https://host:8443/rest/api/1.0/projects/myproject/repos/myrepo/pull-requests/ \
-d src=refs/heads/bugfix/feature-branch \
-d dest=refs/heads/develop
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
X-AREQUESTID: @MG75UCx1378x156187x1
X-ASEN: SEN-3350037
Set-Cookie: _atl_stash_remember_me=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
WWW-Authenticate: Basic realm="Atlassian Stash"
Content-Type: application/json;charset=UTF-8
Content-Length: 130
Date: Fri, 10 Oct 2014 02:58:56 GMT

{"errors":[{"context":null,"message":"Authentication failed. Please check your credentials and try again.","exceptionName":null}]}%

0 votes
TimP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 9, 2014

Hi Jeff,

That's not the Pull Request REST API, that's a web UI url. 

Take a look at https://developer.atlassian.com/static/rest/stash/latest/stash-rest.html, specifically the /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests resource.

cheers,

Tim

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events