-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support recording deployments (#389)
* feat(deployments): create endpoint for recording deployments * test: update tests for version decorator and approvals * feat: support specifying an environment when calling the can-i-deploy and matrix endpoints * chore: made sure all db models are loaded * feat: support marking previously deployed version as not currently deployed * feat: add validation to ensure both tag and environment can't be used together to query the matrix * chore: update key name * chore: update deployed versions resource policy * feat: distinguish between "the" version in an environment and "one of the versions" in an environment in the matrix explanation messages * test: add tests to cover the 'multiple versions in an environment' use case * chore: correct seeded environment data
- Loading branch information
Showing
41 changed files
with
793 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Sequel.migration do | ||
change do | ||
create_table(:deployed_versions, charset: 'utf8') do | ||
primary_key :id | ||
String :uuid, null: false | ||
foreign_key :version_id, :versions, null: false | ||
Integer :pacticipant_id, null: false | ||
foreign_key :environment_id, :environments, null: false | ||
Boolean :currently_deployed, null: false | ||
Boolean :replaced_previous_deployed_version, null: false | ||
DateTime :created_at, nullable: false | ||
DateTime :updated_at, nullable: false | ||
DateTime :undeployed_at | ||
index [:uuid], unique: true, name: "deployed_versions_uuid_index" | ||
index [:pacticipant_id, :currently_deployed], name: "deployed_versions_pacticipant_id_currently_deployed_index" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
lib/pact_broker/api/decorators/deployed_version_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'pact_broker/api/decorators/base_decorator' | ||
require 'pact_broker/api/decorators/embedded_version_decorator' | ||
require 'pact_broker/api/decorators/environment_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class DeployedVersionDecorator < BaseDecorator | ||
property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true | ||
property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true | ||
property :currently_deployed, camelize: true | ||
property :replaced_previous_deployed_version, camelize: true | ||
include Timestamps | ||
property :undeployedAt, getter: lambda { |_| undeployed_at ? FormatDateTime.call(undeployed_at) : nil }, writeable: false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lib/pact_broker/api/resources/deployed_versions_for_version.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'pact_broker/api/resources/base_resource' | ||
require 'pact_broker/configuration' | ||
require 'pact_broker/api/decorators/versions_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class DeployedVersionsForVersion < BaseResource | ||
def content_types_accepted | ||
[["application/json", :from_json]] | ||
end | ||
|
||
def content_types_provided | ||
[["application/hal+json"]] | ||
end | ||
|
||
def allowed_methods | ||
["POST", "OPTIONS"] | ||
end | ||
|
||
def resource_exists? | ||
!!version && !!environment | ||
end | ||
|
||
def post_is_create? | ||
true | ||
end | ||
|
||
def create_path | ||
deployed_version_url(OpenStruct.new(uuid: deployed_version_uuid), base_url) | ||
end | ||
|
||
def from_json | ||
@deployed_version = deployed_version_service.create(deployed_version_uuid, version, environment, replaced_previous_deployed_version) | ||
response.body = to_json | ||
end | ||
|
||
def to_json | ||
decorator_class(:deployed_version_decorator).new(deployed_version).to_json(decorator_options) | ||
end | ||
|
||
def policy_name | ||
:'versions::versions' | ||
end | ||
|
||
private | ||
|
||
attr_reader :deployed_version | ||
|
||
def version | ||
@version ||= version_service.find_by_pacticipant_name_and_number(identifier_from_path) | ||
end | ||
|
||
def environment | ||
@environment ||= environment_service.find(environment_uuid) | ||
end | ||
|
||
def environment_uuid | ||
identifier_from_path[:environment_uuid] | ||
end | ||
|
||
def deployed_version_uuid | ||
@deployed_version_uuid ||= deployed_version_service.next_uuid | ||
end | ||
|
||
def replaced_previous_deployed_version | ||
params[:replacedPreviousDeployedVersion] == true | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'pact_broker/repositories/helpers' | ||
|
||
module PactBroker | ||
module Deployments | ||
class DeployedVersion < Sequel::Model | ||
many_to_one :version, :class => "PactBroker::Domain::Version", :key => :version_id, :primary_key => :id | ||
many_to_one :environment, :class => "PactBroker::Deployments::Environment", :key => :environment_id, :primary_key => :id | ||
|
||
dataset_module do | ||
include PactBroker::Repositories::Helpers | ||
|
||
def last_deployed_version(pacticipant, environment) | ||
currently_deployed | ||
.where(pacticipant_id: pacticipant.id) | ||
.where(environment: environment) | ||
.order(Sequel.desc(:created_at), Sequel.desc(:id)) | ||
.first | ||
end | ||
|
||
def currently_deployed | ||
where(currently_deployed: true) | ||
end | ||
|
||
def for_environment_name(environment_name) | ||
where(environment_id: db[:environments].select(:id).where(name: environment_name)) | ||
end | ||
|
||
def for_pacticipant_name(pacticipant_name) | ||
where(pacticipant_id: db[:pacticipants].select(:id).where(name_like(:name, pacticipant_name))) | ||
end | ||
end | ||
|
||
def record_undeployed | ||
update(currently_deployed: false, undeployed_at: Sequel.datetime_class.now) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'pact_broker/deployments/deployed_version' | ||
|
||
module PactBroker | ||
module Deployments | ||
class DeployedVersionService | ||
def self.next_uuid | ||
SecureRandom.uuid | ||
end | ||
|
||
def self.create(uuid, version, environment, replaced_previous_deployed_version) | ||
if replaced_previous_deployed_version | ||
record_previous_version_undeployed(version.pacticipant, environment) | ||
end | ||
DeployedVersion.create( | ||
uuid: uuid, | ||
version: version, | ||
pacticipant_id: version.pacticipant_id, | ||
environment: environment, | ||
currently_deployed: true, | ||
replaced_previous_deployed_version: replaced_previous_deployed_version | ||
) | ||
end | ||
|
||
def self.record_previous_version_undeployed(pacticipant, environment) | ||
DeployedVersion.last_deployed_version(pacticipant, environment)&.record_undeployed | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.