leonardssh / coc-discord-rpc

😎 An awesome and fully customizable coc-extension to get Discord Rich Presence integration with NeoVim.
https://www.npmjs.com/package/coc-discord-rpc
MIT License
151 stars 6 forks source link

Repository URL not appearing because "git@" or "http(s)" is hardcoded in matching logic #49

Closed whisperity closed 3 years ago

whisperity commented 3 years ago

If the repository URL isn't formatted to a very hardcoded format, the "View repository" button won't appear.

The issue is around here in utils.ts:


        const matched =
            /^(?:git@|https?:\/\/)(?<provider>[a-zA-Z_.~-]+\.[a-z]{2,})(?::|\/)(?<user>.*?)\/(?<repo>.*)$/.exec(
                remoteUrl.stdout.trim()
            );

It is not always applicable that the SSH user is git, or that the SSH user is specified in the URL on the machine. Namely, you can configure in your SSH config:

Host mygithost.com
   HostName mygithost.com
   User gitolite
   Port 4022

and then use mygithost.com:user/repo (or /repo.git) in the config of the local repository.

It is still highly likely that http://mygithost.com/user/repo is still valid in this context and leads to the repository.

Alternatively, a config option (on a per-repo level) where you could specify which URL to open, side-stepping the entire problem.

leonardssh commented 3 years ago

Fixed. https://github.com/LeonardSSH/coc-discord-rpc/commit/983ed00a785df7f44d91f7d72efb91d2499a7e0f

Please update the extension and let me know if the problem is solved.

whisperity commented 3 years ago

Not fixed yet.

I've updated to coc-discord-rpc@5.0.4. The button now appears! However, for some reason, it does not react to clicks, as if no action was attached to it.

Inside Discord, the DOM is the following:

<button
  type="button"
  class="button-2IFFQ4
    button-38aScr
    lookFilled-1Gx00P
    colorGrey-2DXtkV
    buttonSize-AQY2mE
    grow-q77ONN"
  >
    <div class="contents-18-Yxp">
        View Repository
    </div>
</button>

In 983ed00a785df7f44d91f7d72efb91d2499a7e0f you are replacing git@ which is not necessarily part of the remote URL set in Git. It is not mandated that the remote SSH username must be git on the remote server — e.g. when the server uses Gitolite, it is more often than not gitolite in a standard installation. Similarly, gerrit for Gerrit. That's why you usually use the User variable in ~/.ssh/config. You give a hostname (any hostname, but conventionally the actual hostname) which SSH connections (including Git remote URL specifiers) can use, but everything else, like the remote username, the port used (which might not be the standard 22 port!), the key to use for authentication, port forwards, etc., is configured in the config file for your account.

I think the issue is because the URL set in the RPC call isn't a website, but a bare protocol-less URI. In the debugger view, the following appears in the log:

[RPCServer:IPC] Socket Message: 13

{
  "cmd": "SET_ACTIVITY",
  "data": {
    "application_id": "768090036633206815",
    "assets": {
      "large_image": "811014712325439508",
      "large_text": "Editing a CPP file",
      "small_image": "799113198741094442",
      "small_text": "NeoVim v0.6.0"
    },
    "buttons": ["View Repository"],
    "details": "In Dummy - 0 problems found",
    "metadata": {
      "button_urls": ["github.com:username/project"],
      "__proto__": Object
    },
    "name": "NeoVim",
    "state": "Viewing dummy.cpp",
    "type": 0,
    "__proto__": Object,
    },
  "evt": null,
  "nonce": "0123457-abcd-abcd-abcd-0123456789abc",
  "__proto__": Object,
}

The URL sent to Discord at data.metadata.button_urls[0] should be http://github.com/username/project.

leonardssh commented 3 years ago

I've updated to coc-discord-rpc@5.0.4. The button now appears! However, for some reason, it does not react to clicks, as if no action was attached to it.

See https://github.com/LeonardSSH/vscord/issues/20#issuecomment-901091316

The URL sent to Discord at data.metadata.button_urls[0] should be http://github.com/username/project

What I send to discord, is the proper link, I don't know why discord doesn't set it properly. image

This happens with other extensions on the market too.

whisperity commented 3 years ago

You are getting an https:// from somewhere and sending it. That's different from what I can see. For me, the : separator after the hostname remains in the URL that appears...

What is your git config --get remote.origin.url? Because based on the logic I can see in the source code, I don't see what or where the proper http:// is getting applied.

For me, git config --get remote.origin.url is the following: github.com:username/project.git. There is no git@! There is no ssh://! Your code in the fix begins with:

if (remoteUrl.stdout.startsWith('git@') || remoteUrl.stdout.startsWith('ssh://')) {

This is FALSE and the branch is not taken in case of the remoteURL is set up in a way that is set up for me, and how it is explained in the original ticket.

whisperity commented 3 years ago

I'm not knowledgeable much about TypeScript and don't have the appropriate execution environment, but I've rewritten the code in the proposed fix to Python for ease of debugging:

import re

def rewrite(url):
    if url.startswith("ssh://") or url.startswith("git@"):
        url = url.replace("ssh://", "") \
            .replace(":", "/") \
            .replace("git@", "https://") \
            .replace(".git", "") \
            .replace("\n", "")
        return url
    else:
        regexp = re.compile(r"(https:\/\/)([^@]*)@(.*?$)")
        url = re.sub(regexp, r"\1\3", url) \
            .replace(".git", "") \
            .replace("\n", "")
        return url

Now, running the following test code:

print(rewrite("http://github.com/foo/bar.git"))
print(rewrite("http://github.com/foo/bar"))
print(rewrite("git@github.com:foo/bar.git"))
print(rewrite("git@github.com:foo/bar"))
print(rewrite("github.com:foo/bar.git"))
print(rewrite("github.com:foo/bar"))

When running, I get

http://github.com/foo/bar
http://github.com/foo/bar
https://github.com/foo/bar
https://github.com/foo/bar
github.com:foo/bar
github.com:foo/bar

instead of the last two lines being http://github.com/foo/bar.

whisperity commented 3 years ago

I've found this library, written in pure JavaScript: https://github.com/IonicaBizau/git-url-parse. Perhaps you could use this to parse all the components of the remote URL in a portable way, and use the resulting struct to build the URL sent to Discord.

leonardssh commented 3 years ago

See what I can do. Thanks mate.

leonardssh commented 3 years ago

I tested the library you showed me, and it really works.

Input:

JSON.stringify({
    1: gitUrlParse('http://github.com/foo/bar.git').toString('https').replace('.git', ''),
    2: gitUrlParse('http://github.com/foo/bar').toString('https'),
    3: gitUrlParse('git@github.com:foo/bar.git').toString('https').replace('.git', ''),
    4: gitUrlParse('git@github.com:foo/bar').toString('https'),
    5: gitUrlParse('github.com:foo/bar.git').toString('https').replace('.git', ''),
    6: gitUrlParse('github.com:foo/bar').toString('https')
}, null, 4);

Output:

{
    "1": "https://github.com/foo/bar",
    "2": "https://github.com/foo/bar",
    "3": "https://github.com/foo/bar",
    "4": "https://github.com/foo/bar",
    "5": "https://github.com/foo/bar",
    "6": "https://github.com/foo/bar"
}

I think I'll replace it with what I hardcoded.

leonardssh commented 3 years ago

But the button still cannot be clicked by yourself. I logged in from another discord account, and I can click the button on my profile.

¯\_(ツ)_/¯

leonardssh commented 3 years ago

https://user-images.githubusercontent.com/35312043/130327617-dad278a8-76b1-443d-ae9c-687af1fbfa3d.mp4

whisperity commented 3 years ago

Alright, that I understand (it's weird and very well could be a Discord bug), but that does not change the fact that the fix shown is not a fix, because it does not rewrite the URLs correctly. If git@ or ssh:// isn't in the URL, it's handled in the exact same broken way as before.

Nvm, I saw the previous comments.

The Discord bug is weird, though, because e.g. with Spotify it explicitly shows an error message when you want to click on listening what you're already listening to. 🤔

leonardssh commented 3 years ago

🙄

whisperity commented 3 years ago

Alright, can confirm it works now properly! (As in, sans the Discord bug.) Hang on! This link will take you to `http://github.com/whisperity/llvm-project`. Are you sure you want to go there?