Skip to content

Commit

Permalink
Make build parameters more flexible as the Bitrise API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Cecchi committed Aug 28, 2020
1 parent 1acff2e commit 20c1c95
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -28,7 +28,7 @@ This plugin assumes you already have an app configured on Bitrise and uses a Per

### Known issues

- For now the only option is to trigger a build via a commit hash. It should be more flexible as Bitrise allows triggering by branch, tag, commit or default strategy.
- Triggering a build with a tag is not yet implemented
- The author option to trigger the build is not implemented
- The environments option to trigger the build is not implemented
- Pagination on API responses is not implemented
Expand All @@ -45,7 +45,7 @@ Use this action to trigger a workflow on Bitrise and query its status.
| `app_slug` | The app slug of the project on Bitrise | BITRISE_APP_SLUG | |
| `access_token` | The [personal access token](https://devcenter.bitrise.io/api/authentication/) used to call Bitrise API | BITRISE_ACCESS_TOKEN | |
| `workflow` | The name of the workflow to trigger | BITRISE_WORKFLOW | |
| `branch` | The name of branch that will be checked out (optional) | BITRISE_BUILD_BRANCH | |
| `branch` | The name of branch that will be checked out | BITRISE_BUILD_BRANCH | |
| `commit_hash` | The hash of the commit that will be checked out (overrides branch parameter) | BITRISE_BUILD_COMMIT_HASH | |
| `build_message` | A custom message that will be used to identify the build | BITRISE_BUILD_MESSAGE | |
| `triggered_by` | A custom message that will be used to identify where the build was triggered from (optional) | BITRISE_BUILD_TRIGGERED_BY | |
Expand Down
4 changes: 3 additions & 1 deletion fastlane/Fastfile
Expand Up @@ -5,7 +5,9 @@
lane :test do
build = trigger_bitrise_workflow(
workflow: "build",
build_message: "Build triggered from Fastlane :)",
branch: "master",
build_message: "Test build",
triggered_by: "Fastlane Bitrise Automation plugin :)",
wait_for_build: true # Disable this if you don't need to wait for the build results
)

Expand Down
Expand Up @@ -12,12 +12,12 @@ def self.run(params)
type: "bitrise"
},
build_params: {
workflow_id: params[:workflow],
commit_hash: params[:commit_hash],
commit_message: params[:build_message]
workflow_id: params[:workflow]
}
}
trigger_payload[:build_params][:branch] = params[:branch] unless params[:branch].nil? || params[:branch].empty?
trigger_payload[:build_params][:commit_hash] = params[:commit_hash] unless params[:commit_hash].nil? || params[:commit_hash].empty?
trigger_payload[:build_params][:commit_message] = params[:build_message] unless params[:build_message].nil? || params[:build_message].empty?
trigger_payload[:triggered_by] = params[:triggered_by] unless params[:triggered_by].nil? || params[:triggered_by].empty?

