How can I send a pull request via command line in Bitbucket? - command-line

I have to send a lot of pull requests, so I would rather use the bash command line than bitbucket's web interface.
Usage example: $ git-req username
Here is such a script for Github: http://pastebin.com/F9n3nPuu
Is there one for Bitbucket?

Bitbucket with it's RESTful API 2.0 supports managing pull requests without interface. In CLI you can request it with CURL. This older version of the documentation has better interface details.
Get pull request data with CURL
To get full data about specific pull request:
$ curl --user s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests/4
In return I get JSON with full info about my pull request #4 (put your username twice, password and reponame in command).
Create new pull request with RESTClient
To create new pull request we need to provide a lot of data with POST command, below how it looks in my RESTClient:
After firing Bitbucket shows pull request immediately:
Create new pull request with CURL
You can still create the same pull request with one liner:
$ curl -X POST -H "Content-Type: application/json" -u s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests -d '{ "title": "Merge some branches", "description": "stackoverflow example", "source": { "branch": { "name": "choose branch to merge with" }, "repository": { "full_name": "s3m3n/reponame" } }, "destination": { "branch": { "name": "choose branch that is getting changes" } }, "reviewers": [ { "username": "some other user needed to review changes" } ], "close_source_branch": false }'
REST browser tool (discontinued)
If you want to test all possible methods of API hop to REST browser tool of Bitbucket. It will show you all possible requests while returning your real repo's data.

Tried and tested :
Generate personal access token by clicking here
Save the Unique token id, append it after "Bearer in header".
For example: "Authorization : Bearer MDg4MzA4NTcfhtrhthyt/Thyythyh "
Complete JSON sample here:
Step 1 to enter the details and necessary headers
Try running it
Step 2
Output on BitBucket, You will be able to see the pull request
Final output
Command Line Syntax:
curl -i -X POST -H "Authorization:Bearer MDg4MzA4NTk/TlMSS6Ea" -H "X-Atlassian-Token:no-check" -H "Content-Type:application/json" -d '{"description":"1. Changes made 2. Changes made 3. Hello hanges","closed":false,"fromRef":{"id":"refs\/heads\/branch","repository":{"name":"From Repository ","project":{"key":"ProjectName"},"slug":"From Repository "}},"state":"OPEN","title":"Merge changes from branch to master","locked":false,"reviewers":[],"open":true,"toRef":{"id":"refs\/heads\/master","repository":{"name":"RepoName","project":{"key":"ProjectName"},"slug":"RepoName"}}}' 'https://bitbucket.agile.com/rest/api/1.0/projects/projectName/repos/repoName/pull-requests'

There are 2 repos on bitbucket that could help:
the Attlassian team have stash (ruby): https://bitbucket.org/atlassian/bitbucket-server-cli
Zhemao has bitbucket-cli (python): https://bitbucket.org/zhemao/bitbucket-cli
both have pull request feature from command line.

I wasn't too satisfied with the answers in this thread, so I created an package for it:
https://www.npmjs.com/package/bitbucket-pr
Instructions:
npm i -g bitbucket-pr
... Go to folder where you want to create a pull request ...
bitbucket-pr

I have created a pull request cli utility to ease my tasks.
Currently,
it can create/delete pr's right from the terminal
show basic diff for a raised PR
I have tested it with bitbucket enterprise 6.10.10
Source code: https://github.com/psadi/bbcli

Related

Use Github action secrets on local machine - is it possible?

I know I can use curl to list the secrets of a repo via curl like so:
curl -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token [personal access token]" \
https://api.github.com/repos/[user]/[repo]/actions/secrets
This returns something like this:
{
"total_count": 1,
"secrets": [
{
"name": "A_TEST_SECRET",
"created_at": "2022-04-14T13:12:22Z",
"updated_at": "2022-04-14T13:12:22Z"
}
]
}
But is it somehow possible to also retrieve the secret's value?
My use case is this:
I have a step in a Github Actions workflow that uses a secret and needs to be executed before I can run my build step, which just contains a script to run.
Now sometimes I want to also run my buildscript on my local machine, but to do that I also need to run the step before it, which needs the secret. Is there any way I can retrieve the secret's value to a local machine from Github to do that?
As stated by Github Partner brightran on this Github Community Thread:
We have no safe way to fetch the values of secrets, and actually it is not recommended to try fetching and sharing the secrets.
He also shared in that post two ways to use the repository secrets in the source code files of your project when you build the project in the workflow runs, maybe you can try them with your buildscript in a Github Actions workflow that could be triggered manually (using a workflow_dispatch event, for example).

Create github issue from travis.yaml

I am looking for some ways to create a github issue from travis.
I am calling some scripts in travis.yaml file and I need to create a github issue when travis is executed. I came across documents on calling github APIS using curl command.
Eg: curl -u $username -i -H "Content-Type: application/json" -X POST --data '{"title":"'$title'", "body":"'$body'"}' https://api.github.com/repos/$username/$repo_name/issues
Instead of username , since the build is triggered via travis, should I use github tokens? Is there any environment variable available which represents github token.
Found the answer myself. Create a github token using the github API and add that as ENV variable to your Travis CI settings.
This token can be used to perform the curl operation in travis shell script.
Helpful link : https://blogs.infosupport.com/accessing-githubs-rest-api-with-curl/

