natalie | 84417e8 | 2024-10-11 16:57:50 +0800 | [diff] [blame] | 1 | |
| 2 | function 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 |
| 11 | end |
| 12 | |
| 13 | core.register_action("set_repo_name", { "http-req" }, function(txn) |
| 14 | local requestPath = txn.sf:path() |
| 15 | local pp = mysplit(requestPath, "/") |
natalie | 48da168 | 2024-11-12 10:21:51 +0800 | [diff] [blame^] | 16 | local queryString = txn.sf:query() |
natalie | 84417e8 | 2024-10-11 16:57:50 +0800 | [diff] [blame] | 17 | 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 |
natalie | 48da168 | 2024-11-12 10:21:51 +0800 | [diff] [blame^] | 21 | git = true |
natalie | 84417e8 | 2024-10-11 16:57:50 +0800 | [diff] [blame] | 22 | end |
| 23 | local s = 1 |
| 24 | if pp[1] == 'a' or pp[1] == 'p' then |
natalie | 48da168 | 2024-11-12 10:21:51 +0800 | [diff] [blame^] | 25 | s = 2 |
natalie | 84417e8 | 2024-10-11 16:57:50 +0800 | [diff] [blame] | 26 | 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 |
natalie | e4ad4e5 | 2024-11-01 15:29:31 +0800 | [diff] [blame] | 34 | repo = "/" |
| 35 | if pp[s+1] ~= nil then |
| 36 | repo = "/"..pp[s+1] |
| 37 | end |
natalie | 84417e8 | 2024-10-11 16:57:50 +0800 | [diff] [blame] | 38 | core.Info(repo) |
| 39 | local sep = string.find(repo, "~") |
| 40 | if sep ~= nil then |
| 41 | repo = string.sub(repo, 1, sep - 1) |
| 42 | end |
| 43 | repo = repo:gsub("%%2F", "/") |
| 44 | repo = repo:gsub("%%2f", "/") |
| 45 | end |
| 46 | end |
| 47 | if repo == "" then |
| 48 | repo = requestPath |
| 49 | end |
| 50 | txn.http:req_set_header("Gerrit-Project", repo) |
| 51 | end) |
| 52 | |