Danger: Unable to Setup with Bitbucket Server

Created on 9 Feb 2017  ·  32Comments  ·  Source: danger/danger

Hey,

we are currently trying to integrate danger into our internal infrastructure. Sadly we are unable to do so, as the docs seem to be incomplete.

version:
computer:App user$ gem list | grep danger
danger (4.2.1)

executing danger:

computer:App user$ bundle exec danger local --verbose
bundler: failed to load command: danger (/usr/local/bin/danger)
RuntimeError: danger cannot find your git remote, please set a remote. And the repository must host on GitHub.com or GitHub Enterprise.
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/ci_source/local_git_repo.rb:61:in `raise_error_for_missing_remote'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/ci_source/local_git_repo.rb:43:in `initialize'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/danger_core/environment_manager.rb:30:in `new'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/danger_core/environment_manager.rb:30:in `initialize'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/commands/local.rb:70:in `new'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/lib/danger/commands/local.rb:70:in `run'
 /Library/Ruby/Gems/2.0.0/gems/claide-1.0.1/lib/claide/command.rb:334:in `run'
 /Library/Ruby/Gems/2.0.0/gems/danger-4.2.1/bin/danger:5:in `<top (required)>'
 /usr/local/bin/danger:23:in `load'
 /usr/local/bin/danger:23:in `<top (required)>'

environment vars:
export DANGER_BITBUCKETSERVER_USERNAME=username
export DANGER_BITBUCKETSERVER_PASSWORD=password
export DANGER_BITBUCKETSERVER_HOST=address-to-bitbucket-server (e.g. bitbucket.local.com)
export GIT_URL=ssh://[email protected]:7999/PROJECTNAME/REPONAME.git

Are there any major missteps on our side?

Most helpful comment

For anyone interested, I got danger (5.8.2) working locally (for testing purposes) with Bitbucket Server as follows:

DANGER_BITBUCKETSERVER_HOST="https://your-bitbucket-server.com" \
DANGER_BITBUCKETSERVER_USERNAME="USERNAME" \
DANGER_BITBUCKETSERVER_PASSWORD="PASSWORD" \
JENKINS_URL="https://your-jenkins.com" \
GIT_URL=ssh://[email protected]/project/repo.git \
CHANGE_ID=1 \
bundle exec danger

Couple of notes:

  • JENKINS_URL - It can technically be anything. It is just to force Jenkins behavior of Danger
  • GIT_URL - Address of your GIT repo (set by Jenkins by default)
  • CHANGE_ID - ID of the PR

Make sure your locale is UTF-8 to avoid crashes

printenv LC_ALL
en_US.UTF-8

All 32 comments

I wonder if danger local is working with bit bucket?

Yeah - it does look like the command danger local only supports GitHub - local.rb

I'd definitely accept PRs making it run against any of the other sources (same as with danger pr)

@olbrichj Are you still facing this issue?

We actually stopped looking into it.

So we can close this? @orta?

~Ish - if this represents "I want to run danger local or danger pr against bitbucket` then not really. It's an action item for someone to be able to do it :)

I started looking into implementing danger pr for bitbucket today. Not able to figure out how to go about it.

Hi, I am just facing this issue. Was this feature implemented? Is it possible to use danger pr with a bitbucket repo?

Nope, don't think so

Please correct me if I am wrong.
"If our repo is hosted on Bitbucket, we can't use danger"

danger pr or danger local yes, but not the command you run on CI danger

@orta
Could you please elaborate your answer. Also check my comment in https://github.com/danger/danger/issues/744

Do you mean danger should only be run from CI(in which case I need to configure Jenkins on my machine) & not command line? Please share if you have any link to setup for Bitbucket.