response = Helper::BitriseRequestHelper.post(params, 'builds', trigger_payload.to_json)
Expand Down Expand Up @@ -114,12 +114,12 @@ def self.available_options
FastlaneCore::ConfigItem.new(key: :commit_hash,
env_name: "BITRISE_BUILD_COMMIT_HASH",
description: "The hash of the commit that will be checked out",
optional: false,
optional: true,
type: String),
FastlaneCore::ConfigItem.new(key: :build_message,
env_name: "BITRISE_BUILD_MESSAGE",
description: "A custom message that will be used to identify the build",
optional: false,
optional: true,
type: String),
FastlaneCore::ConfigItem.new(key: :triggered_by,
env_name: "BITRISE_BUILD_TRIGGERED_BY",
Expand Down
70 changes: 48 additions & 22 deletions spec/trigger_bitrise_workflow_action_spec.rb
Expand Up @@ -11,44 +11,75 @@

describe Fastlane::Actions::TriggerBitriseWorkflowAction do
describe 'trigger bitrise build' do
it 'calls the Bitrise API with the provided parameters and returns build info' do
it 'calls the Bitrise API and returns build info' do
stub_request(:post, "https://api.bitrise.io/v0.1/apps/appslug123/builds").
to_return(body: build_created_response, status: 201)

response = Fastlane::Actions::TriggerBitriseWorkflowAction.run(
app_slug: "appslug123",
workflow: "workflow_name",
wait_for_build: false
)

expect(response['status']).to eq("on_hold")
expect(response['build_url']).to eq("http://example.com/builds/100")
expect(response['build_number']).to eq(100)
expect(response['build_slug']).to eq("abc123")
end

it 'calls the Bitrise API with the minimum parameters when others are not specified' do
expected_request_payload = {
hook_info: {
type: "bitrise"
},
build_params: {
workflow_id: "workflow_name"
}
}
stub_trigger = stub_request(:post, "https://api.bitrise.io/v0.1/apps/appslug123/builds").
with(body: expected_request_payload).
to_return(body: build_created_response, status: 201)

response = Fastlane::Actions::TriggerBitriseWorkflowAction.run(
app_slug: "appslug123",
workflow: "workflow_name",
wait_for_build: false
)

assert_requested(stub_trigger)
end

it 'calls the Bitrise API with the commit_hash parameter when specified' do
expected_request_payload = {
hook_info: {
type: "bitrise"
},
build_params: {
workflow_id: "workflow_name",
commit_hash: "commit_hash",
commit_message: "build_message"
commit_hash: "commit_hash"
}
}
stub_request(:post, "https://api.bitrise.io/v0.1/apps/appslug123/builds").
with(body: expected_request_payload).
to_return(body: build_created_response, status: 201)
stub_trigger = stub_request(:post, "https://api.bitrise.io/v0.1/apps/appslug123/builds").
with(body: expected_request_payload).
to_return(body: build_created_response, status: 201)

response = Fastlane::Actions::TriggerBitriseWorkflowAction.run(
app_slug: "appslug123",
workflow: "workflow_name",
commit_hash: "commit_hash",
build_message: "build_message",
wait_for_build: false
)

expect(response['status']).to eq("on_hold")
expect(response['build_url']).to eq("http://example.com/builds/100")
expect(response['build_number']).to eq(100)
expect(response['build_slug']).to eq("abc123")
assert_requested(stub_trigger)
end

it 'calls the Bitrise API with the branch parameter when specified' do
it 'calls the Bitrise API with the commit_message parameter when specified' do
expected_request_payload = {
hook_info: {
type: "bitrise"
},
build_params: {
workflow_id: "workflow_name",
branch: "branch_name",
commit_hash: "commit_hash",
commit_message: "build_message"
}
}
Expand All @@ -59,24 +90,21 @@
response = Fastlane::Actions::TriggerBitriseWorkflowAction.run(
app_slug: "appslug123",
workflow: "workflow_name",
branch: "branch_name",
commit_hash: "commit_hash",
build_message: "build_message",
wait_for_build: false
)

assert_requested(stub_trigger)
end

it 'calls the Bitrise API without the branch parameter when specified as empty' do
it 'calls the Bitrise API with the branch parameter when specified' do
expected_request_payload = {
hook_info: {
type: "bitrise"
},
build_params: {
workflow_id: "workflow_name",
commit_hash: "commit_hash",
commit_message: "build_message"
branch: "branch_name"
}
}
stub_trigger = stub_request(:post, "https://api.bitrise.io/v0.1/apps/appslug123/builds").
Expand All @@ -86,9 +114,7 @@
response = Fastlane::Actions::TriggerBitriseWorkflowAction.run(
app_slug: "appslug123",
workflow: "workflow_name",
branch: "",
commit_hash: "commit_hash",
build_message: "build_message",
branch: "branch_name",
wait_for_build: false
)

Expand Down

0 comments on commit 20c1c95

Please sign in to comment.