Skip to content

Commit

Permalink
Download raw contents instead of using the git patches (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzadp authored Dec 19, 2023
1 parent ca5211b commit 56d7f7c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 66 deletions.
28 changes: 0 additions & 28 deletions .github/scripts/gather-markdown-files.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* This scripts gathers source markdown files of approved and proposed RFCS into the mdbook/src directory.
*/

const fs = require('fs');

// The amount of days that an RFC is considered "new".
Expand All @@ -15,16 +19,14 @@ const dateDaysBefore = (daysBefore) => {
}

[
"patches",
"new-rfcs",
"proposed-rfcs",
"stale-rfcs",
"mdbook/src/approved",
"mdbook/src/new",
"mdbook/src/stale",
"mdbook/src/proposed"
].forEach(path => fs.mkdirSync(path, {resursive: true}))

const TOC = "**Table of Contents**\n\n<\!-- toc -->\n"

module.exports = async ({github, context}) => {
const owner = 'polkadot-fellows'
const repo = 'RFCs'
Expand All @@ -45,30 +47,28 @@ module.exports = async ({github, context}) => {
);
if (addedMarkdownFiles.length !== 1) continue;
const [rfcFile] = addedMarkdownFiles;
const rawText = await (await fetch(rfcFile.raw_url)).text();

const isNew = new Date(pr.created_at) > dateDaysBefore(NEW_RFC_PERIOD_DAYS)
const isStale = new Date(pr.updated_at) < dateDaysBefore(STALE_RFC_PERIOD_DAYS)
const status = isNew ? 'new' : (isStale ? 'stale' : 'proposed')

/*
The git patches are the only way to get the RFC contents without additional API calls.
The alternative would be to download the file contents, one call per PR.
The patch in this object is not a full patch with valid syntax, so we need to modify it a bit - add a header.
*/
const filename = rfcFile.filename.replace("text/", "")
// This header will cause the patch to create a file in {new,proposed,stale}-rfcs/*.md when git-applied.
const patch = `--- /dev/null\n+++ b/${status}-rfcs/${filename}\n` + rfcFile.patch + "\n"
fs.writeFileSync(`patches/${filename}.patch`, patch)

/*
We want to link the proposed RFCs to their respective PRs.
While we have it, we add a link to the source to markdown files and a TOC.
Later, we will append the text of the RFCs to those files.
*/
fs.writeFileSync(
`mdbook/src/${status}/${filename}`,
`[(source)](${pr.html_url})\n\n`
+ "**Table of Contents**\n\n<\!-- toc -->\n\n"
)
+ TOC
+ rawText
)
}

// Copy the approved (already-merged) RFCs markdown files, first adding a source link at the top and a TOC.
for (const file of fs.readdirSync("text/")) {
if (!file.endsWith(".md")) continue;
const text = `[(source)](https://github.com/polkadot-fellows/RFCs/blob/main/text/${file})\n\n`
+ TOC
+ fs.readFileSync(`text/${file}`)
fs.writeFileSync(`mdbook/src/approved/${file}`, text)
}
}
22 changes: 3 additions & 19 deletions .github/workflows/mdbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ jobs:
steps:
- name: Checkout this repo
uses: actions/checkout@v3

- name: Download all proposed RFCs (open PRs)
uses: actions/github-script@v7
with:
script: |
const script = require('.github/scripts/download-rfc-prs.js')
await script({github, context})
# Create the proposed RFC markdown files from the patches gathered in the step above.
- run: |
# We execute the patches, which results in markdown files created in patches/text/*.md
for f in ./patches/*.patch;
do
[ -e "$f" ] || break
git apply $f
done;
# Gather source files - approved (merged) files, and files created in the previous steps.
- name: Gather markdown files into mdbook source directory
# Gather mdbook source files - approved (merged) files, and open PRs.
- name: Gather mdbook sources
uses: actions/github-script@v7
with:
script: |
const script = require('.github/scripts/gather-markdown-files.js')
const script = require('.github/scripts/gather-mdbook-sources.js')
await script({github, context})
- name: Build the mdbook SUMMARY.md
uses: actions/github-script@v7
Expand Down

0 comments on commit 56d7f7c

Please sign in to comment.