1
1
require 'pact_broker/api/pact_broker_urls'
2
2
require 'pact_broker/date_helper'
3
3
require 'pact_broker/pacts/create_formatted_diff'
4
+ require 'pact_broker/pacts/sort_verifiable_content'
4
5
require 'pact_broker/repositories'
5
6
require 'yaml'
6
7
@@ -10,13 +11,13 @@ module Pacts
10
11
class Diff
11
12
include PactBroker ::Repositories
12
13
13
- def process ( params )
14
+ def process ( params , comparison_pact_params = nil , options = { } )
14
15
pact = find_pact ( params )
15
- previous_distinct_pact = pact_repository . find_previous_distinct_pact ( pact )
16
+ comparison_pact = comparison_pact_params ? find_pact ( comparison_pact_params ) : pact_repository . find_previous_distinct_pact ( pact )
16
17
17
- if previous_distinct_pact
18
- next_pact = pact_repository . find_next_pact ( previous_distinct_pact )
19
- DiffDecorator . new ( pact , previous_distinct_pact , next_pact , params [ :base_url ] ) . to_text
18
+ if comparison_pact
19
+ next_pact = pact_repository . find_next_pact ( comparison_pact ) || pact
20
+ DiffDecorator . new ( pact , comparison_pact , next_pact , params [ :base_url ] , { raw : options [ :raw ] } ) . to_text
20
21
else
21
22
no_previous_version_message pact
22
23
end
@@ -27,7 +28,8 @@ def process(params)
27
28
def find_pact ( params )
28
29
pact_repository . find_pact ( params . consumer_name ,
29
30
params . consumer_version_number ,
30
- params . provider_name )
31
+ params . provider_name ,
32
+ params . pact_version_sha )
31
33
end
32
34
33
35
def no_previous_version_message ( pact )
@@ -46,11 +48,12 @@ def no_previous_version_message(pact)
46
48
# the latest distinct version content was first created.
47
49
48
50
class DiffDecorator
49
- def initialize ( pact , previous_distinct_pact , next_pact , base_url )
51
+ def initialize ( pact , comparison_pact , next_pact , base_url , options )
50
52
@pact = pact
51
- @previous_distinct_pact = previous_distinct_pact
53
+ @comparison_pact = comparison_pact
52
54
@next_pact = next_pact
53
55
@base_url = base_url
56
+ @options = options
54
57
end
55
58
56
59
def to_text
@@ -59,7 +62,7 @@ def to_text
59
62
60
63
private
61
64
62
- attr_reader :pact , :previous_distinct_pact , :next_pact , :base_url
65
+ attr_reader :pact , :comparison_pact , :next_pact , :base_url , :options
63
66
64
67
def change_date_in_words
65
68
DateHelper . local_date_in_words next_pact . created_at
@@ -70,15 +73,15 @@ def now
70
73
end
71
74
72
75
def header
73
- title = "# Diff between versions #{ previous_distinct_pact . consumer_version_number } and #{ pact . consumer_version_number } of the pact between #{ pact . consumer . name } and #{ pact . provider . name } "
76
+ title = "# Diff between versions #{ comparison_pact . consumer_version_number } and #{ pact . consumer_version_number } of the pact between #{ pact . consumer . name } and #{ pact . provider . name } "
74
77
description = "The following changes were made #{ change_date_ago_in_words } ago (#{ change_date_in_words } )"
75
78
76
79
title + "\n \n " + description
77
80
end
78
81
79
82
def links
80
83
self_url = PactBroker ::Api ::PactBrokerUrls . pact_url ( base_url , pact )
81
- previous_distinct_url = PactBroker ::Api ::PactBrokerUrls . pact_url ( base_url , previous_distinct_pact )
84
+ previous_distinct_url = PactBroker ::Api ::PactBrokerUrls . pact_url ( base_url , comparison_pact )
82
85
83
86
links = {
84
87
"current-pact-version" => {
@@ -88,20 +91,28 @@ def links
88
91
} ,
89
92
"previous-distinct-pact-version" => {
90
93
"title" => "Pact" ,
91
- "name" => previous_distinct_pact . name ,
94
+ "name" => comparison_pact . name ,
92
95
"href" => previous_distinct_url
93
96
}
94
97
}
95
98
"## Links\n " + YAML . dump ( links ) . gsub ( /---/ , '' )
96
99
end
97
100
98
101
def diff
99
- CreateFormattedDiff . ( pact . json_content , previous_distinct_pact . json_content )
102
+ CreateFormattedDiff . ( prepare_content ( pact . json_content ) , prepare_content ( comparison_pact . json_content ) )
100
103
end
101
104
102
105
def change_date_ago_in_words
103
106
DateHelper . distance_of_time_in_words next_pact . created_at , now
104
107
end
108
+
109
+ def prepare_content json_content
110
+ if options [ :raw ]
111
+ json_content
112
+ else
113
+ PactBroker ::Pacts ::SortVerifiableContent . call ( json_content )
114
+ end
115
+ end
105
116
end
106
117
end
107
118
end
0 commit comments