Class: RailsErrorDashboard::Services::GithubLinkGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/github_link_generator.rb

Overview

Generates links to source code on GitHub, GitLab, or Bitbucket Supports commit SHA, branch, and tag references

Examples:

generator = GithubLinkGenerator.new(
  repository_url: "https://github.com/user/repo",
  file_path: "app/models/user.rb",
  line_number: 42,
  commit_sha: "abc123def456"
)
generator.generate_link
# => "https://github.com/user/repo/blob/abc123def456/app/models/user.rb#L42"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_url:, file_path:, line_number:, commit_sha: nil, branch: nil) ⇒ GithubLinkGenerator

Initialize a new link generator

Parameters:

  • repository_url (String)

    Base repository URL (GitHub, GitLab, Bitbucket)

  • file_path (String)

    Relative path to file from repository root

  • line_number (Integer)

    Line number to link to

  • commit_sha (String, nil) (defaults to: nil)

    Git commit SHA (recommended for accuracy)

  • branch (String, nil) (defaults to: nil)

    Branch name (fallback if no commit SHA)



27
28
29
30
31
32
33
34
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 27

def initialize(repository_url:, file_path:, line_number:, commit_sha: nil, branch: nil)
  @repository_url = repository_url
  @file_path = file_path
  @line_number = line_number.to_i
  @commit_sha = commit_sha
  @branch = branch || "main"
  @error = nil
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def branch
  @branch
end

#commit_shaObject (readonly)

Returns the value of attribute commit_sha.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def commit_sha
  @commit_sha
end

#errorObject (readonly)

Returns the value of attribute error.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def error
  @error
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def file_path
  @file_path
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def line_number
  @line_number
end

#repository_urlObject (readonly)

Returns the value of attribute repository_url.



18
19
20
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 18

def repository_url
  @repository_url
end

Instance Method Details

Generate a link to the source file on the repository host

Returns:

  • (String, nil)

    URL to the source file or nil if invalid



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
# File 'lib/rails_error_dashboard/services/github_link_generator.rb', line 39

def generate_link
  return nil if repository_url.blank? || file_path.blank?

  # Normalize repository URL
  normalized_repo = normalize_repository_url

  # Determine reference (commit SHA or branch)
  reference = determine_reference

  # Generate link based on repository type
  case detect_repository_type
  when :github
    generate_github_link(normalized_repo, reference)
  when :gitlab
    generate_gitlab_link(normalized_repo, reference)
  when :bitbucket
    generate_bitbucket_link(normalized_repo, reference)
  else
    @error = "Unsupported repository type"
    nil
  end
rescue StandardError => e
  @error = "Error generating link: #{e.message}"
  RailsErrorDashboard::Logger.error("GithubLinkGenerator error for #{repository_url} - #{e.message}")
  nil
end