Onebox: rewrite Github links to specific commit?

In this old topic from two years ago (Only 21 “likes” allowed in 24 hours?), a post includes a link to Github, citing a specific line in one of Discourse’s source files.

However, time has moved on and what was once helpful is now quite confusing as the lines shown are not related to the topic at all.

Should onebox’d Github links be rewritten (on submitting a post or asynchronously, similar to when linked images are downloaded and lightboxed), so that the link points to a specific commit instead of a movable target?

4 Likes

This is a hard problem and is more of an issue for Github.

What if in the time the user links the lines, a new commit is made invalidating the original link. How would Onebox know that?

The onebox gem wouldn’t. A background task in Discourse needs to do the heavy lifting. Using the commit that is closest but not after the post revision’s created_at timestamp where the unresolved Github link was first introduced should be close enough.

For instance, regarding the link in question, this commit would apply:

I don’t know if that’s easy or not but that sure would be a good idea. We hate links that break over time.

While that kind of magic would be quite helpful in most cases, I could imagine posts where either the author did not intend to link to a specific line (but got the link from somewhere else, or clicked on the line), but actually the file (or that line in the file), in the latest version.

Maybe a hint in a popover:

Would you like to use a permalink instead? [learn more]

Where the learn more button expands the popup and explains a bit about permalinks.

True, so the rewriting should probably only catch links that include any line anchors /#L\d+(-L\d+)?$/

1 Like

(I clarified my post above)
It could still happen that an author got a link from somewhere (the code search, for example) and just copied it verbatim. That would create another instance where the author might not actually want to link to the line, but point to the class or similar.

Well, first of all, I don’t think this even applies since we’re only discussing oneboxed links. Oneboxes get embedded into the post, the user can see this happen in the post preview pane, embedded content should not change over time.

If the link is not oneboxed, it is not rewritten at all, so you can always link to a given file at latest commit by surrounding that link with text or giving it a label different from the link target.

By the way… the Github code search emits links to specific commits. :wink:

Right, I forgot the user will instantly see the oneboxed response, and the fact that oneboxes shouldn’t change (I guess noone expects them to) is a good point.

I withdraw my concerns :grinning:

1 Like