Fix unknown committers causing crash

This commit is contained in:
hazycora 2023-08-03 07:48:52 -05:00
parent 50e7dcaac3
commit cd97774fb2
No known key found for this signature in database
GPG key ID: 215AF1F81F86940E
3 changed files with 13 additions and 2 deletions

5
lib/hash.js Normal file
View file

@ -0,0 +1,5 @@
import { createHash } from 'node:crypto'
export default function hash(string) {
return createHash('md5').update(string).digest().toString('hex')
}

View file

@ -4,6 +4,7 @@ import { Eta } from 'eta'
import render, { renderHtml } from './lib/render.js'
import languageColors from './lib/languagecolors.js'
import fetch from 'node-fetch'
import hash from './lib/hash.js'
// satori is using the native fetch api, causing a warning, so make node-fetch the default
globalThis.fetch = fetch
@ -109,7 +110,12 @@ app.get('/:owner/:repo/commit/:hash', async function (req, res) {
repository: {
full_name: `${req.params.owner}/${req.params.repo}`
},
committer: commit.committer,
committer: commit.committer ?? {
...commit.commit.author,
avatar_url: `${forgejoBaseUrl}/avatar/${hash(
commit.commit.author.email.toLowerCase()
)}`
},
stats: commit.stats,
created_at: commit.created
},

View file

@ -25,7 +25,7 @@
<%~ include('partials/commit-stats.eta', {stats: it.commit.stats}) %>
<div class="info-line">
<img class="avatar" width="48" height="48" src="<%= it.commit.committer.avatar_url %>?size=48"></img>
<p class="author"><%= it.commit.committer.full_name || it.commit.committer.login %></p>
<p class="author"><%= it.commit.committer.full_name || it.commit.committer.name || it.commit.committer.login %></p>
<p>committed <%= new Intl.DateTimeFormat('en-US', { dateStyle: 'long' }).format(new Date(it.commit.created_at)) %></p>
<%~ include('icons/code-commit.eta') %>
<p><%= it.commit.sha.slice(0, 7) %></p>