Skip to content

feat: link GitHub references in commits and show a live card - #36

Closed
DirkHoffman wants to merge 1 commit into
dev-null-sh:productionfrom
DirkHoffman:feat/github-context
Closed

DirkHoffman wants to merge 1 commit into
dev-null-sh:productionfrom
DirkHoffman:feat/github-context

Conversation

@DirkHoffman

Copy link
Copy Markdown

What this does

When a commit mentions something on GitHub, devnull now turns it into a link and shows a small card with live details from GitHub. A commit like "Finally merged laravel/framework#5123" now tells you what was merged, and whether it's still open, merged or closed, without leaving devnull.

Light Dark

What gets recognized

You write It links to The card shows
https://github.com/laravel/framework the repository description, stars, language
https://github.com/laravel/framework/issues/1 or laravel/framework#1 the issue or pull request title, and open / closed / merged
https://github.com/laravel/framework/pull/1 the pull request title, and open / closed / merged
https://github.com/laravel/framework/releases/tag/v13.0.0 the release release name, released
https://github.com/laravel/framework/commit/42faad4… or laravel/framework@42faad4 the commit first line of the commit message
  • References work anywhere in the commit body. Full URLs are shown in a shorter form, the way GitHub does it (laravel/framework#1).
  • Only the first reference gets a card, so the commit text stays the main thing.
  • A plain owner/repo is not linked on its own. Too many normal words look like that (and/or, client/server). Paste the URL instead.

How it works

  1. App\Services\GitHubReference finds references in text. Each supported format is one regex in a list, checked against whole words, so trailing punctuation like (…) or . is ignored.
  2. App\Actions\AttachGitHubContext takes a page of commits and adds two fields to each one: github_links and github_card. LogController and UserController pass their paginated commits through it.
  3. Cards are fetched from the GitHub API in parallel (Http::pool), with a 2s connect timeout and a 3s timeout. Results are cached for 10 minutes. A reference that appears in several commits is fetched once.
  4. If GitHub is slow, down or rate-limited, the card still renders with just the label and the link, and the request is retried a minute later. A page never breaks because of GitHub.
  5. If the reference doesn't exist on GitHub (a 404), the link stays but there's no card. So text that only looks like a reference, such as score 3/4#1, doesn't get an empty card.
  6. On the frontend, LinkedText renders the links (React elements only, no dangerouslySetInnerHTML) and GitHubCard renders the card. Colors reuse the existing lane colors.

Configuration

GITHUB_TOKEN is optional. Without it GitHub allows 60 requests per hour, which is fine locally because of the cache. In production, set a token with no scopes (public data only) to raise that to 5,000 per hour:

GITHUB_TOKEN=

Trying it locally

php artisan migrate:fresh --seed now also seeds a handful of commits that reference real repositories, issues, pull requests, releases and commits, plus one reference that doesn't exist (it gets a link but no card) and one and/or that should not be linked.

Tests

  • GitHubReferenceTest: every supported format, text that must not match, deduplication and order, and punctuation.
  • AttachGitHubContextTest: a card for each kind of reference, the fallback on a server error or a failed connection, no card for a reference that doesn't exist, caching for 10 minutes, retrying after a minute, one request for a shared reference, the token header, and no requests for commits without references.
  • LogControllerTest and UserControllerTest: one test each to prove the wiring.

The full suite passes with 100% code coverage and 100% type coverage. PHPStan, Pint, Rector, tsc and the linter pass for the changed files.

Ideas for later

Kept out of this PR to keep it small:

  • Never wait on GitHub. Use Cache::flexible() (stale-while-revalidate), or warm the cache in a queued job when a commit is created, so a cold cache never slows down a page.
  • Link any GitHub URL. Paths like /tree/main/src, /blob/… or /pull/1/files could become plain links without a card.
  • More card types. Discussions, gists, or user and organization profiles.
  • Hide the URL that the card already shows. Like X does when a link is at the end of a post.
  • Other forges. GitLab or Codeberg would be a new list of formats plus one API mapping.

Mentions and hashtags are not part of this. They're on the "not now" list in PRODUCT.md. The words "stars" and "pull request" on the cards describe GitHub's own objects; they aren't new devnull features.

@DirkHoffman

Copy link
Copy Markdown
Author
3-new-commit
4-posting-a-commit.mp4
5-scrolling-the-log.mp4
1-log-light 2-log-dark

@DirkHoffman

Copy link
Copy Markdown
Author

It probably at somepoint does need an api key, but nuno will never leak those so we are good ;)

Commits that mention a GitHub repository, issue, pull request, release or
commit now render those references as links, and the first one as a small
card with live details from the GitHub API (title, state, stars, language).
References that don't exist on GitHub keep their link but get no card.
@nunomaduro

Copy link
Copy Markdown
Member

can you do that when we first create the commit and not at runtime?

@nunomaduro

Copy link
Copy Markdown
Member

Before we consider your pull request today, can you fix the conflicts? Thank you!

@DirkHoffman

Copy link
Copy Markdown
Author

Closing this one. The feedback was right: GitHub shouldn't be called while people browse the app. With production moving this fast, a fresh PR is cleaner: it builds on the link previews from #73 and asks GitHub once, when the commit is created. It follows shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants