• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nightconcept / almandine / 14789063338

02 May 2025 05:06AM UTC coverage: 67.965% (-32.0%) from 100.0%
14789063338

push

github

web-flow
fix: Add with e2e tests (#16)

311 of 448 new or added lines in 3 files covered. (69.42%)

314 of 462 relevant lines covered (67.97%)

4.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.33
/src/utils/url.lua
1
--[[
2
  URL Utilities
3
  Provides functions for manipulating and normalizing URLs, primarily for GitHub sources.
4
]]
5

6
local M = {}
1✔
7

8
---
9
-- Normalize GitHub URLs, separating base URL from ref/commit and providing a raw download URL.
10
-- Handles blob URLs, raw URLs, and optionally overrides the ref/commit.
11
-- For non-GitHub URLs, it returns the original URL as base and download URL.
12
-- @param url string The URL to normalize.
13
-- @param commit_hash_override string|nil Optional commit hash/ref to force into the URLs.
14
-- @return string|nil base_url The conceptual base URL (e.g., GitHub blob URL format). Nil on error.
15
-- @return string|nil ref The branch, tag, or commit hash found or used.
16
-- @return string|nil commit_hash The commit hash if the ref is a 40-char hex string. Nil otherwise.
17
-- @return string|nil download_url The direct download URL (e.g., raw.githubusercontent). Nil on error.
18
-- @return string|nil error_message Description of error if normalization fails.
19
function M.normalize_github_url(url, commit_hash_override)
1✔
20
  if type(url) ~= "string" then
×
NEW
21
    return nil, nil, nil, nil, "URL must be a string."
×
22
  end
23

24
  local base_url, ref, commit_hash, download_url
25

26
  -- Pattern 1: GitHub Blob URL
27
  local user, repo, blob_ref, path = url:match("^https://github%.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$")
×
28
  if user then
×
NEW
29
    ref = commit_hash_override or blob_ref -- Use override if present
×
NEW
30
    base_url = string.format("https://github.com/%s/%s/blob/%s/%s", user, repo, ref, path)
×
31
    -- Check if the effective ref is a commit hash
32
    -- Ugly, but let's try explicit repetition to rule out quantifier issues
NEW
33
    local explicit_pattern = "^%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x$"
×
NEW
34
    local match_result = ref:match(explicit_pattern)
×
NEW
35
    if match_result then
×
NEW
36
      commit_hash = ref -- Assign to the function-scope commit_hash
×
37
    end
NEW
38
    download_url = string.format("https://raw.githubusercontent.com/%s/%s/%s/%s", user, repo, ref, path)
×
NEW
39
    return base_url, ref, commit_hash, download_url, nil
×
40
  end
41

42
  -- Pattern 2: GitHub Raw URL
43
  local user_raw, repo_raw, raw_ref, path_raw =
44
    url:match("^https://raw%.githubusercontent%.com/([^/]+)/([^/]+)/([^/]+)/(.+)$")
×
45
  if user_raw then
×
NEW
46
    ref = commit_hash_override or raw_ref -- Use override if present
×
NEW
47
    base_url = string.format("https://github.com/%s/%s/blob/%s/%s", user_raw, repo_raw, ref, path_raw)
×
48
    -- Check if the effective ref is a commit hash
49
    -- Ugly, but let's try explicit repetition to rule out quantifier issues
NEW
50
    local explicit_pattern_raw = "^%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x$"
×
NEW
51
    local match_result_raw = ref:match(explicit_pattern_raw)
×
NEW
52
    if match_result_raw then
×
NEW
53
      commit_hash = ref -- Assign to the function-scope commit_hash
×
54
    end
55
    download_url =
×
NEW
56
      string.format("https://raw.githubusercontent.com/%s/%s/%s/%s", user_raw, repo_raw, ref, path_raw)
×
NEW
57
    return base_url, ref, commit_hash, download_url, nil
×
58
  end
59

60
  -- Pattern 3: GitHub Gist Raw URL
61
  if url:match("^https://gist%.githubusercontent%.com/") then
×
62
    -- Gists have a different structure, treat as opaque download URL.
63
    -- No standard base URL or separate ref/commit concept here.
NEW
64
    base_url = url -- Use the input URL itself as the 'base'
×
NEW
65
    ref = nil
×
NEW
66
    commit_hash = nil
×
67
    download_url = url
×
NEW
68
    return base_url, ref, commit_hash, download_url, nil
×
69
  end
70

71
  -- Pattern 4: GitHub URL that isn't a blob/raw file link (e.g., repo root)
72
  if url:match("^https://github%.com/") then
×
73
    -- Cannot determine a direct file download URL.
NEW
74
    return url, nil, nil, nil, "URL points to a GitHub page, not a specific file blob/raw content."
×
75
  end
76

77
  -- Pattern 5: Non-GitHub URL
78
  -- Assume it's directly downloadable.
79
  base_url = url
×
NEW
80
  ref = nil
×
NEW
81
  commit_hash = nil
×
82
  download_url = url
×
NEW
83
  return base_url, ref, commit_hash, download_url, nil
×
84
end
85

86
---
87
-- Create a standardized source identifier string for GitHub URLs (`github:user/repo/path@ref`).
88
-- @param url string The GitHub URL (blob or raw) to process.
89
-- @return string|nil identifier The formatted identifier string. Nil if not a recognized GitHub file URL.
90
-- @return string|nil error_message Description of error if processing fails.
91
function M.create_github_source_identifier(url)
1✔
92
  if type(url) ~= "string" then
4✔
NEW
93
    return nil, "URL must be a string."
×
94
  end
95

96
  -- Pattern 1: GitHub Blob URL
97
  local user, repo, ref, path = url:match("^https://github%.com/([^/]+)/([^/]+)/blob/([^/]+)/(.+)$")
4✔
98
  if user then
4✔
99
    return string.format("github:%s/%s/%s@%s", user, repo, path, ref), nil
4✔
100
  end
101

102
  -- Pattern 2: GitHub Raw URL
103
  local user_raw, repo_raw, ref_raw, path_raw =
NEW
104
    url:match("^https://raw%.githubusercontent%.com/([^/]+)/([^/]+)/([^/]+)/(.+)$")
×
NEW
105
  if user_raw then
×
NEW
106
    return string.format("github:%s/%s/%s@%s", user_raw, repo_raw, path_raw, ref_raw), nil
×
107
  end
108

109
  -- Not a recognized format for this specific identifier type.
NEW
110
  return nil, "URL is not a recognized GitHub blob or raw file URL format."
×
111
end
112

113
return M
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc