Download a file from bitbucket feature branch using BitBucketAPI - bitbucket

HI I am trying to download a file based on the bit bucket branch selection . I am able to download the file from ci or master branch , but unable to download from feature branch. Can you please help me .
If i download from CI branch it works :
https://api.bitbucket.org/2.0/repositories/***Dev/testrepo/src/ci/pom.xml - this works without any issues
https://api.bitbucket.org/2.0/repositories/***Dev/testrepo/src/feature/testFeature/pom.xml - this is not working and getting java.io.FileNotFoundException:
Could you guys please help me.
Thanks in Advance.

Try this...
https://api.bitbucket.org/2.0/repositories/{workspace}/{repo}/src/{branchHash}/{path}
For branch names which include a '/' you need to use the branch hash which you can get from,
https://api.bitbucket.org/2.0/repositories/{workspace}/{repo}/refs/branches/

the above solution works only to read a file from master and ci branch ( where branch name is not having any slashes in it ) , it will not work for feature/Release branches .
I am doing it by fetching the last commit hash and with the commit hash and reading the file with last commit hash
https://api.bitbucket.org/2.0/repositories/domainname/repositoryName/refs/branches/branchName
From the Json read the commit hash - responseJsonMap["target"]["hash"]
Then construct the URL with commit hash and read the file
https://api.bitbucket.org/2.0/repositories/domainName/repositoryName/src/${hashValue}/pom.xml?at=branchName

Related

Why does MERCURIAL_REVISION return the value NULL when I try accessing it on Jenkins

On Mercurial I've implemented a hook in my hgrc file that activates when some sort of change occurs in Jenkins(i.e tagging or committing). Here is my hook code:
curl -X POST http://tokenusername:115d59a462df750d4f12347975b3d691cf#127.0.0.1:8080/job/pipelinejob/buildWithParameters/mercurial/notifyCommit?url=http://127.0.0.1:85/hg/experimentrepoistory?token=1247
So there's no issue with my hook notifying Jenkins that a change has occurred and the pipeline executes but for some reason I am having trouble getting the commit id or any or the author name's who made the commit etc. I went to the script console in jenkins and wrote the following code in groovy to see if the changeset data from Mercurial transferred over to Jenkins. Also all the libraries are imported
def job = hudson.model.Hudson.instance.getItem("pipelinejob")
def builds = job.getBuilds()
def thisBuild = builds[0]
println('Lets test Mercurial fields ' + thisBuild.getEnvironment()['MERCURIAL_REVISION']) //Lets test Mercurial fields null
It makes me think that MERCURIAL_REVISION for some reason wasnt defined even though I provided a job that has the changeset info. I was reading this documentation https://javadoc.jenkins.io/plugin/mercurial/hudson/plugins/mercurial/MercurialChangeSet.html#MercurialChangeSet-- that lists a bunch of functions that have alot of functions like getCommitId() getNode() etc that gets the information that I need. Problem is I'm not entirely sure how to instantiate MercurialChangeSet with the jenkin jobs pipelinejob that in theory should have the Mercurial commitId information. Thats why I wanted to know if I perhaps missed something obvious regarding accessing MERCURIAL_REVISION
So I found out that I need to enable the Pipeline script from SCM and that I need to put the Jenkinsfile with the pipeline code inside my workspace directory in order to get the changeset information. I am not entirely sure why this works since I would think the Jenkinsfile needs to be in the repo directory of the SCM

Download file at specific commit with Bitbucket REST API

We are trying to find a way to download a single file from a Bitbucket project using the REST API at a specific commit. Currently, we have the ability to download a file at a specific branch:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Fheads%2Fmaster
Note that the end of the URL, when decoded, contains the query parameter at=refs/heads/master, which refers to the master branch. This also works for specific tags:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Ftags%2Ftesttag1
Here the query parameter at=refs/tags/testtag1 refers to the tag (commit) testtag1.
But because of the nature of our implementation, we would like to refer directly to a commit SHA-1 hash via the Bitbucket REST API. Is this possible?
Obviously, one ugly workaround would be to just add a tag to every commit. But this could bloat the repository and it also feels like an unnecessary hack.
With the help of this SO question, I found one of the answers which tipped me off to the correct syntax. Use this:
<URL>?at=commit_hash
For example:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=bed2dda5
Here is a table of three main endpoint types with the Bitbucket REST API:
query parameter | role
---------------------------------------------
refs/heads/master | specify master branch
refs/tags/someTag | specify 'someTag' tag
at=bed2dda5 | specify commit #bed2dda5

Git commit issues with multiple files having same name

I am working on a project that contains multiples files with same file name. I am using git to maintain local versions of my changes. After staging the modified files, I notice that files with same name are appearing with status "R" implying replacing one file with another of same name but in different directory tree. How to make sure both are committed without being replaced by one another. I could not find relevant help material regarding this in any of git documentation.
Since this is a proprietary code, I am pasting only sample directory structure:
M <Proj_Root_Folder>/<dirA>/<dirAA>/file1.h
M <Proj_Root_Folder>/<dirA>/<dirAA>/file2.h
M <Proj_Root_Folder>/<dirA>/<dirAA>/file3.h
R <Proj_Root_Folder>/<dirB>/<dirBA>/file4.h -> <Proj_Root_Folder>/<dirA>/<dirAA>/file4.h
In git "R" means "rename". Git thinks that ///file4.h is a file that has been moved from where it was originally. Most likely because the file looks simmer.

How to display commit content in ruby

How do you display the commit content specified with SHA-1 in a Ruby on Rails application?
There's a good library for this. Include it in your gemfile and look at the docs for how to implement.
https://github.com/schacon/ruby-git
As a disclaimer I'm not sure if this is an answer to your question but I'll give you what I know:
In the console (folder for your project, obviously), you can get a list of the files you have changed and their status for your upcoming commit with:
git status
To get a readout of the exact code changes, you can type into the console:
gitk
Hope that helps!
Just use grit:
require 'grit'
repo = Grit::Repo.new(path_to_repo)
repo.commit(sha)
Then just play with the commit object returned. Check out http://grit.rubyforge.com/. If you want to see the changed files by that commit you can do
commit.diffs.each {|d| puts d.a_path || d.b_path }

Git merge from a specific folder only

I've created a rails website for client X. I now have a client, Y, who wants a website that does the exact same thing as client X, but with a different skin.
I made a git branch from clientXcode and called it clientYcode. I then did all the changes to the views to make it look different, and lala, the same website with a different skin.
Now here's what I don't understand about git: I've made many changes to clientXcode in the views, models, and controllers; and now I want to merge those changes into clientYcode, excluding any view changes. Since views, models, and controllers each have their own folder in rails I was hoping to be able to do something like:
git merge client_x_code 'app/controllers/*', 'app/models/*'
QUESTION 1: Is something like that possible with git? If so, how would I do it?
QUESTION 2: Was branching the best solution to make a copy of my project?
Well I found the easiest solution to my problem...
git checkout clientYcode
git checkout clientXcode "app/controllers/"
git checkout clientXcode "app/models/"
And that does what I want!
The simplest course of action is to merge everything, except the content of the directory you want to keep.
You can do that by adding a merge driver in that directory, as the How do I tell git to always select my local version for conflicted merges on a specific file? question details.
With that merge driver in place, Branching is a good solution in this case.
Extract:
We have a .gitattributes file defined in the dirWithCopyMerge directory (defined only in the branch where the merge will occurs: myBranch), and we have a .git\config file which now contains a merge driver.
[merge "keepMine"]
name = always keep mine during merge
driver = keepMine.sh %O %A %B

Resources