So for what I did was
1) Cloned bitbucket repo on my local machine
2) cd into directory and "bundle init"
3) update Gemfile by adding gem "danger"
4) bundle install (gems will be installed in $HOME/.rvm)
5) bundler exec danger init

  • Create a Dangerfile and add a few simple rules.
  • Create a GitHub account for Danger to use, for messaging. DangerBot will need access to your repo
  • Set up an access token for Danger.
  • Set up Danger to run on your CI.
    6) edited .bash_profile by adding below lines
    export DANGER_BITBUCKETSERVER_USERNAME="AAA"
    export DANGER_BITBUCKETSERVER_PASSWORD="BBB"
    export DANGER_BITBUCKETSERVER_HOST="http://CCC"
    7) bundler exec danger pr (blocked here)

danger pr the command only supports working with GitHub - you'll have to skip that step

I am referring http://danger.systems/guides/getting_started.html#setting-up-danger-to-run-on-your-ci documentation but not sure what should I do next to run Dangerfile

Nothing, looks ready, it should be fine to run via danger on your CI as a part of the CI process - danger pr is a command for testing the Dangerfile on your computer instead

danger pr the command only supports working with GitHub - you'll have to skip that step

You mean skip running danger pr in terminal instead run in CI(Jenkins) process? How does it make difference if I run in terminal or in CI if Buitbucket itself is not supported? Sorry to bother you but would want to be clear on basics

I feel like I've repeated this point, danger is the command which runs on your CI - it supports bitbucket, using the ENV vars you set up.

danger pr is a separate command, which you use on your computer to test a dangerfile, it does not support bitbucket.

Thank you 👍

Hi @orta @RishabhTayal @KrauseFx @jeroenvisser101
My progress with Danger so far is as below. Please let me know if I am doing any mistake.

On my mac machine

ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16]

danger --version
/usr/local/lib/ruby/gems/2.5.0/gems/git-1.3.0/lib/git/lib.rb:999
5.5.9

Created a directory /Users/walvekar.anand/Danger which has Dangerfile

Included below code in Jenkins build shell script
cd "/Users/walvekar.anand/DangerProj”
PATH="/usr/local/bin/:$PATH"
JENKINS_URL="http://localhost:8080/job/Danger/" DANGER_BITBUCKETSERVER_PASSWORD=“XXX” DANGER_BITBUCKETSERVER_HOST="https://YYY.git" bundle exec danger --verbose

Error in Jenkins console
Not a Pull Request - skipping danger run
Finished: SUCCESS

Hi @AnandWalvekar, I think something is wrong with the way you're setting the environment variables. Could you try adding this:

export JENKINS_URL="..."
# ...
bundle exec danger --verbose

Afaik, the only way for that error to appear is when that ENV var isn't available.

Hi @jeroenvisser101
You are right it was not getting the env vars indeed. I have updated my above comment accordingly which is now showing error as "Not a Pull Request - skipping danger run"

Check out this method: https://github.com/danger/danger/blob/master/lib/danger/ci_source/jenkins.rb#L91-L101, it checks the PR number.

Adding 2 more env vars
CHANGE_URL="https://bitbucket.xxx.com/projects/UUU/repos/react-native/pull-requests" CHANGE_ID="1"

Verified the project root dir
Gemfile
Gemfile.lock
Dangerfile

Verified the remote
git remote -v

Dangerfile content
warn("Big PR") if git.lines_of_code > 500

I get error
Could not set up API to Code Review site for Danger

For Danger to run on this project, you need to expose a set of following the ENV vars:

  • GitHub: DANGER_GITHUB_API_TOKEN
  • GitLab: DANGER_GITLAB_API_TOKEN
  • BitbucketServer: DANGER_BITBUCKETSERVER_USERNAME, DANGER_BITBUCKETSERVER_PASSWORD, DANGER_BITBUCKETSERVER_HOST
  • BitbucketCloud: DANGER_BITBUCKETCLOUD_USERNAME, DANGER_BITBUCKETCLOUD_PASSWORD
  • VSTS: DANGER_VSTS_API_TOKEN, DANGER_VSTS_HOST

