Skip to content

Commit 1158bd9

Browse files
mattst88Priya5
authored andcommitted
widgets: Fix some issues (#1900)
* widgets: correct OH_Partner_frame.png dimensions, metadata, and test fixtures OH_Partner_frame.png had a 1px transparent row at y=0, so all visible content started at y=1. This caused the rendered Detailed widget to appear shifted down with an empty row at the top and the bottom pixel clipped off. The Detailed widget model also reported width: 230 but the frame image is 232px wide, causing the HTML embed <img width="230"> to clip 2px off the right edge. Test fixtures regenerated to match the corrected frame. * widgets: correct Tiny widget height to match profile_tiny.png (15 → 16) profile_tiny.png is 80×16 but the Tiny widget model reported height: 15, causing the HTML embed <img height="15"> to scale the image down by 1px vertically. * widgets: add PNG output format support Widget image generation produces PNG internally — the pipeline opens OH_Partner_frame.png, composites text and kudos onto it, and returns the result via MiniMagick's to_blob, which emits PNG bytes. Previously those bytes were served as image/gif regardless of what was requested, which is a lie about the Content-Type. Add proper ?format=png support by renaming render_image_for_gif_format to render_image_for_image_format (and the "not supported" equivalent) across all five widget controllers. PNG requests now receive Content-Type: image/png with the same underlying bytes — no conversion needed since the data is already PNG. GIF requests preserve existing behavior for backward compatibility. * widgets: flatten transparent background to white OH_Partner_frame.png has an alpha channel — the right-side content area where name, commits, and kudos are composited was transparent rather than opaque white. The kudos laurel badge composited on top has a white background, making the two visually inconsistent: the laurel shows white while surrounding areas are transparent, appearing as a checkerboard in image viewers or taking on the page background color in browsers. Add image.flatten to setup_blank in badge_helper.rb. flatten fills transparent pixels with the current background color, which is already set to white on the line above, producing a fully opaque image before any compositing begins. setup_blank is shared by all three badge types (Account, Partner, Thin), so all affected test fixtures are regenerated. * positions: use must_include instead of must_match for org name Faker-generated org names with special characters (e.g. apostrophes) create broken regexes when passed as strings to must_match. Use must_include instead, which does plain substring matching.
1 parent 7411042 commit 1158bd9

22 files changed

Lines changed: 33 additions & 21 deletions
-750 Bytes
Loading

app/controllers/account_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class AccountWidgetsController < WidgetsController
44
before_action :set_account
5-
before_action :render_image_for_gif_format
5+
before_action :render_image_for_image_format
66
before_action :account_context, only: :index
77

88
def index

app/controllers/organization_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class OrganizationWidgetsController < WidgetsController
44
before_action :set_organization
5-
before_action :render_not_supported_for_gif_format
5+
before_action :render_not_supported_for_image_format
66
before_action :render_iframe_for_js_format
77
before_action :organization_context, only: :index
88

app/controllers/project_widgets_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class ProjectWidgetsController < WidgetsController
44
helper :Projects, :Analyses
55
before_action :set_project
6-
before_action :render_image_for_gif_format, only: %i[partner_badge thin_badge]
7-
before_action :render_not_supported_for_gif_format, except: %i[partner_badge thin_badge index]
6+
before_action :render_image_for_image_format, only: %i[partner_badge thin_badge]
7+
before_action :render_not_supported_for_image_format, except: %i[partner_badge thin_badge index]
88
before_action :render_iframe_for_js_format
99
before_action :project_context, only: :index
1010

app/controllers/stack_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class StackWidgetsController < WidgetsController
44
before_action :set_widget
55
before_action :set_stack_and_account
6-
before_action :render_not_supported_for_gif_format
6+
before_action :render_not_supported_for_image_format
77
before_action :render_iframe_for_js_format
88
before_action :account_context, only: :index
99

app/controllers/widgets_controller.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,30 @@ def set_widget
2727
.new(permitted_params)
2828
end
2929

30-
def render_image_for_gif_format
31-
return unless request.format.gif?
30+
def render_image_for_image_format
31+
return unless request.format.gif? || request.format.png?
3232

33+
send_data(cached_widget_image, disposition: 'inline', type: image_mime_type, filename: image_filename, status: 200)
34+
end
35+
36+
def cached_widget_image
3337
cache_key = "#{@widget.parent.class.name}/#{@widget.parent.id}/#{@widget.name}"
34-
image = Rails.cache.fetch(cache_key, expires_in: 4.hours) { @widget.image }
35-
send_data(image, disposition: 'inline', type: 'image/gif', filename: 'widget.gif', status: 200)
38+
Rails.cache.fetch(cache_key, expires_in: 4.hours) { @widget.image }
3639
end
3740

38-
def render_not_supported_for_gif_format
39-
return unless request.format.gif?
41+
def render_not_supported_for_image_format
42+
return unless request.format.gif? || request.format.png?
4043

4144
image = Rails.cache.fetch('not_supported_widget') { WidgetBadge::Thin.create([text: 'Not supported']) }
42-
send_data(image, disposition: 'inline', type: 'image/gif', filename: 'widget.gif', status: 406)
45+
send_data(image, disposition: 'inline', type: image_mime_type, filename: image_filename, status: 406)
46+
end
47+
48+
def image_mime_type
49+
request.format.png? ? 'image/png' : 'image/gif'
50+
end
51+
52+
def image_filename
53+
request.format.png? ? 'widget.png' : 'widget.gif'
4354
end
4455

4556
def render_iframe_for_js_format

app/models/widget/account_widget/detailed.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Widget::AccountWidget::Detailed < Widget::AccountWidget
44
def width
5-
230
5+
232
66
end
77

88
def height

app/models/widget/account_widget/tiny.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Widget::AccountWidget::Tiny < Widget::AccountWidget
44
def height
5-
15
5+
16
66
end
77

88
def width

lib/widget_badge/badge_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module ClassMethods
1515
def setup_blank
1616
image = MiniMagick::Image.open(IMAGE_DIR.join('OH_Partner_frame.png'))
1717
image.background('white')
18+
image.flatten
1819
image
1920
end
2021

test/controllers/positions_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class PositionsControllerTest < ActionController::TestCase
332332
assert_response :success
333333
language = position.name_fact.primary_language.nice_name
334334
_(unescaped_response_body).must_match "1\nCommit\n</a>in mostly\n#{language}"
335-
_(unescaped_response_body).must_match position.name_fact.analysis.project.organization.name
335+
_(unescaped_response_body).must_include position.name_fact.analysis.project.organization.name
336336
assert_select 'div#all_projects.chart-with-data[data-value]', 1
337337
end
338338

0 commit comments

Comments
 (0)