GitLabgitlab-org
cli
main
1// This file implements URL parsing for GitLab resources to allow commands
2// to accept full GitLab URLs instead of just resource IDs.
3//
4// Supported URL formats:
5// - Merge Requests: https://gitlab.com/group/project/-/merge_requests/123
6// - Issues: https://gitlab.com/group/project/-/issues/456
7// - Self-managed instances: https://invent.kde.org/kde/project/-/merge_requests/42
8// - Deep subgroups: https://gitlab.com/group/sub1/sub2/project/-/merge_requests/123
9// - URL fragments and query parameters are automatically stripped
10package cmdutils
12import (
13 "net/url"
14 "regexp"
15 "strconv"
17 "gitlab.com/gitlab-org/cli/internal/config"
18 "gitlab.com/gitlab-org/cli/internal/glrepo"
19)
21// GitLabResourceType represents the type of GitLab resource
22type GitLabResourceType string
24const (
25 // GitLabResourceMergeRequest represents a merge request resource
26 GitLabResourceMergeRequest GitLabResourceType = "merge_request"
28 // GitLabResourceIssue represents an issue resource
29 GitLabResourceIssue GitLabResourceType = "issue"
30)
32// GitLabResourceMetadata contains the parsed metadata from a GitLab URL
33type GitLabResourceMetadata struct {
34 ID int
35 Type GitLabResourceType
36 Repo glrepo.Interface
37}
39// mergeRequestURLPathRE is a regex which matches the following patterns:
40//
41// OWNER/REPO/merge_requests/id
42// OWNER/REPO/-/merge_requests/id
43// GROUP/NAMESPACE/REPO/merge_requests/id
44// GROUP/NAMESPACE/REPO/-/merge_requests/id
45// including nested subgroups:
46// GROUP/SUBGROUP/../../REPO/-/merge_requests/id
47//
48// This pattern avoids backtracking issues by using a more specific pattern
49// instead of .* which can cause catastrophic backtracking in edge cases.
50var mergeRequestURLPathRE = regexp.MustCompile(`^(/(?:[^-][^/]+/){2,})+(?:-/)?merge_requests/(\d+)(?:/.*)?$`)
52// issueURLPathRE is a regex which matches the following patterns:
53//
54// OWNER/REPO/issues/id
55// OWNER/REPO/-/issues/id
56// OWNER/REPO/-/issues/incident/id
57// OWNER/REPO/-/work_items/id
58// GROUP/NAMESPACE/REPO/issues/id
59// GROUP/NAMESPACE/REPO/-/issues/id
60// GROUP/NAMESPACE/REPO/-/issues/incident/id
61// GROUP/NAMESPACE/REPO/-/work_items/id
62// including nested subgroups:
63// GROUP/SUBGROUP/../../REPO/-/issues/id
64// GROUP/SUBGROUP/../../REPO/-/issues/incident/id
65// GROUP/SUBGROUP/../../REPO/-/work_items/id
66//
67// The pattern tolerates trailing path segments (e.g., /issues/1/foo) to align
68// with mergeRequestURLPathRE behavior. Note that url.Parse strips query strings
69// and fragments into separate fields before regex matching.
70var issueURLPathRE = regexp.MustCompile(`^(/(?:[^-][^/]+/){2,})+(?:-/)?(?:issues/(?:incident/)?|work_items/)(\d+)(?:/.*)?$`)
72// groupWorkItemURLPathRE is a regex which matches the following patterns:
73//
74// groups/GROUP/-/work_items/id
75// groups/GROUP/-/epics/id
76// groups/GROUP/epics/id
77// including nested subgroups:
78// groups/GROUP/SUBGROUP/-/work_items/id
79var groupWorkItemURLPathRE = regexp.MustCompile(`^/groups/(.+?)/(?:-/)?(?:work_items|epics)/(\d+)(?:/.*)?$`)
81// ParseGitLabURL returns nil when urlStr is not a merge request or issue URL.
82// URLs without a scheme are accepted.
83func ParseGitLabURL(urlStr, defaultHostname string, cfg config.Config) *GitLabResourceMetadata {
84 u := parseResourceURL(urlStr)
85 if u == nil {
86 return nil
87 }
89 if m := mergeRequestURLPathRE.FindStringSubmatch(u.Path); m != nil {
90 return resourceFromMatch(*u, m, GitLabResourceMergeRequest, defaultHostname, cfg)
91 }
92 if m := issueURLPathRE.FindStringSubmatch(u.Path); m != nil {
93 return resourceFromMatch(*u, m, GitLabResourceIssue, defaultHostname, cfg)
94 }
95 return nil
96}
98// ParseMergeRequestFromURL extracts merge request ID and repository from a GitLab URL
99// Returns 0 and nil if the URL is not a valid merge request URL
100func ParseMergeRequestFromURL(urlStr, defaultHostname string, cfg config.Config) (int, glrepo.Interface) {
101 metadata := ParseGitLabURL(urlStr, defaultHostname, cfg)
102 if metadata != nil && metadata.Type == GitLabResourceMergeRequest {
103 return metadata.ID, metadata.Repo
104 }
105 return 0, nil
106}
108// ParseIssueFromURL returns 0 and nil if urlStr is not an issue, incident, or work item URL.
109// Unlike ParseGitLabURL, it never resolves the URL as a merge request, so an
110// issue URL with a trailing merge_requests segment still parses as an issue.
111func ParseIssueFromURL(urlStr, defaultHostname string, cfg config.Config) (int, glrepo.Interface) {
112 u := parseResourceURL(urlStr)
113 if u == nil {
114 return 0, nil
115 }
117 m := issueURLPathRE.FindStringSubmatch(u.Path)
118 if m == nil {
119 return 0, nil
120 }
121 metadata := resourceFromMatch(*u, m, GitLabResourceIssue, defaultHostname, cfg)
122 if metadata == nil {
123 return 0, nil
124 }
125 return metadata.ID, metadata.Repo
126}
128// ParseGroupWorkItemFromURL returns the group path and IID of a group work
129// item or epic URL, or "" and 0 if urlStr is not one. The host's configured
130// subfolder is ignored.
131func ParseGroupWorkItemFromURL(urlStr string, cfg config.Config) (string, int) {
132 u := parseResourceURL(urlStr)
133 if u == nil {
134 return "", 0
135 }
137 m := groupWorkItemURLPathRE.FindStringSubmatch("/" + glrepo.TrimSubfolder(u, cfg))
138 if m == nil {
139 return "", 0
140 }
141 id, err := strconv.Atoi(m[2])
142 if err != nil {
143 return "", 0
144 }
145 return m[1], id
146}
148func parseResourceURL(urlStr string) *url.URL {
149 u, err := url.Parse(urlStr)
150 if err != nil {
151 return nil
152 }
153 if u.Scheme != "" && u.Scheme != "https" && u.Scheme != "http" {
154 return nil
155 }
156 return u
157}
159// resourceFromMatch expects m to be a submatch of mergeRequestURLPathRE or
160// issueURLPathRE, where m[1] is the project path and m[2] is the resource ID.
161func resourceFromMatch(u url.URL, m []string, resourceType GitLabResourceType, defaultHostname string, cfg config.Config) *GitLabResourceMetadata {
162 resourceID, err := strconv.Atoi(m[2])
163 if err != nil {
164 return nil
165 }
167 u.Path = m[1]
168 repo, err := glrepo.FromURL(&u, defaultHostname, cfg)
169 if err != nil {
170 return nil
171 }
173 return &GitLabResourceMetadata{
174 ID: resourceID,
175 Type: resourceType,
176 Repo: repo,
177 }
178}
Diagnostics6 git calls · 36 ms
CallStatusSizeTime036 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.2 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.8 KiB · hit30 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=30 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=30 git.fetch.wants=062895659202fa4cd99b96b1ebc93358db70a5a7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e13011296aa9fe
- Request
- 98d5950b…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format