blob: 6791c14363267bfb017f46db284a3f5dc834ebb5 [file] [log] [blame]
natalie84417e82024-10-11 16:57:50 +08001
2function mysplit(inputstr, sep)
3 if sep == nil then
4 sep = "%s"
5 end
6 local t = {}
7 for str in inputstr:gmatch("([^"..sep.."]+)") do
8 table.insert(t, str)
9 end
10 return t
11end
12
13core.register_action("set_repo_name", { "http-req" }, function(txn)
14 local requestPath = txn.sf:path()
15 local pp = mysplit(requestPath, "/")
natalie48da1682024-11-12 10:21:51 +080016 local queryString = txn.sf:query()
natalie84417e82024-10-11 16:57:50 +080017 local has_srv = string.find(queryString, "service=git-")
18 local git = has_srv ~= nil
19 local e = has_srv ~= nil and #pp-2 or #pp-1
20 if pp[#pp] == "git-upload-pack" or pp[#pp] == "git-recieve-pack" then
natalie48da1682024-11-12 10:21:51 +080021 git = true
natalie84417e82024-10-11 16:57:50 +080022 end
23 local s = 1
24 if pp[1] == 'a' or pp[1] == 'p' then
natalie48da1682024-11-12 10:21:51 +080025 s = 2
natalie84417e82024-10-11 16:57:50 +080026 end
27 local repo = ""
28 if git then
29 for k = s, e, 1 do
30 repo = repo.."/"..pp[k]
31 end
32 else
33 if pp[s] == "changes" or pp[s] == "projects" then
nataliee4ad4e52024-11-01 15:29:31 +080034 repo = "/"
35 if pp[s+1] ~= nil then
36 repo = "/"..pp[s+1]
37 end
natalie84417e82024-10-11 16:57:50 +080038 local sep = string.find(repo, "~")
39 if sep ~= nil then
40 repo = string.sub(repo, 1, sep - 1)
41 end
42 repo = repo:gsub("%%2F", "/")
43 repo = repo:gsub("%%2f", "/")
44 end
45 end
46 if repo == "" then
47 repo = requestPath
48 end
49 txn.http:req_set_header("Gerrit-Project", repo)
50 end)
51