github new branch creation and pull request - using REST API

I need to create a branch and then do a pull request using REST API. First i tired to create a new branch with the following curl command.
[root#localhost tmp]# curl -d '{ "ref": "refs/heads/new_branch", "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd" }' -u user-name -X POST https://api.github.com/repos/user-name/myrepo/git/refs
Enter host password for user 'user-name':
{
"message": "Object does not exist",
"documentation_url": "https://developer.github.com/v3/git/refs/#create-a-reference"
}
A better alternative (since February 2020) is to use the new GitHub cli, which replaces/offers an alternative to github/hub.
See "GitHub CLI is now in beta " and "Supercharge your command line experience":
It is cli.github.com.
If will create a fork for you:
Create a pull request
Create a branch, make several commits to fix the bug described in the issue, and use gh to create a pull request to share your contribution.
By using GitHub CLI to create pull requests, it also automatically creates a fork when you don’t already have one, and it pushes your branch and creates your pull request to get your change merged.
a good alternative is to use hub instead. https://github.com/github/hub
For example:
if you haven't cloned the repo yet:
hub checkout [PULLREQ-URL]
if you have a repo cloned already, and just want a new branch
hub checkout [BRANCH]
Using the following link to review the commands for creating a new branch (AKA: refs) and this link to review how to validate the credentials, I could create a new empty branch. If you use the command without the validation, you will get a confusing answer
"message": "Not Found"
I followed this step to create a new branch named:
Get the SHA using this command -> GET https://api.github.com/repos/{username}/{repository}/git/refs/heads and copy one SHA (In my case, all are the same)
Create a token using this link and copy it. This token will be used in the parameter to create the branch.
Create the new branch using this command -> POST https://api.github.com/repos/{username}/{repository}/git/refs, setting a basic authorization {username}:{token} in the header and in the body the follow content:
{
"ref": "refs/heads/{BranchName}",
"sha": "{SHA copied in the step 1}"
}
Finally, you will obtain the information of the new branch

How to get all commits in a Git tag through GitHub API

I have to fetch all new commits that were a part when a new tag was created on a Git repo. This needs to be done through GitHub API.
For example the Git UI says Tagging Tag1 and has a sha associated with it... let's say the sha is : SHA1
Now how do I get all commits which happened or were a part of Tag1 through GitHub API? I want to store all these commits and perform some analysis on them.
Based on the clarification on your comment:
I want to get all commits between this newly created tag and previous tag
1. Get all the tags in a given repo, so you can get the current and the previous tag names
curl -X "GET" "https://api.github.com/repos/:owner/:repo/tags" \
-H "Authorization: token YOUR_GITHUB_ACCESS_TOKEN"
2. Get all the commits between the latest 2 tags
curl -X "GET" "https://api.github.com/repos/:owner/:repo/compare/:tag_1...:tag_2" \
-H "Authorization: token YOUR_GITHUB_ACCESS_TOKEN"
Doc links:
https://developer.github.com/v3/repos/#list-tags
https://developer.github.com/v3/repos/commits/#compare-two-commits

Creating release using Octopus rest API

Please can anybody explain me how to create release using Octopus REST API.
I can create a release using octo.exe but have no idea how to do that using REST API.
I went through the http://localhost:8080/api, but cant figure it out how to create a release.
Is this REST api providing that feature??
I have not used Octopus myself, but as far as I understand you have to make an HTTP POST request to an URL like this:
http://localhost:8080/api/projects/1/releases
In your request body you specify the same parameters as you would do it using the command line tool but you have to encode them as JSON.
I had similar trouble with this myself.
In order to create a release, you must make a POST request to the Octo server.
https://myoctoserver:port/api/releases
Provide the following headers:
X-Octopus-ApiKey: API-XXXXXXXXXXXX
Content-Type: application/json
The body of the request must be JSON. Below is an example:
{
"Version": "2017.02.25.183053" ,
"ProjectId": "MyProject" ,
"ChannelId": "DefaultOrOtherChannelId
}
The Version and ProjectId properties are required. Channel ID is optional. However, if you have more than one channel or if no channel in your project is marked as default then you must include ChannelId as well.
I hope this helps!
Curl Example
The sample below has been successful.
BODY='{"ProjectId":"'$PROJECT_ID'","ChannelId":"Channels-1","Version":"'$VERSION'","SelectedPackages":[{"StepName":"$STEP_NAME1","Version":"'$VERSION'"},{"StepName":"$STEP_NAME2","Version":"'$VERSION'"}]}'
curl -X POST --write-out %{http_code} --silent --output /dev/null -H "X-Octopus-ApiKey:$API_KEY" -H "Content-Type:application/json" -d $BODY "https://octopus.example.com/api/releases"
Notes
In order to find the ChannelId and ProjectId I had to query the Octopus database. The IDs will look something like Projects-1 or Channel-1
Documentation for interfacing with the Octopus REST API leaves a lot to be desired:
https://github.com/OctopusDeploy/OctopusDeploy-Api/wiki/Releases
I could never get it working through this approach, instead, I use the octo.exe command line utility to create releases:
octo create-release --project HelloWorld --version 1.0.3 --server http://octopus/ --apiKey API-ABCDEF123456
Octo.exe included as part of tentacle or server installs, Octopus also provide it as a seperate utility:
http://octopusdeploy.com/downloads