This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathchangeset.rb
More file actions
126 lines (101 loc) · 2.78 KB
/
changeset.rb
File metadata and controls
126 lines (101 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
class Changeset
attr_reader :repo, :previous_commit, :commit
BRANCH_TAGS = ["master", "develop"].freeze
ATTRIBUTE_TABS = %w[files commits pull_requests statuses risks jira_issues].freeze
def initialize(repo, previous_commit, commit)
@repo = repo
@commit = commit
@previous_commit = previous_commit || @commit
end
def github_url
"#{Rails.application.config.samson.github.web_url}/#{repo}/compare/#{commit_range}"
end
def commit_range
"#{previous_commit}...#{commit}"
end
def comparison
@comparison ||= find_comparison
end
def commits
@commits ||= comparison.commits.map { |data| Commit.new(repo, data) }
end
def files
comparison.files
end
def pull_requests
@pull_requests ||= find_pull_requests
end
def risks?
risky_pull_requests.any?
end
def risky_pull_requests
@risky_pull_requests ||= pull_requests.select(&:risky?)
end
def jira_issues
@jira_issues ||= pull_requests.map(&:jira_issues).flatten
end
def authors
commits.map(&:author).compact.uniq
end
def author_names
commits.map(&:author_name).compact.uniq
end
def empty?
@previous_commit == @commit
end
def error
comparison.error
end
private
def find_comparison
if empty?
NullComparison.new(nil)
else
# for branches that frequently change we make sure to always get the correct cache,
# others might get an outdated changeset if they are reviewed with different shas
if BRANCH_TAGS.include?(commit)
@commit = GITHUB.branch(repo, CGI.escape(commit)).commit[:sha]
end
Rails.cache.fetch(cache_key) do
GITHUB.compare(repo, previous_commit, commit)
end
end
rescue Octokit::Error, Faraday::ConnectionFailed => e
NullComparison.new("GitHub: #{e.message.sub("Octokit::", "").underscore.humanize}")
end
def find_pull_requests
numbers = commits.map(&:pull_request_number).compact
numbers.map { |num| PullRequest.find(repo, num) }.
compact.
concat(find_pull_requests_for_branch)
end
def cache_key
[self.class, repo, previous_commit, commit].join('-')
end
def find_pull_requests_for_branch
return [] if not_pr_branch?
org = repo.split("/", 2).first
GITHUB.pull_requests(repo, head: "#{org}:#{commit}").map do |github_pr|
Changeset::PullRequest.new(repo, github_pr)
end
rescue Octokit::Error, Faraday::ConnectionFailed => e
Rails.logger.warn "Failed fetching pull requests for branch #{commit}:\n#{e}"
[]
end
def not_pr_branch?
commit =~ Build::SHA1_REGEX || commit =~ Release::VERSION_REGEX
end
class NullComparison
attr_reader :error
def initialize(error)
@error = error
end
def commits
[]
end
def files
[]
end
end
end