Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

danger pr treats GitLab URLs as GitHub and fails #1388

Open
unikitty37 opened this issue Sep 26, 2022 · 6 comments
Open

danger pr treats GitLab URLs as GitHub and fails #1388

unikitty37 opened this issue Sep 26, 2022 · 6 comments

Comments

@unikitty37
Copy link

Report

What did you do?

export DANGER_GITLAB_HOST=my.gitlab.instance
export DANGER_GITLAB_API_BASE_URL=https://my.gitlab.instance/api/v4
export DANGER_GITLAB_API_TOKEN=**REDACTED**
danger --base=main pr https://my.gitlab.instance/user/project/-/merge_requests/1

What did you expect to happen?

Danger either examines the URL format, or retrieves the URL, and then works out that it should be using the GitLab tokens.

I'm not sure if this is down to Danger failing to detect GitLab when it's an on-prem instance, or danger pr only supporting GitHub, though.

What happened instead?

Danger just goes ahead and treats it as GitHub, regardless:

bundler: failed to load command: danger (/usr/local/bundle/bin/danger)
/usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/support/pull_request_finder.rb:157:in `client': No API token given, please provide one using `DANGER_GITHUB_API_TOKEN` or `DANGER_GITHUB_BEARER_TOKEN` (RuntimeError)
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/support/pull_request_finder.rb:90:in `find_remote_pull_request'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/support/pull_request_finder.rb:50:in `find_pull_request'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/support/pull_request_finder.rb:18:in `call'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/local_git_repo.rb:88:in `find_pull_request'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/ci_source/local_git_repo.rb:42:in `initialize'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/danger_core/environment_manager.rb:30:in `new'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/danger_core/environment_manager.rb:30:in `initialize'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/commands/pr.rb:60:in `new'
	from /usr/local/bundle/gems/danger-9.0.0/lib/danger/commands/pr.rb:60:in `run'
	from /usr/local/bundle/gems/claide-1.1.0/lib/claide/command.rb:334:in `run'
	from /usr/local/bundle/gems/danger-9.0.0/bin/danger:5:in `<top (required)>'
	from /usr/local/bundle/bin/danger:25:in `load'
	from /usr/local/bundle/bin/danger:25:in `<top (required)>'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli/exec.rb:58:in `load'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli/exec.rb:58:in `kernel_load'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli/exec.rb:23:in `run'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli.rb:483:in `exec'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli.rb:31:in `dispatch'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/cli.rb:25:in `start'
	from /usr/local/bundle/gems/bundler-2.3.15/exe/bundle:48:in `block in <top (required)>'
	from /usr/local/bundle/gems/bundler-2.3.15/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
	from /usr/local/bundle/gems/bundler-2.3.15/exe/bundle:36:in `<top (required)>'
	from /usr/local/bundle/bin/bundle:25:in `load'
	from /usr/local/bundle/bin/bundle:25:in `<main>'

Your Environment

  • Which CI are you running on? GitLab CI, though the command is being run locally, not in CI.
  • Are you running the latest version of Danger? 9.0.0
  • What is your Dangerfile?
# frozen_string_literal: true

# region core
CHANGED_FILES = (git.added_files + git.modified_files).freeze
ADDED_FILES = git.added_files.freeze

CI_PROJECT_URL = ENV['CI_PROJECT_URL']
CI_REF = ENV['CI_COMMIT_REF_NAME']
CI_JOB_NAME = ENV['CI_JOB_NAME']

ROOT_PATH = Pathname.new(Dir.pwd)

lockfile_lines = {}
lockfile = IO.readlines('Gemfile.lock', chomp: true)
lockfile.each_index do |index|
  %r{\A(\S+?)\s+\(}.match(lockfile[index]) { |matches| lockfile_lines[matches[1]] = index + 1 } # line 1 is lockfile[0]
end

def artifacts_link(name)
  "[#{name}](#{CI_PROJECT_URL}/-/jobs/artifacts/#{CI_REF}/raw/#{name}?job=#{CI_JOB_NAME})"
end

# Check that db/structure.sql is changed if migrations changed (allow any .rubocop.yml changes)
updated_migrations_files = CHANGED_FILES.select { |path| path.include? 'db/migrate' }
                                        .reject { |path| path.include? '.rubocop.yml' }
structure_updated = CHANGED_FILES.include?('db/structure.sql')

if updated_migrations_files.any? && !structure_updated
  migration_message = <<~MESSAGE
    Check if db/structure.sql needs to be updated.
    You have the following migrations updated or added:
    #{gitlab.html_link(updated_migrations_files)}
  MESSAGE
  warn(migration_message, file: updated_migrations_files.first, line: 1)
end
# endregion
@orta
Copy link
Member

orta commented Sep 26, 2022

Pretty sure it only works with github

@unikitty37
Copy link
Author

Ah, that's a shame. I'm trying to debug why I can't get Danger working with this new repo, and having to make a back-and-forth with CI is really slowing things down 🙁

I'll leave this open until I can have a look to see if I can come up with a PR to add GitLab functionality…

@dmlond
Copy link

dmlond commented Feb 20, 2023

this isnt as nice as using pr , but you can set the following 3 environment variables in your terminal session before running danger and it works. These are Gitlab Predefined Variables that are set in every gitlab job.

export GITLAB_CI=true
export CI_PROJECT_PATH='path' # The project namespace with the project name included.
export CI_MERGE_REQUEST_IID=41 # this is the internal id of the merge request in the project
danger

@chrisvltn
Copy link

In case someone lands here, in addition to @dmlond 's variables, I also had to add CI_API_V4_URL and DANGER_GITLAB_API_TOKEN for it to work in a self-hosted instance

export GITLAB_CI=true
export CI_PROJECT_PATH='group/project' # The project namespace with the project name included.
export CI_MERGE_REQUEST_IID=41 # this is the internal id of the merge request in the project
export DANGER_GITLAB_API_TOKEN="xxx"
export CI_API_V4_URL="https://example.gitlab.com/api/v4"
danger

@leoniralves
Copy link

leoniralves commented Apr 18, 2024

The @dmlond and @chrisvltn solutions worked for me, but now I'm getting 404 error on Danger request.
Did anyone have the same problem?
https://gitlab.com/gitlab-org/gitlab-foss/-/issues/29022

@leoniralves
Copy link

Update: For me, @dmlond and @chrisvltn solutions worked, but I needed to change the CI_PROJECT_PATH value. Instead of group/project I used project_id.

export CI_PROJECT_PATH="12345"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants