Skip to content

Commit

Permalink
Merge pull request #688 from bugsnag/create_project_step
Browse files Browse the repository at this point in the history
Create project via data access api and configure maze runner
  • Loading branch information
sazap10 authored Oct 18, 2024
2 parents 9ae3b99 + 77fe74d commit 7554071
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
12 changes: 12 additions & 0 deletions lib/features/steps/setup_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @!group Setup steps

# Creates a new project in bugsnag
Given('I create a new project {string} with type {string}') do |name, type|
org_id = data_access_api.get_first_org_id
project = data_access_api.create_project(org_id, name, type)
project_id = project['id']
data_access_api.set_project_id(project_id)
api_key = data_access_api.get_project_api_key(project_id)
Maze.config.bugsnag_repeater_api_key = api_key
Maze.config.bugsnag_data_access_project_id = project_id
end
47 changes: 35 additions & 12 deletions lib/maze/client/bugsnag/data_access_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,52 @@ module Bugsnag
# An abstraction for the underlying Bugsnag data access
class DataAccessApi
# @param api_key The Bugsnag API key
# @param endpoint The endpoint to use for the Bugsnag Data Access API#
# @param endpoint The endpoint to use for the Bugsnag Data Access API
# @param project_id The project ID to use for the Bugsnag Data Access API
def initialize
@auth_token = Maze.config.bugsnag_data_access_api_key
@endpoint = Maze.config.bugsnag_data_access_api_endpoint
@project_id = Maze.config.bugsnag_data_access_project_id
opts = {
auth_token: @auth_token,
endpoint: @endpoint,
project_id: @project_id
}
if @endpoint.start_with?('http')
@client = create_client(auth_token: @auth_token, endpoint: @endpoint, project_id: @project_id)
end

def set_project_id(project_id)
@project_id = project_id
@client = create_client(auth_token: @auth_token, endpoint: @endpoint, project_id: @project_id)
end

def get_event(event_id)
@client.event(@project_id, event_id)
end

def create_project(org_id, name, type)
@client.create_project(org_id, name, type)
end

def get_project_api_key(project_id)
project = @client.project(project_id)
project['api_key']
end

def get_first_org_id
orgs = @client.organizations
orgs.first['id']
end

private

def create_client(auth_token:, endpoint:, project_id:, opts: {})
opts[:auth_token] = auth_token
opts[:endpoint] = endpoint
opts[:project_id] = project_id
if endpoint.start_with?('http')
opts[:connection_options] = {
ssl: {
verify: false
}
}
end
@client = ::Bugsnag::Api::Client.new(opts)
end

def get_event(event_id)
@client.event(@project_id, event_id)
::Bugsnag::Api::Client.new(opts)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: We can create a project, send an event to the new project and verify
that the event is available via the data access api

Scenario: We can create a project, send an event to the new project and verify
Given I create a new project "My Project" with type "ruby"
When I send a request to the server
Then I wait to receive an error
And the last event is available via the data access api
And the pipeline event payload field "exceptions.0.message" equals "This is an error"
6 changes: 3 additions & 3 deletions test/fixtures/pipeline-events/features/steps/steps.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
When('I send a request to the server') do
test_payload = JSON.generate({
apiKey: ENV['MAZE_REPEATER_API_KEY'],
apiKey: Maze.config.bugsnag_repeater_api_key,
notifier: {
name: 'Ruby Bugsnag Notifier',
version: '6.27.1',
Expand Down Expand Up @@ -51,10 +51,10 @@
http = Net::HTTP.new('localhost', '9339')
request = Net::HTTP::Post.new('/notify')
request['content-type'] = 'application/json'
request['bugsnag-api-key'] = ENV['MAZE_REPEATER_API_KEY']
request['bugsnag-api-key'] = Maze.config.bugsnag_repeater_api_key
request['bugsnag-payload-version'] = '4.0'
request['bugsnag-sent-at'] = Time.now.utc.iso8601
request.body = test_payload

http.request(request)
end
end

0 comments on commit 7554071

Please sign in to comment.