Skip to content

Commit 5ff6aad

Browse files
authored
feat: add plugin to fetch terasology engine info from github (#197)
* adjust .gitignore * add workspace for new plugin * consume plugin
1 parent 472b5c4 commit 5ff6aad

7 files changed

Lines changed: 185 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ bundle-stats.json
6161
# Generated JSON
6262
src/generated/
6363
plugins/source-terasology-modules/data.json
64+
plugins/source-terasology-engine/data.json

gatsby-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ module.exports = {
9494
accessToken: process.env.GITHUB_TOKEN,
9595
},
9696
},
97+
{
98+
resolve: "source-terasology-engine",
99+
options: {
100+
accessToken: process.env.GITHUB_TOKEN,
101+
},
102+
},
97103
{
98104
resolve: "gatsby-plugin-nprogress",
99105
options: {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"private": "true",
66
"workspaces": [
77
".",
8-
"plugins/source-terasology-modules"
8+
"plugins/source-terasology-modules",
9+
"plugins/source-terasology-engine"
910
],
1011
"author": "The Terasology Foundation",
1112
"dependencies": {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# source-terasology-engine
2+
3+
Custom [Gatsby Source Plugin](https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/creating-a-source-plugin/) to fetch enriched information for the [Terasology Engine](https://github.com/MovingBlocks/Terasology) from GitHub.
4+
5+
## Install
6+
7+
This is a local plugin that is automatically loaded from the `plugins/` folder.
8+
9+
## Configuration
10+
11+
```js
12+
// in gatsby-config.js
13+
module.exports = {
14+
plugins: [
15+
//...
16+
{
17+
resolve: "source-terasology-engine",
18+
options: {
19+
accessToken: `<your GitHub access token>`
20+
}
21+
}
22+
]
23+
}
24+
```
25+
26+
Use [Environment Variables](https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/) to avoid exposing secrets.
27+
28+
## Usage
29+
30+
```graphql
31+
query Engine {
32+
allTerasologyEngine {
33+
issues{
34+
nodes {
35+
id
36+
title
37+
author {
38+
login
39+
}
40+
labels {
41+
nodes {
42+
name
43+
}
44+
}
45+
updatedAt
46+
url
47+
}
48+
}
49+
}
50+
}
51+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const { graphql } = require("@octokit/graphql");
2+
const { DateTime } = require("luxon");
3+
const fs = require("fs");
4+
5+
const PLUGIN_NAME = "source-terasology-engine";
6+
7+
const query = `
8+
query Engine {
9+
organization(login: "MovingBlocks") {
10+
repository(name: "Terasology") {
11+
issues(first: 10, filterBy: {labels: "Good First Issue", states: OPEN}, orderBy: {field: UPDATED_AT, direction: DESC}) {
12+
nodes {
13+
id
14+
title
15+
author {
16+
login
17+
}
18+
labels(first: 10) {
19+
nodes {
20+
name
21+
}
22+
}
23+
updatedAt
24+
url
25+
}
26+
}
27+
}
28+
}
29+
}
30+
`;
31+
32+
exports.onPreInit = ({ reporter }) =>
33+
reporter.verbose("Loaded source-terasology-engine");
34+
35+
exports.sourceNodes = async (
36+
{
37+
actions: { createNode },
38+
createContentDigest,
39+
createNodeId,
40+
reporter,
41+
cache,
42+
},
43+
{ accessToken }
44+
) => {
45+
const gql = graphql.defaults({
46+
headers: {
47+
authorization: `token ${accessToken}`,
48+
},
49+
});
50+
51+
const lastFetchedKey = "terasology-engine-last-fetched";
52+
const dataKey = "terasology-engine-data";
53+
54+
const now = DateTime.utc();
55+
const lastFetched = DateTime.fromISO(await cache.get(lastFetchedKey), {
56+
zone: "utc",
57+
});
58+
59+
let engine = {};
60+
61+
// Temporary hack to avoid fetching data from GitHub during local testing.
62+
// Un-/comment as needed after changing the query.
63+
if (fs.existsSync(`${__dirname}/data.json`)) {
64+
// engine = JSON.parse(fs.readFileSync(`${__dirname}/data.json`));
65+
}
66+
67+
if (lastFetched.plus({ hours: 12 }) > now) {
68+
reporter.info(
69+
`[${PLUGIN_NAME}] Loading Terasology engine info from cache ...`
70+
);
71+
engine = JSON.parse(await cache.get(dataKey));
72+
} else {
73+
reporter.info(
74+
`[${PLUGIN_NAME}] Fetching Terasology engine info from GitHub ...`
75+
);
76+
77+
const { organization } = await gql(query);
78+
engine = organization.repository;
79+
80+
await cache.set(dataKey, JSON.stringify(engine));
81+
await cache.set(lastFetchedKey, now.toISO());
82+
83+
fs.writeFileSync(`${__dirname}/data.json`, JSON.stringify(engine, null, 2));
84+
}
85+
86+
reporter.success(`[${PLUGIN_NAME}] Loaded Terasology engine info.`);
87+
88+
const node = {
89+
id: createNodeId(`TerasologyEngine`),
90+
issues: engine.issues,
91+
parent: "__SOURCE__",
92+
children: [],
93+
internal: {
94+
type: "TerasologyEngine",
95+
},
96+
};
97+
98+
node.internal.contentDigest = createContentDigest(node);
99+
100+
createNode(node);
101+
102+
reporter.success(`[${PLUGIN_NAME}] Created node for Terasology engine.`);
103+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "source-terasology-engine",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "A Gatsby source plugin to fetch Terasology engine information.",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"build": "echo \"No build script setup\""
10+
},
11+
"keywords": [
12+
"gatsby",
13+
"gatsby-plugin"
14+
],
15+
"author": "The Terasology Foundation <terasology@gmail.com>",
16+
"dependencies": {
17+
"@octokit/graphql": "^5.0.4",
18+
"gatsby-source-filesystem": "^5.3.1",
19+
"luxon": "^3.2.0"
20+
}
21+
}

0 commit comments

Comments
 (0)