|
| 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 | +}; |
0 commit comments