class CC::Service::Asana

Constants

ENDPOINT

Public Instance Methods

receive_issue() click to toggle source
# File lib/cc/services/asana.rb, line 41
def receive_issue
  title = %(Fix "#{issue["check_name"]}" issue in #{constant_name})

  body = [issue["description"], details_url].join("\n\n")

  create_task(title, body)
end
receive_quality() click to toggle source
# File lib/cc/services/asana.rb, line 49
def receive_quality
  create_task("#{quality_title} - #{details_url}")
end
receive_test() click to toggle source
# File lib/cc/services/asana.rb, line 30
def receive_test
  result = create_task("Test task from Code Climate")
  result.merge(
    message: "Ticket <a href='#{result[:url]}'>#{result[:id]}</a> created.",
  )
rescue CC::Service::HTTPError => ex
  body = JSON.parse(ex.response_body)
  ex.user_message = body["errors"].map { |e| e["message"] }.join(" ")
  raise ex
end
receive_vulnerability() click to toggle source
# File lib/cc/services/asana.rb, line 53
def receive_vulnerability
  formatter = CC::Formatters::TicketFormatter.new(self)
  title = formatter.format_vulnerability_title

  create_task("#{title} - #{details_url}")
end

Private Instance Methods

authenticate_http() click to toggle source
# File lib/cc/services/asana.rb, line 91
def authenticate_http
  if config.personal_access_token.present?
    http.headers["Authorization"] = "Bearer #{config.personal_access_token}"
  else
    http.basic_auth(config.api_key, "")
  end
end
create_task(name, notes = "") click to toggle source
# File lib/cc/services/asana.rb, line 62
def create_task(name, notes = "")
  params = generate_params(name, notes)
  authenticate_http
  http.headers["Content-Type"] = "application/json"
  service_post(ENDPOINT, params.to_json) do |response|
    body = JSON.parse(response.body)
    id = body["data"]["id"]
    url = "https://app.asana.com/0/#{config.workspace_id}/#{id}"
    { id: id, url: url }
  end
end
generate_params(name, notes = nil) click to toggle source
# File lib/cc/services/asana.rb, line 74
def generate_params(name, notes = nil)
  params = {
    data: { workspace: config.workspace_id, name: name, notes: notes },
  }

  if config.project_id.present?
    # Note this is undocumented, found via trial & error
    params[:data][:projects] = [config.project_id]
  end

  if config.assignee.present?
    params[:data][:assignee] = config.assignee
  end

  params
end