MCPcopy
hub / github.com/github/github-mcp-server / expandRepoResourceURI

Function expandRepoResourceURI

pkg/github/repository_resource.go:264–311  ·  view source on GitHub ↗

expandRepoResourceURI builds a resource URI using the appropriate URI template based on the provided parameters (sha, ref, or default).

(owner, repo, sha, ref string, pathParts []string)

Source from the content-addressed store, hash-verified

262// expandRepoResourceURI builds a resource URI using the appropriate URI template
263// based on the provided parameters (sha, ref, or default).
264func expandRepoResourceURI(owner, repo, sha, ref string, pathParts []string) (string, error) {
265 baseValues := uritemplate.Values{
266 "owner": uritemplate.String(owner),
267 "repo": uritemplate.String(repo),
268 "path": uritemplate.List(pathParts...),
269 }
270
271 switch {
272 case sha != "":
273 baseValues["sha"] = uritemplate.String(sha)
274 return repositoryResourceCommitContentURITemplate.Expand(baseValues)
275
276 case ref != "":
277 // Parse ref to determine which template to use
278 switch {
279 case strings.HasPrefix(ref, "refs/heads/"):
280 branch := strings.TrimPrefix(ref, "refs/heads/")
281 baseValues["branch"] = uritemplate.String(branch)
282 return repositoryResourceBranchContentURITemplate.Expand(baseValues)
283
284 case strings.HasPrefix(ref, "refs/tags/"):
285 tag := strings.TrimPrefix(ref, "refs/tags/")
286 baseValues["tag"] = uritemplate.String(tag)
287 return repositoryResourceTagContentURITemplate.Expand(baseValues)
288
289 case strings.HasPrefix(ref, "refs/pull/") && strings.HasSuffix(ref, "/head"):
290 // Extract PR number from "refs/pull/{number}/head"
291 prPart := strings.TrimPrefix(ref, "refs/pull/")
292 prNumber := strings.TrimSuffix(prPart, "/head")
293 baseValues["prNumber"] = uritemplate.String(prNumber)
294 return repositoryResourcePrContentURITemplate.Expand(baseValues)
295
296 case looksLikeSHA(ref):
297 // ref is actually a SHA (e.g., from resolveGitReference)
298 baseValues["sha"] = uritemplate.String(ref)
299 return repositoryResourceCommitContentURITemplate.Expand(baseValues)
300
301 default:
302 // For other refs (like a branch name without refs/heads/ prefix),
303 // treat it as a branch
304 baseValues["branch"] = uritemplate.String(ref)
305 return repositoryResourceBranchContentURITemplate.Expand(baseValues)
306 }
307
308 default:
309 return repositoryResourceContentURITemplate.Expand(baseValues)
310 }
311}

Callers 1

GetFileContentsFunction · 0.85

Calls 3

looksLikeSHAFunction · 0.85
ListMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected