class CodeBuildNotifier::ProjectSummary
Public Instance Methods
Creates entries in a partition key with hardcoded primary key. Within the partition key, there is one record for each github repo. The 'projects' field in that record is a Map data type, keyed off project code, containing the status of the last build for that code.
# File lib/codebuild-notifier/project_summary.rb, line 26 def update return unless whitelisted_branch? updates = project_summary_entry yield updates if block_given? update_item(updates) end
Private Instance Methods
# File lib/codebuild-notifier/project_summary.rb, line 68 def attr_names { '#commit_hash' => 'commit_hash', '#git_repo_url' => 'git_repo_url', '#timestamp' => 'timestamp', '#projects' => 'projects' }.merge(project_code_attr_name) end
# File lib/codebuild-notifier/project_summary.rb, line 89 def attr_values { ':build_status' => status_value, ':commit_hash' => source_id, ':git_repo_url' => current_build.git_repo_url, ':timestamp' => current_build.start_time.to_i } end
# File lib/codebuild-notifier/project_summary.rb, line 47 def new_record? return @new_record if defined?(@new_record) item = dynamo_client.get_item( key: { 'source_id' => source_id, 'version_key' => version_key }, table_name: dynamo_table ).item @new_record = item.nil? end
For an existing record, the project key already exists, so an attribute name is needed to be able to update the nested item path. For a new record, the project code is specified as the root key of the value assigned to the projects field.
# File lib/codebuild-notifier/project_summary.rb, line 81 def project_code_attr_name if new_record? {} else { '#project_code' => current_build.project_code } end end
# File lib/codebuild-notifier/project_summary.rb, line 38 def project_summary_entry { key: { source_id: source_id, version_key: version_key }, expression_attribute_names: attr_names, expression_attribute_values: attr_values, update_expression: update_expression } end
# File lib/codebuild-notifier/project_summary.rb, line 59 def source_id 'project_summary' end
# File lib/codebuild-notifier/project_summary.rb, line 98 def status_map { 'build_id' => current_build.build_id, 'status' => current_build.status, 'timestamp' => current_build.start_time.to_i } end
If a record already exists, we can address the nested item path for the current project directly and just store the updated status. Otherwise, we have to create a new map object in the projects field.
# File lib/codebuild-notifier/project_summary.rb, line 109 def status_value if new_record? { current_build.project_code => status_map } else status_map end end
# File lib/codebuild-notifier/project_summary.rb, line 117 def update_expression projects_key = new_record? ? '#projects' : '#projects.#project_code' 'SET #commit_hash = :commit_hash, ' \ '#timestamp = :timestamp, ' \ '#git_repo_url = :git_repo_url, ' \ "#{projects_key} = :build_status" end
The repo url isn't a good format to use as a URL param.
# File lib/codebuild-notifier/project_summary.rb, line 64 def version_key Digest::MD5.hexdigest(current_build.git_repo_url) end
# File lib/codebuild-notifier/project_summary.rb, line 34 def whitelisted_branch? config.whitelist_branches.include?(current_build.branch_name) end