Module: Commiti::PrOpener

Defined in:
lib/services/git/pr/pr_opener.rb

Constant Summary collapse

SCP_REMOTE =
%r{\A(?<user>[^@]+)@(?<host>[^:\s/]+):(?<path>[^\s]+)\z}
MAX_PREFILLED_URL_LENGTH =
1800
MAX_PREFILLED_TITLE_LENGTH =
120

Class Method Summary collapse

Class Method Details

.compare_url(origin_url:, base_branch:, head_branch:, title:, body:) ⇒ Object



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
# File 'lib/services/git/pr/pr_opener.rb', line 11

def self.compare_url(origin_url:, base_branch:, head_branch:, title:, body:)
  remote = extract_remote_info(origin_url)
  raise 'Supported providers for browser PR opening are GitHub, GitLab, and GitBucket.' if remote.nil?

  full_url = prefilled_url(
    remote: remote,
    base_branch: base_branch,
    head_branch: head_branch,
    title: title,
    body: body,
    include_title: true,
    include_body: true
  )
  return full_url if full_url.length <= MAX_PREFILLED_URL_LENGTH

  without_body = prefilled_url(
    remote: remote,
    base_branch: base_branch,
    head_branch: head_branch,
    title: title,
    body: body,
    include_title: true,
    include_body: false
  )
  return without_body if without_body.length <= MAX_PREFILLED_URL_LENGTH

  prefilled_url(
    remote: remote,
    base_branch: base_branch,
    head_branch: head_branch,
    title: title,
    body: body,
    include_title: false,
    include_body: false
  )
end

.detect_provider(host) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/services/git/pr/pr_opener.rb', line 171

def self.detect_provider(host)
  normalized = host.to_s.downcase
  return :gitlab if normalized.include?('gitlab')
  return :gitbucket if normalized.include?('gitbucket')
  return :github if normalized.include?('github')

  nil
end

.encode_branch_for_path(branch) ⇒ Object



114
115
116
# File 'lib/services/git/pr/pr_opener.rb', line 114

def self.encode_branch_for_path(branch)
  URI.encode_www_form_component(branch.to_s).gsub('+', '%20')
end

.extract_owner_repo(origin_url) ⇒ Object



230
231
232
233
234
235
# File 'lib/services/git/pr/pr_opener.rb', line 230

def self.extract_owner_repo(origin_url)
  info = extract_remote_info(origin_url)
  return nil if info.nil?

  { owner: info[:namespace], repo: info[:repo] }
end

.extract_remote_info(origin_url) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/services/git/pr/pr_opener.rb', line 118

def self.extract_remote_info(origin_url)
  remote_text = origin_url.to_s.strip
  return nil if remote_text.empty?

  parsed = parse_uri_remote(remote_text) || parse_scp_remote(remote_text)
  return nil if parsed.nil?

  normalized = normalize_repo_path(parsed[:path])
  return nil if normalized.nil?

  provider = detect_provider(parsed[:host])
  return nil if provider.nil?

  {
    provider: provider,
    host: parsed[:host],
    web_scheme: parsed[:web_scheme],
    namespace: normalized[:namespace],
    repo: normalized[:repo]
  }
end

