1
0
mirror of https://github.com/jiriks74/presence.nvim synced 2024-11-23 20:37:50 +01:00

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
This commit is contained in:
Tobias Schmitz 2021-08-15 17:30:45 +02:00 committed by GitHub
parent e632306af1
commit bcd4b98de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 {