Skip to content

Commit 8caacd2

Browse files
Strokkur424zlataovceDoc94
authored
feat: add last update commit+committer (#575)
* Add last author of page * Remove debug logging and add dev note * pnpm format * Use GitHub token if possible and remove redundant API call * Make time zone be dependant on client's time zone * Undo client side time zone because scorp just told me that it runs server side anyways meaning that it would not even work regardless * Fix incorrect author link by retrieving commit has instead of latest commit * Add Discord server invite link * refactor: error handling, add commit link * fix: pass in GitHub token from workflow * Undo tag styling * fix: wrap * fix: format --------- Co-authored-by: Matouš Kučera <[email protected]> Co-authored-by: Pedro <[email protected]>
1 parent fc4b5ce commit 8caacd2

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

.github/workflows/build-preview.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
run: "pnpm install"
2929
- name: "build"
3030
run: "pnpm build"
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3133
- name: "copy cloudflare configuration files to build directory"
3234
run: |
3335
cp _headers dist/

.github/workflows/deploy.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ jobs:
3030
run: "pnpm install"
3131
- name: "build"
3232
run: "pnpm build"
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3335
- name: "copy cloudflare configuration files to build directory"
3436
run: |
3537
cp _headers dist/

astro.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default defineConfig({
3838
Sidebar: "./src/components/overrides/Sidebar.astro",
3939
PageFrame: "./src/components/overrides/PageFrame.astro",
4040
Footer: "./src/components/overrides/Footer.astro",
41+
LastUpdated: "./src/components/overrides/LastUpdated.astro",
4142
Banner: "./src/components/overrides/Banner.astro",
4243
TableOfContents: "./src/components/overrides/TableOfContents.astro",
4344
MobileTableOfContents: "./src/components/overrides/MobileTableOfContents.astro",
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
import { REPO, getCommitInfo } from "../../utils/git";
3+
4+
const { lang, lastUpdated } = Astro.locals.starlightRoute;
5+
const filePath = Astro.locals.starlightRoute.entry.filePath;
6+
7+
const info = await getCommitInfo(filePath);
8+
---
9+
10+
{
11+
lastUpdated && (
12+
<p>
13+
<span class="nowrap">
14+
Last updated:
15+
<time datetime={lastUpdated.toISOString()}>
16+
{lastUpdated.toLocaleDateString(lang, {
17+
dateStyle: "medium",
18+
timeZone: "UTC",
19+
})}
20+
</time>
21+
</span>{" "}
22+
{info && (
23+
<span class="nowrap">
24+
by <a href={info.committer.href}>{info.committer.name}</a>
25+
in <a href={`https://github.com/${REPO}/commit/${info.hash}`}>{info.hash.substring(0, 7)}</a>
26+
</span>
27+
)}
28+
</p>
29+
)
30+
}
31+
32+
<style>
33+
.nowrap {
34+
white-space: nowrap;
35+
}
36+
</style>

src/content/docs/paper/dev/api/command-api/basics/introduction.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ The following pages will be added to the documentation in the future:
4848
:::
4949

5050
## Additional support
51-
For support regarding the command API, you can always ask in our [Discord Server](https://discord.gg/PaperMC) in the `#paper-dev` channel!
51+
For support regarding the command API, you can always ask in our [Discord server](https://discord.gg/PaperMC) under the
52+
[`#paper-dev`](https://discord.com/channels/289587909051416579/555462289851940864) channel!

src/utils/git.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { execSync } from "child_process";
2+
3+
export interface CommitterInfo {
4+
name: string;
5+
href: string;
6+
}
7+
8+
export interface CommitInfo {
9+
hash: string;
10+
committer: CommitterInfo;
11+
}
12+
13+
const token = process.env.GITHUB_TOKEN;
14+
15+
const options: RequestInit = token
16+
? {
17+
headers: {
18+
Accept: "application/vnd.github+json",
19+
"User-Agent": "papermc-docs/author",
20+
Authorization: `Bearer ${token}`,
21+
},
22+
}
23+
: {
24+
headers: {
25+
Accept: "application/vnd.github+json",
26+
"User-Agent": "papermc-docs/author",
27+
},
28+
};
29+
30+
export const REPO = "PaperMC/docs";
31+
const cache = new Map<string, CommitterInfo>();
32+
33+
export const getCommitInfo = async (filePath: string): Promise<CommitInfo | null> => {
34+
let email: string, hash: string;
35+
try {
36+
email = execSync(`git log -1 --pretty="format:%ae" -- "${filePath}"`).toString();
37+
hash = execSync(`git log -1 --pretty="format:%H" -- "${filePath}"`).toString();
38+
} catch (e) {
39+
return null;
40+
}
41+
42+
const cached = cache.get(email);
43+
if (cached) {
44+
return { hash, committer: cached };
45+
}
46+
47+
let name: string;
48+
try {
49+
name = execSync(`git log -1 --pretty="format:%an" -- "${filePath}"`).toString();
50+
} catch (e) {
51+
return null;
52+
}
53+
54+
const info: CommitterInfo = { name, href: `mailto:${email}` };
55+
56+
const res = await fetch(`https://api.github.com/repos/${REPO}/commits/${hash}`, options);
57+
if (res.ok) {
58+
const commit = await res.json();
59+
info.href = commit.author.html_url;
60+
}
61+
62+
cache.set(email, info);
63+
return { hash, committer: info };
64+
};

0 commit comments

Comments
 (0)