mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
Various GitHub Onebox improvements (#13163)
* FIX: Improve GitHub folder regexp in Onebox It used to match any GitHub URL that was not matched by the other GitHub Oneboxes and it did not do a good job at handling those. With this change, the generic Onebox will handle the remaining URLs. * FEATURE: Add Onebox for GitHub Actions * FEATURE: Add Onebox for PR check runs * FIX: Remove image from GitHub folder Oneboxes It is a generic, auto-generated image which does not provide any value. * DEV: Add tests * FIX: Strip HTML comments from PR body
This commit is contained in:
87
lib/onebox/engine/github_actions_onebox.rb
Normal file
87
lib/onebox/engine/github_actions_onebox.rb
Normal file
@ -0,0 +1,87 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative '../mixins/github_body'
|
||||
|
||||
module Onebox
|
||||
module Engine
|
||||
class GithubActionsOnebox
|
||||
include Engine
|
||||
include LayoutSupport
|
||||
include JSON
|
||||
|
||||
matches_regexp(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?github\.com\/(?<org>.+)\/(?<repo>.+)\/(actions\/runs\/[[:digit:]]+|pull\/[[:digit:]]*\/checks\?check_run_id=[[:digit:]]+)/)
|
||||
always_https
|
||||
|
||||
def url
|
||||
if type == :actions_run
|
||||
"https://api.github.com/repos/#{match[:org]}/#{match[:repo]}/actions/runs/#{match[:run_id]}"
|
||||
elsif type == :pr_run
|
||||
"https://api.github.com/repos/#{match[:org]}/#{match[:repo]}/check-runs/#{match[:check_run_id]}"
|
||||
end
|
||||
end
|
||||
|
||||
def self.priority
|
||||
90 # overlaps with GithubPullRequestOnebox
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def match_url
|
||||
return if defined?(@match) && defined?(@type)
|
||||
|
||||
if match = @url.match(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?github\.com\/(?<org>.+)\/(?<repo>.+)\/actions\/runs\/(?<run_id>[[:digit:]]+)/)
|
||||
@match = match
|
||||
@type = :actions_run
|
||||
end
|
||||
|
||||
if match = @url.match(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?github\.com\/(?<org>.+)\/(?<repo>.+)\/pull\/(?<pr_id>[[:digit:]]*)\/checks\?check_run_id=(?<check_run_id>[[:digit:]]+)/)
|
||||
@match = match
|
||||
@type = :pr_run
|
||||
end
|
||||
end
|
||||
|
||||
def match
|
||||
return @match if defined?(@match)
|
||||
|
||||
match_url
|
||||
@match
|
||||
end
|
||||
|
||||
def type
|
||||
return @type if defined?(@type)
|
||||
|
||||
match_url
|
||||
@type
|
||||
end
|
||||
|
||||
def data
|
||||
status = "unknown"
|
||||
if raw["status"] == "completed"
|
||||
if raw["conclusion"] == "success"
|
||||
status = "success"
|
||||
elsif raw["conclusion"] == "failure"
|
||||
status = "failure"
|
||||
elsif raw["conclusion"] == "cancelled"
|
||||
end
|
||||
elsif raw["status"] == "in_progress"
|
||||
status = "pending"
|
||||
end
|
||||
|
||||
title = if type == :actions_run
|
||||
raw["head_commit"]["message"].lines.first
|
||||
elsif type == :pr_run
|
||||
pr_url = "https://api.github.com/repos/#{match[:org]}/#{match[:repo]}/pulls/#{match[:pr_id]}"
|
||||
::MultiJson.load(URI.open(pr_url, read_timeout: timeout))["title"]
|
||||
end
|
||||
|
||||
{
|
||||
link: @url,
|
||||
title: title,
|
||||
name: raw["name"],
|
||||
run_number: raw["run_number"],
|
||||
status => true,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -7,14 +7,9 @@ module Onebox
|
||||
include StandardEmbed
|
||||
include LayoutSupport
|
||||
|
||||
matches_regexp(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?(github)\.com[\:\d]*(\/[^\/]+){2}/)
|
||||
matches_regexp(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?(github)\.com[\:\d]*(\/[^\/]+){2}\/tree/)
|
||||
always_https
|
||||
|
||||
def self.priority
|
||||
# This engine should have lower priority than the other Github engines
|
||||
150
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def data
|
||||
@ -46,7 +41,6 @@ module Onebox
|
||||
|
||||
{
|
||||
link: url,
|
||||
image: og.image,
|
||||
title: Onebox::Helpers.truncate(title, 250),
|
||||
path: display_path,
|
||||
description: display_description,
|
||||
|
Reference in New Issue
Block a user