From bcd4b98de63748baafd2e6abaefd71496c8acf73 Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Sun, 15 Aug 2021 17:30:45 +0200 Subject: [PATCH] Validate and correct repo url for discord button (#34) * match ssh url format and generate https url for discord buttons * only check for ssh url format after nil check * kind of validate repository urls * small fixes * use actual function Resolves #33 --- lua/presence/init.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lua/presence/init.lua b/lua/presence/init.lua index 0518886..2650941 100644 --- a/lua/presence/init.lua +++ b/lua/presence/init.lua @@ -693,6 +693,37 @@ function Presence:get_buttons(buffer, parent_dirpath) -- Default behavior to show a "View Repository" button if the repo URL is valid if repo_url then + + -- Check if repo url uses short ssh syntax + local domain, project = repo_url:match("^git@(.+):(.+)$") + if domain and project then + self.log:debug(string.format("Repository URL uses short ssh syntax: %s", repo_url)) + repo_url = string.format("https://%s/%s", domain, project) + end + + -- Check if repo url uses a valid protocol + local protocols = { + "ftp", + "git", + "http", + "https", + "ssh", + } + local protocol, relative = repo_url:match("^(.+)://(.+)$") + if not vim.tbl_contains(protocols, protocol) or not relative then + self.log:debug(string.format("Repository URL uses invalid protocol: %s", repo_url)) + return nil + end + + -- Check if repo url has the user specified + local user, path = relative:match("^(.+)@(.+)$") + if user and path then + self.log:debug(string.format("Repository URL has user specified: %s", repo_url)) + repo_url = string.format("https://%s", path) + else + repo_url = string.format("https://%s", relative) + end + self.log:debug(string.format("Adding button with repository URL: %s", repo_url)) return {