From 5c1b49f494f07bf37ba3c60f3b9f70d1842d8b60 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Fri, 4 Dec 2015 16:23:21 +0200 Subject: Add added, modified and removed properties to commit object in webhook --- app/models/commit.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'app/models/commit.rb') diff --git a/app/models/commit.rb b/app/models/commit.rb index 492f6be1ce3..912b4dedf51 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -146,7 +146,10 @@ class Commit author: { name: author_name, email: author_email - } + }, + added: repo_changes[:added], + modified: repo_changes[:modified], + removed: repo_changes[:removed] } end @@ -196,4 +199,23 @@ class Commit def status ci_commit.try(:status) || :not_found end + + private + + def repo_changes + changes = { added: [], modified: [], removed: [] } + + diffs.each do |diff| + case true + when diff.deleted_file + changes[:removed] << diff.old_path + when diff.renamed_file, diff.new_file + changes[:added] << diff.new_path + else + changes[:modified] << diff.new_path + end + end + + changes + end end -- cgit v1.2.1 From 5df2c4419c5019b5003ddfa6adb59c84c3d9910c Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 7 Dec 2015 14:11:15 +0200 Subject: fox specs --- app/models/commit.rb | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'app/models/commit.rb') diff --git a/app/models/commit.rb b/app/models/commit.rb index 912b4dedf51..fecadfeec8e 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -135,10 +135,10 @@ class Commit description.present? end - def hook_attrs + def hook_attrs(with_changed_files = false) path_with_namespace = project.path_with_namespace - { + data = { id: id, message: safe_message, timestamp: committed_date.xmlschema, @@ -146,11 +146,18 @@ class Commit author: { name: author_name, email: author_email - }, - added: repo_changes[:added], - modified: repo_changes[:modified], - removed: repo_changes[:removed] + } } + + if with_changed_files + data.merge!({ + added: repo_changes[:added], + modified: repo_changes[:modified], + removed: repo_changes[:removed] + }) + end + + data end # Discover issues should be closed when this commit is pushed to a project's @@ -205,14 +212,16 @@ class Commit def repo_changes changes = { added: [], modified: [], removed: [] } - diffs.each do |diff| - case true - when diff.deleted_file - changes[:removed] << diff.old_path - when diff.renamed_file, diff.new_file - changes[:added] << diff.new_path - else - changes[:modified] << diff.new_path + if diffs.any? + diffs.each do |diff| + case true + when diff.deleted_file + changes[:removed] << diff.old_path + when diff.renamed_file, diff.new_file + changes[:added] << diff.new_path + else + changes[:modified] << diff.new_path + end end end -- cgit v1.2.1 From 3c97cbc74cf87856ed7b1af197358d4e3adb1240 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 7 Dec 2015 15:13:06 +0200 Subject: fixes after review --- app/models/commit.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'app/models/commit.rb') diff --git a/app/models/commit.rb b/app/models/commit.rb index fecadfeec8e..14883c96f5f 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -135,7 +135,7 @@ class Commit description.present? end - def hook_attrs(with_changed_files = false) + def hook_attrs(with_changed_files: false) path_with_namespace = project.path_with_namespace data = { @@ -150,11 +150,7 @@ class Commit } if with_changed_files - data.merge!({ - added: repo_changes[:added], - modified: repo_changes[:modified], - removed: repo_changes[:removed] - }) + data.merge!(repo_changes) end data @@ -212,16 +208,13 @@ class Commit def repo_changes changes = { added: [], modified: [], removed: [] } - if diffs.any? - diffs.each do |diff| - case true - when diff.deleted_file - changes[:removed] << diff.old_path - when diff.renamed_file, diff.new_file - changes[:added] << diff.new_path - else - changes[:modified] << diff.new_path - end + diffs.each do |diff| + if diff.deleted_file + changes[:removed] << diff.old_path + elsif diff.renamed_file || diff.new_file + changes[:added] << diff.new_path + else + changes[:modified] << diff.new_path end end -- cgit v1.2.1