Found these keys in your ENV: CHANGE_ID, JENKINS_URL, CHANGE_URL, DANGER_BITBUCKETSERVER_PASSWORD, DANGER_BITBUCKETSERVER_HOST, HUDSON_SERVER_COOKIE, SHELL, TMPDIR, BUILD_TAG, OLDPWD, WORKSPACE, RUN_CHANGES_DISPLAY_URL, com.apple.java.jvmTask, USER, __CF_USER_TEXT_ENCODING, JAVA_MAIN_CLASS_76, JENKINS_HOME, PATH, RUN_DISPLAY_URL, PWD, JOB_NAME, BUILD_DISPLAY_NAME, XPC_FLAGS, BUILD_ID, JOB_BASE_NAME, XPC_SERVICE_NAME, SHLVL, HOME, EXECUTOR_NUMBER, JENKINS_SERVER_COOKIE, NODE_LABELS, LOGNAME, HUDSON_HOME, NODE_NAME, JOB_DISPLAY_URL, BUILD_NUMBER, HUDSON_COOKIE, JAVA_ARCH, _, BUNDLER_ORIG_BUNDLE_BIN_PATH, BUNDLER_ORIG_BUNDLE_GEMFILE, BUNDLER_ORIG_BUNDLER_ORIG_MANPATH, BUNDLER_ORIG_BUNDLER_VERSION, BUNDLER_ORIG_GEM_HOME, BUNDLER_ORIG_GEM_PATH, BUNDLER_ORIG_MANPATH, BUNDLER_ORIG_PATH, BUNDLER_ORIG_RB_USER_INSTALL, BUNDLER_ORIG_RUBYLIB, BUNDLER_ORIG_RUBYOPT, BUNDLE_BIN_PATH, BUNDLE_GEMFILE, BUNDLER_VERSION, RUBYOPT, RUBYLIB, GEM_PATH, GEM_HOME, MANPATH.

Failing the build, Danger cannot run without API access.
You can see more information at http://danger.systems/guides/getting_started.html
Build step 'Execute shell' marked build as failure
Finished: FAILURE

I miss DANGER_BITBUCKETSERVER_USERNAME in the list of found keys in ENV

Hi @jeroenvisser101
I have added DANGER_BITBUCKETSERVER_USERNAME which make my command to look at below.
JENKINS_URL="http://localhost:8080/job/Danger/" DANGER_BITBUCKETSERVER_USERNAME=“XXX” DANGER_BITBUCKETSERVER_PASSWORD=“YYY” DANGER_BITBUCKETSERVER_HOST="https://[email protected]/path/to/project.git" CHANGE_URL="https://git.TTT.com/projects/path/to/repos/react-native/pull-requests" CHANGE_ID="1" bundle exec danger --verbose

But now its shows
Parse error
screen shot 2018-02-14 at 10 00 24 am

