summaryrefslogtreecommitdiff
path: root/scripts/api/create_issue.rb
blob: 2117c285771678c4971508d67047dcb898b00421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

require 'gitlab'
require_relative 'default_options'

class CreateIssue
  def initialize(options)
    @project = options.fetch(:project)

    # Force the token to be a string so that if api_token is nil, it's set to '',
    # allowing unauthenticated requests (for forks).
    api_token = options.delete(:api_token).to_s

    warn "No API token given." if api_token.empty?

    @client = Gitlab.client(
      endpoint: options.delete(:endpoint) || API::DEFAULT_OPTIONS[:endpoint],
      private_token: api_token
    )
  end

  def execute(issue_data)
    client.create_issue(project, issue_data.delete(:title), issue_data)
  end

  private

  attr_reader :project, :client
end