Skip to content

Commit d88d79e

Browse files
committed
Fix incorrect author link by retrieving commit has instead of latest commit
1 parent 75f34f0 commit d88d79e

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/components/overrides/LastUpdated.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const acc: GitHubAccount = await getGitHubAccountFromFile(filePath);
1818
})}
1919
</time>
2020
{" by "}
21-
<a set:html={acc.displayName} href={acc.accountLink} />
21+
<a href={acc.accountLink}>{acc.displayName}</a>
2222
</p>
2323
)
2424
}

src/utils/git-utils.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,30 @@ const headers: RequestInit =
2525
};
2626

2727
const repo: string = "PaperMC/docs";
28-
const cache: Map<string, GitHubAccount> = new Map();
28+
const emailCache: Map<string, GitHubAccount> = new Map();
2929

3030
// Git
3131
export async function getGitHubAccountFromFile(filePath: string): Promise<GitHubAccount | null> {
32-
const displayName = execSync(`git log -1 --pretty="format:%an" -- "${filePath}"`).toString();
3332
const email = execSync(`git log -1 --pretty="format:%ae" -- "${filePath}"`).toString();
34-
35-
const cached = cache.get(email);
33+
const cached = emailCache.get(email);
3634
if (cached != null) {
37-
cached.displayName = displayName;
3835
return cached;
3936
}
4037

38+
const displayName = execSync(`git log -1 --pretty="format:%an" -- "${filePath}"`).toString();
39+
const hash = execSync(`git log -1 --pretty="format:%H" -- "${filePath}"`).toString();
40+
4141
// As the email seems to not be directly linked to an account, we instead use the GitHub API
42-
const url = new URL(`https://api.github.com/repos/${repo}/commits`);
43-
url.searchParams.set("path", filePath);
42+
const url = new URL(`https://api.github.com/repos/${repo}/commits/${hash}`);
4443
url.searchParams.set("per_page", "1");
4544

46-
// It is **KNOWN** that if a commit is not pushed to GitHub, the link will be of the person who last edited the page remotely.
47-
// This is only a bug in local dev or other branch environments, and not in production.
48-
const commit = (await fetch(url, headers).then((response) => response.json()))[0];
45+
let commit = await fetch(url, headers).then((response) => response.json());
4946
let acc: GitHubAccount = {
50-
displayName: displayName,
47+
displayName: commit.commit?.author?.name ?? displayName,
5148
email: email,
5249
accountLink: commit?.author?.html_url,
5350
};
5451

55-
cache.set(email, acc);
52+
emailCache.set(email, acc);
5653
return acc;
5754
}

0 commit comments

Comments
 (0)