Skip to content

Commit

Permalink
Merge pull request #17 from NoRedInk/extract-dependencies
Browse files Browse the repository at this point in the history
Release 0.8.0
  • Loading branch information
dgtized authored Feb 7, 2019
2 parents 357624d + 01b8322 commit 22c2082
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 114 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Style/SingleLineBlockParams:
Style/FormatString:
Enabled: false

Style/FormatStringToken:
Enabled: false

Style/WordArray:
Enabled: false

Metrics/CyclomaticComplexity:
Max: 10

Expand Down
19 changes: 5 additions & 14 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-01-22 16:22:58 -0600 using RuboCop version 0.60.0.
# on 2019-02-07 14:17:24 -0600 using RuboCop version 0.60.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Offense count: 4
Metrics/AbcSize:
Max: 52
Max: 51

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 124
Max: 125

# Offense count: 6
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 41
Max: 40

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 9

# Offense count: 12
# Configuration parameters: EnforcedStyle.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
Exclude:
- 'exe/deploy-complexity.rb'
- 'lib/deploy_complexity/deploy.rb'
- 'lib/deploy_complexity/output_formatter.rb'
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
deploy-complexity (0.7.0)
deploy-complexity (0.8.0)
octokit (~> 4.0)
slack-notifier (~> 2.3.2)
values (~> 1.8.0)
Expand Down
46 changes: 12 additions & 34 deletions lib/deploy_complexity/changed_dependencies.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'deploy_complexity/dependency'

module DeployComplexity
# Detects and formats changes in dependencies
# This is the parent class - each type of dependency file should implement
Expand All @@ -12,11 +14,7 @@ def initialize(file:, old:, new:)
end

def changes
[
format_dependencies("Added", added_dependencies),
format_dependencies("Removed", removed_dependencies),
format_updated_dependencies(updated_dependencies)
].flatten
dependencies.reject(&:unchanged?)
end

private
Expand All @@ -28,35 +26,15 @@ def parse_dependencies(_file)
{}
end

def added_dependencies
@new_dependencies.dup.delete_if { |package, _| @old_dependencies.key?(package) }
end

def removed_dependencies
@old_dependencies.dup.delete_if { |package, _| @new_dependencies.key?(package) }
end

def updated_dependencies
@new_dependencies.each_with_object({}) do |(package, new_version), changed_dependencies|
next if @old_dependencies[package].nil?
next if @old_dependencies[package] == new_version

changed_dependencies[package] = {
old: @old_dependencies[package],
new: new_version
}
end
end

def format_dependencies(label, dependencies)
dependencies.map do |(package, version)|
"#{label} #{package}: #{version} (#{@file})"
end
end

def format_updated_dependencies(dependencies)
dependencies.map do |(package, versions)|
"Updated #{package}: #{versions.fetch(:old)} -> #{versions.fetch(:new)} (#{@file})"
def dependencies
packages = (@new_dependencies.keys + @old_dependencies.keys).uniq.sort
packages.map do |package|
DeployComplexity::Dependency.with(
package: package,
current: @new_dependencies.fetch(package, nil),
previous: @old_dependencies.fetch(package, nil),
file: @file
)
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/deploy_complexity/changed_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ module DeployComplexity
# Parses git shortstat output
# `git diff --name-only v0.4.0...v0.5.0`
class ChangedFiles
def initialize(names, versioned_url)
def initialize(names)
@names = names.split(/\n/)
@versioned_url = versioned_url
end

def migrations
@names.grep(%r{^db/migrate}).map do |line|
@versioned_url + line.chomp
end
@names.grep(%r{^db/migrate}).map(&:chomp)
end

def elm_packages
Expand Down
37 changes: 37 additions & 0 deletions lib/deploy_complexity/dependency.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'values'

module DeployComplexity
# Represents a potential change in a package version for a dependency file
class Dependency < Value.new(:package, :current, :previous, :file)
def change
if current.nil?
:removed
elsif previous.nil?
:added
elsif current != previous
:updated
else
:unchanged
end
end

def unchanged?
change == :unchanged
end

def to_s
case change
when :removed
"Removed %s (%s)" % [package, file]
when :added
"Added %s: %s (%s)" % [package, current, file]
when :updated
"Updated %s: %s -> %s (%s)" % [package, previous, current, file]
else
"Unchanged %s (%s)" % [package, file]
end
end
end
end
16 changes: 6 additions & 10 deletions lib/deploy_complexity/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'deploy_complexity/changed_javascript_packages'
require 'deploy_complexity/changed_ruby_gems'
require 'deploy_complexity/git'
require 'deploy_complexity/github'

module DeployComplexity
# The main module for deploy complexity that parses output from git
Expand All @@ -24,7 +25,7 @@ def initialize(base, to, options)
end

def generate
gh_url = options[:gh_url]
github = Github.new(options[:gh_url])
dirstat = options[:dirstat]
stat = options[:stat]

Expand All @@ -40,8 +41,7 @@ def generate

shortstat = `git diff --shortstat --summary #{range}`.split(/\n/)
names_only = `git diff --name-only #{range}`
versioned_url = "#{gh_url}/blob/#{Git.safe_name(to)}/"
changed_files = DeployComplexity::ChangedFiles.new(names_only, versioned_url)
changed_files = DeployComplexity::ChangedFiles.new(names_only)

dirstat = `git diff --dirstat=lines,cumulative #{range}` if dirstat
# TODO: investigate summarizing language / spec content based on file suffix,
Expand All @@ -54,13 +54,13 @@ def generate
base: base,
revision: revision,
commits: commits,
pull_requests: pull_requests(merges, gh_url),
pull_requests: pull_requests(merges),
merges: merges,
shortstat: shortstat,
dirstat: dirstat,
stat: stat,
time_delta: time_delta,
gh_url: gh_url,
github: github,
base_reference: reference(base),
to_reference: reference(to),
migrations: changed_files.migrations,
Expand Down Expand Up @@ -111,8 +111,6 @@ def parse_when(tag)
end
end

COMPARE_FORMAT = "%s/compare/%s...%s"

def time_between_deploys(from, to)
deploy_time = parse_when(to)
last_time = parse_when(from)
Expand Down Expand Up @@ -140,18 +138,16 @@ def reference(name)
end

# TODO: consider moving this to a separate parser and testing
def pull_requests(merges, gh_url)
def pull_requests(merges)
merges.map do |line|
line.match(/pull request #(\d+) from (.*)$/) do |m|
{
gh_url: gh_url,
pr_number: m[1].to_i,
joiner: "-",
name: Git.safe_name(m[2])
}
end || line.match(/(\w+)\s+(.*)\(\#(\d+)\)/) do |m|
{
gh_url: gh_url,
pr_number: m[3].to_i,
joiner: "S",
name: m[2]
Expand Down
23 changes: 23 additions & 0 deletions lib/deploy_complexity/github.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module DeployComplexity
# Generate Github urls for revisions, comparisons, etc
class Github
def initialize(gh_url)
@project_url = gh_url
end

def blob(version, path = nil)
base = "%s/blob/%s/" % [@project_url, version]
path ? base + path : base
end

def compare(base, to)
"%s/compare/%s...%s" % [@project_url, base, to]
end

def pull_request(number)
"%s/pull/%d" % [@project_url, number]
end
end
end
18 changes: 10 additions & 8 deletions lib/deploy_complexity/output_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'values'
require 'deploy_complexity/github'

module DeployComplexity
Attachment = Value.new(:title, :text, :color)
Expand All @@ -21,7 +22,7 @@ class OutputFormatter <
:dirstat,
:stat,
:time_delta,
:gh_url,
:github,
:migrations,
:elm_packages,
:ruby_dependencies,
Expand All @@ -47,7 +48,7 @@ def text
text << empty_commit_message
else
text << summary_stats
text << compare_url
text << compare_link
text << shortstats
end

Expand Down Expand Up @@ -80,6 +81,10 @@ def header
"Deploy tag #{to} [#{revision}]"
end

def compare_link
github.compare(base_reference, to_reference)
end

def summary_stats
"%d pull requests of %d merges, %d commits %s" %
[pull_requests.count, merges.count, commits.count, time_delta]
Expand All @@ -91,14 +96,11 @@ def shortstats
shortstat.first.strip
end

def compare_url
"%s/compare/%s...%s" % [gh_url, base_reference, to_reference]
end

def migration_attachment
links = migrations.map { |migration| github.blob(revision, migration) }
Attachment.with(
title: "Migrations",
text: migrations.join("\n"),
text: links.join("\n"),
color: "#E6E6FA"
)
end
Expand Down Expand Up @@ -131,7 +133,7 @@ def pull_request_attachment
Attachment.with(
title: "Pull Requests",
text: pull_requests.map do |pr|
url = "#{pr.fetch(:gh_url)}/pull/#{pr.fetch(:pr_number)}"
url = github.pull_request(pr.fetch(:pr_number))
"#{url} #{pr.fetch(:joiner)} #{pr.fetch(:name)}"
end.join("\n"),
color: "#FFCCB6"
Expand Down
18 changes: 17 additions & 1 deletion lib/deploy_complexity/slack_output_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "deploy_complexity/output_formatter"
require "deploy_complexity/github"

module DeployComplexity
# Formats deploy complexity output for slack
Expand Down Expand Up @@ -33,12 +34,27 @@ def header
"*#{super}*"
end

def compare_link
"<%s|%s...%s>" % [super, base_reference, to_reference]
end

def migration_attachment
links = migrations.map do |migration|
"<%s|%s>" % [github.blob(revision, migration), migration]
end
Attachment.with(
title: "Migrations",
text: links.join("\n"),
color: "#E6E6FA"
)
end

# Override parent by fancy formatting links
def pull_request_attachment
Attachment.with(
title: "Pull Requests",
text: pull_requests.map do |pr|
url = "#{pr.fetch(:gh_url)}/pull/#{pr.fetch(:pr_number)}"
url = github.pull_request(pr.fetch(:pr_number))
"<#{url}|#{pr.fetch(:pr_number)}> - #{pr.fetch(:name)}"
end.join("\n"),
color: "#FFCCB6"
Expand Down
2 changes: 1 addition & 1 deletion lib/deploy_complexity/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DeployComplexity
VERSION = "0.7.0"
VERSION = "0.8.0"
end
Loading

0 comments on commit 22c2082

Please sign in to comment.