.github_like_compare_url(remote:, base_branch:, head_branch:, title:, body:, include_title: true, include_body: true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/services/git/pr/pr_opener.rb', line 73

def self.github_like_compare_url(remote:, base_branch:, head_branch:, title:, body:, include_title: true, include_body: true)
  query_params = {
    github_compare_prefill_param(remote[:provider]) => '1'
  }
  normalized_title = normalize_title(title)
  query_params['title'] = normalized_title if include_title && !normalized_title.empty?
  query_params['body'] = body.to_s if include_body && !body.to_s.empty?
  query = URI.encode_www_form(query_params)

  base = "#{remote[:web_scheme]}://#{remote[:host]}"
  path = "#{remote[:namespace]}/#{remote[:repo]}"

  "#{base}/#{path}/compare/#{encode_branch_for_path(base_branch)}...#{encode_branch_for_path(head_branch)}?#{query}"
end

.gitlab_mr_url(remote:, base_branch:, head_branch:, title:, body:, include_title: true, include_description: true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/services/git/pr/pr_opener.rb', line 93

def self.gitlab_mr_url(remote:, base_branch:, head_branch:, title:, body:, include_title: true, include_description: true)
  query_params = {
    'merge_request[source_branch]' => head_branch,
    'merge_request[target_branch]' => base_branch
  }
  normalized_title = normalize_title(title)
  query_params['merge_request[title]'] = normalized_title if include_title && !normalized_title.empty?
  query_params['merge_request[description]'] = body.to_s if include_description && !body.to_s.empty?
  query = URI.encode_www_form(query_params)

  base = "#{remote[:web_scheme]}://#{remote[:host]}"
  path = "#{remote[:namespace]}/#{remote[:repo]}"

  "#{base}/#{path}/-/merge_requests/new?#{query}"
end

.mac?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/services/git/pr/pr_opener.rb', line 241

def self.mac?
  RUBY_PLATFORM.include?('darwin')
end

.normalize_repo_path(raw_path) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/services/git/pr/pr_opener.rb', line 158

def self.normalize_repo_path(raw_path)
  clean = raw_path.to_s.strip
  clean = clean.sub(%r{\A/+}, '').sub(%r{/+\z}, '')
  clean = clean.sub(/\.git\z/, '')
  segments = clean.split('/').reject(&:empty?)
  return nil if segments.length < 2

  {
    namespace: segments[0..-2].join('/'),
    repo: segments[-1]
  }
end

.open_in_browser(url) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/services/git/pr/pr_opener.rb', line 199

def self.open_in_browser(url)
  success = if windows?
              open_windows_browser(url)
            elsif mac?
              system('open', url)
            else
              system('xdg-open', url)
            end

  raise 'Failed to open browser for PR URL.' unless success

  true
end

.open_windows_browser(url) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/services/git/pr/pr_opener.rb', line 213

def self.open_windows_browser(url)
  cleaned_url = url.to_s.strip.sub(/\A\\+/, '')

  # Prefer shell protocol handler. This bypasses cmd/explorer parsing of '&'.
  return true if system('rundll32', 'url.dll,FileProtocolHandler', cleaned_url)

  # PowerShell fallback, passing URL as an argument to avoid command parsing.
  system(
    'powershell',
    '-NoProfile',
    '-Command',
    '$u=$args[0]; Start-Process -FilePath $u',
    '--',
    cleaned_url
  )
end

.parse_scp_remote(remote_text) ⇒ Object



151
152
153
154
155
156
# File 'lib/services/git/pr/pr_opener.rb', line 151

def self.parse_scp_remote(remote_text)
  match = remote_text.match(SCP_REMOTE)
  return nil if match.nil?

  { host: match[:host], path: match[:path], web_scheme: 'https' }
end

.parse_uri_remote(remote_text) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/services/git/pr/pr_opener.rb', line 140

def self.parse_uri_remote(remote_text)
  uri = URI.parse(remote_text)
  return nil unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) || uri.scheme == 'ssh'
  return nil if uri.host.to_s.strip.empty?

  web_scheme = uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) ? uri.scheme : 'https'
  { host: uri.host, path: uri.path, web_scheme: web_scheme }
rescue URI::InvalidURIError
  nil
end

.suggest_title(pr_body, head_branch:) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/services/git/pr/pr_opener.rb', line 180

def self.suggest_title(pr_body, head_branch:)
  in_summary = false
  pr_body.to_s.each_line do |line|
    stripped = line.strip
    if stripped == '## Summary'
      in_summary = true
      next
    end

    break if in_summary && stripped.start_with?('## ')
    next unless in_summary
    next if stripped.empty? || stripped.start_with?('-', '*')

    return stripped[0, 72]
  end

  "Update #{head_branch}"
end

.windows?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/services/git/pr/pr_opener.rb', line 237

def self.windows?
  RUBY_PLATFORM.include?('mingw') || RUBY_PLATFORM.include?('mswin')
end