JSON::ParserError: 765: unexpected token at 'Invalid request from host 125.19.27.315: Note that Bitbucket does not support Git's legacy HTTP transport protocol.'
/usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/json/common.rb:156:in parse' /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/json/common.rb:156:inparse'
/usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/request_sources/bitbucket_server_api.rb:67:in fetch_json' /usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/request_sources/bitbucket_server_api.rb:36:infetch_pr_json'
/usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/request_sources/bitbucket_server.rb:46:in fetch_details' /usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/danger_core/environment_manager.rb:51:infill_environment_vars'
/usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/danger_core/dangerfile.rb:267:in run' /usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/danger_core/executor.rb:28:inrun'
/usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/lib/danger/commands/runner.rb:68:in run' /usr/local/lib/ruby/gems/2.5.0/gems/claide-1.0.2/lib/claide/command.rb:334:inrun'
/usr/local/lib/ruby/gems/2.5.0/gems/danger-5.5.9/bin/danger:5:in <top (required)>' /usr/local/bin/danger:23:inload'
/usr/local/bin/danger:23:in ` Build step 'Execute shell' marked build as failure
Finished: FAILURE

See the first line of the error:

JSON::ParserError: 765: unexpected token at 'Invalid request from host 125.19.27.315: Note that Bitbucket does not support Git's legacy HTTP transport protocol.'

ISSUE RESOLVED

I just changed DANGER_BITBUCKETSERVER_HOST="https://[email protected]/path/to/project.git"
to DANGER_BITBUCKETSERVER_HOST="https://git.TTT.com" in above command & it worked.
Thank you very much @orta @jeroenvisser101 for you help.

Maybe you could send a PR making a warning for people with ENV Vars like that, so others don't get confused in the future?

Hi @orta @jeroenvisser101 @JuanitoFatas
I am not sure what went wrong again. With same command which worked before now its giving error

screen shot 2018-02-26 at 1 21 26 pm

The code in 5.5.10 tag is
screen shot 2018-02-26 at 1 25 02 pm

I have verified API using curl command which works fine
curl -D- -u username:password -X GET -H "Content-Type: application/json" https://companyurl.com/rest/api/1.0/projects/AWPK/repos/react-native/pull-requests/17

Observation
In the below command which was working before, irrespective of CHANGE_ID value, the error remains same. I tried giving the valid pull request Id but seems not working

JENKINS_URL="http://localhost:8080/job/Danger/" DANGER_BITBUCKETSERVER_USERNAME=“XXX” DANGER_BITBUCKETSERVER_PASSWORD=“YYY” DANGER_BITBUCKETSERVER_HOST="https://[email protected]" CHANGE_URL="https://git.TTT.com/projects/path/to/repos/react-native/pull-requests" CHANGE_ID="1" bundle exec danger --verbose

Hi @orta @jeroenvisser101
Resolved.... The issue was with back tick.
DANGER_BITBUCKETSERVER_USERNAME= XXX
DANGER_BITBUCKETSERVER_USERNAME= " XXX "

For anyone interested, I got danger (5.8.2) working locally (for testing purposes) with Bitbucket Server as follows:

DANGER_BITBUCKETSERVER_HOST="https://your-bitbucket-server.com" \
DANGER_BITBUCKETSERVER_USERNAME="USERNAME" \
DANGER_BITBUCKETSERVER_PASSWORD="PASSWORD" \
JENKINS_URL="https://your-jenkins.com" \
GIT_URL=ssh://[email protected]/project/repo.git \
CHANGE_ID=1 \
bundle exec danger

Couple of notes:

  • JENKINS_URL - It can technically be anything. It is just to force Jenkins behavior of Danger
  • GIT_URL - Address of your GIT repo (set by Jenkins by default)
  • CHANGE_ID - ID of the PR

Make sure your locale is UTF-8 to avoid crashes

printenv LC_ALL
en_US.UTF-8

I can confirm that this also works fine testing on a for Self-Hosted GitLab environment if you use the GITLAB_ environment variables for host, base url and access token.

For anyone interested, I got danger (5.8.2) working locally (for testing purposes) with Bitbucket Server as follows:

DANGER_BITBUCKETSERVER_HOST="https://your-bitbucket-server.com" \
DANGER_BITBUCKETSERVER_USERNAME="USERNAME" \
DANGER_BITBUCKETSERVER_PASSWORD="PASSWORD" \
JENKINS_URL="https://your-jenkins.com" \
GIT_URL=ssh://[email protected]/project/repo.git \
CHANGE_ID=1 \
bundle exec danger

Couple of notes:

  • JENKINS_URL - It can technically be anything. It is just to force Jenkins behavior of Danger
  • GIT_URL - Address of your GIT repo (set by Jenkins by default)
  • CHANGE_ID - ID of the PR

Make sure your locale is UTF-8 to avoid crashes

printenv LC_ALL
en_US.UTF-8
Was this page helpful?
0 / 5 - 0 ratings