Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

List statuses in release details #2922

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/models/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Changeset
attr_reader :repo, :previous_commit, :commit
BRANCH_TAGS = ["master", "develop"].freeze
ATTRIBUTE_TABS = %w[files commits pull_requests risks jira_issues].freeze
ATTRIBUTE_TABS = %w[files commits pull_requests statuses risks jira_issues].freeze

def initialize(repo, previous_commit, commit)
@repo = repo
Expand Down Expand Up @@ -34,6 +34,10 @@ def pull_requests
@pull_requests ||= find_pull_requests
end

def github_status
@github_status ||= GithubStatus.for_reference(repo, commit)
end

def risks?
risky_pull_requests.any?
end
Expand Down
28 changes: 15 additions & 13 deletions app/models/github_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@ def initialize(state, statuses)
@statuses = statuses
end

def self.fetch(release)
repo = release.project.repository_path
ref = release.commit

# Base the cache key on the Release, so that an update to it effectively
# clears the cache.
cache_key = [name, release]

response = Rails.cache.read(cache_key)

def self.for_reference(repo, ref)
# Fetch the data if the cache returned nil.
response ||= begin
GITHUB.combined_status(repo, ref)
Expand All @@ -57,12 +48,23 @@ def self.fetch(release)
Status.new(context, statuses.max_by { |status| status.created_at.to_i })
end

new(response.state, statuses)
end

def self.for_release(release)
# Base the cache key on the Release, so that an update to it effectively
# clears the cache.
cache_key = [name, release, "v2"]

status = Rails.cache.read(cache_key)
status ||= for_reference(release.project.repository_path, release.commit)

# Don't cache pending statuses, since we expect updates soon.
unless statuses.any?(&:pending?)
Rails.cache.write(cache_key, response, expires_in: 1.hour)
unless status.statuses.any?(&:pending?)
Rails.cache.write(cache_key, status, expires_in: 1.hour)
end

new(response.state, statuses)
status
end

def success?
Expand Down
2 changes: 1 addition & 1 deletion app/models/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def to_param
end

def github_status
@github_status ||= GithubStatus.fetch(self)
@github_status ||= GithubStatus.for_release(self)
end

def self.find_by_param!(version)
Expand Down
3 changes: 3 additions & 0 deletions app/views/changeset/_statuses.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% changeset.github_status.statuses.each do |status| %>
<%= github_commit_status_icon(status.state) %> <b><%= status.context %>:</b> <%= link_to status.description, status.url %><br/>
<% end %>
5 changes: 5 additions & 0 deletions app/views/releases/row_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<li><a href="#pull_requests-<%= @release.version %>" data-toggle="tab">Pull Requests</a></li>
<li class="active"><a href="#commits-<%= @release.version %>" data-toggle="tab">Commits</a></li>
<li><a href="#files-<%= @release.version %>" data-toggle="tab">Files</a></li>
<li><a href="#statuses-<%= @release.version %>" data-toggle="tab">Statuses</a></li>
<li><a href="#risks-<%= @release.version %>" data-toggle="tab">Risks</a></li>
<li><a href="#jira_issues-<%= @release.version %>" data-toggle="tab">JIRA Issues</a></li>
</ul>
Expand All @@ -26,6 +27,10 @@
<%= render 'changeset/files', changeset: @changeset %>
</div>

<div class="tab-pane" id="statuses-<%= @release.version %>">
<%= render 'changeset/statuses', changeset: @changeset %>
</div>

<div class="tab-pane" id="risks-<%= @release.version %>">
<%= render 'changeset/risks', changeset: @changeset, type: "release" %>
</div>
Expand Down
4 changes: 1 addition & 3 deletions app/views/releases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

<legend>Commit Status</legend>
<p>
<% @release.github_status.statuses.each do |status| %>
<%= github_commit_status_icon(status.state) %> <b><%= status.context %>:</b> <%= link_to status.description, status.url %><br/>
<% end %>
<%= render "changeset/statuses", changeset: @changeset %>
</p>

<% if @changeset.empty? %>
Expand Down
24 changes: 21 additions & 3 deletions config/brakeman.ignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
{
"ignored_warnings": [

{
"warning_type": "Cross-Site Scripting",
"warning_code": 4,
"fingerprint": "50350274f9c62a91f27562722e2191833e489c5eb093c411425b2472b6b7dbf2",
"check_name": "LinkToHref",
"message": "Potentially unsafe model attribute in link_to href",
"file": "app/views/changeset/_statuses.html.erb",
"line": 2,
"link": "https://brakemanscanner.org/docs/warning_types/link_to_href",
"code": "link_to((Unresolved Model).new.description, (Unresolved Model).new.url)",
"render_path": [{"type":"controller","class":"ReleasesController","method":"show","line":10,"file":"app/controllers/releases_controller.rb"},{"type":"template","name":"releases/row_content","line":31,"file":"app/views/releases/row_content.html.erb"}],
"location": {
"type": "template",
"template": "changeset/_statuses"
},
"user_input": "(Unresolved Model).new.url",
"confidence": "Weak",
"note": ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not should be something like Linking to github status url

}
],
"updated": "2018-01-12 08:38:04 -0800",
"brakeman_version": "4.1.1"
"updated": "2018-09-12 16:04:57 +0200",
"brakeman_version": "4.3.1"
}
4 changes: 2 additions & 2 deletions test/models/github_status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:release) { stub("release", project: project, commit: ref) }

describe "#state" do
let(:status) { GithubStatus.fetch(release) }
let(:status) { GithubStatus.for_release(release) }

it "returns `missing` if there's no response from Github" do
stub_api({}, 401)
Expand All @@ -24,7 +24,7 @@
end

describe "#statuses" do
let(:status) { GithubStatus.fetch(release) }
let(:status) { GithubStatus.for_release(release) }

it "returns a single status per context" do
# The most recent status is used.
Expand Down