From fcb909f95773cdce23b37d795a42fb062278f51b Mon Sep 17 00:00:00 2001 From: Arista Indrajaya Date: Fri, 23 Feb 2024 17:27:02 +0700 Subject: [PATCH] Add plugin to fetch GH releases page recent data --- docs/.env.example | 3 +- docs/plugins/changelog-plugin/fetchData.js | 116 +++++++++++++++++++++ docs/plugins/changelog-plugin/index.js | 30 ++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 docs/plugins/changelog-plugin/fetchData.js create mode 100644 docs/plugins/changelog-plugin/index.js diff --git a/docs/.env.example b/docs/.env.example index b4a7fa5f1..22f6e715f 100644 --- a/docs/.env.example +++ b/docs/.env.example @@ -2,4 +2,5 @@ GTM_ID=xxxx UMAMI_PROJECT_API_KEY=xxxx UMAMI_APP_URL=xxxx ALGOLIA_API_KEY=xxxx -ALGOLIA_APP_ID=xxxx \ No newline at end of file +ALGOLIA_APP_ID=xxxx +GITHUB_ACCESS_TOKEN=xxxx \ No newline at end of file diff --git a/docs/plugins/changelog-plugin/fetchData.js b/docs/plugins/changelog-plugin/fetchData.js new file mode 100644 index 000000000..fa97521fd --- /dev/null +++ b/docs/plugins/changelog-plugin/fetchData.js @@ -0,0 +1,116 @@ +const fs = require('fs'); +const path = require('path'); + +async function fetchData(siteConfig) { + const accessToken = siteConfig.customFields.githubAccessToken; + const owner = siteConfig.customFields.organizationName; + const repo = siteConfig.customFields.projectName; + const apiUrl = `https://api.github.com/repos/${owner}/${repo}/releases`; + + const outputDirectory = path.join(__dirname, '../../docs/quickstart/changelogs-v2'); + + if (!fs.existsSync(outputDirectory)) { + fs.mkdirSync(outputDirectory); + } + + let counter = 1; + const categoryFilePath = path.join(outputDirectory, '_category_.json'); + const cacheFilePath = path.join(outputDirectory, 'cache.json'); + + let cachedData = {}; + if (fs.existsSync(cacheFilePath)) { + cachedData = JSON.parse(fs.readFileSync(cacheFilePath, 'utf-8')); + } + + // Function to retrieve issue details from GitHub API + async function getIssueDetails(issueNumber) { + const issueApiUrl = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`; + const response = await fetch(issueApiUrl, { + method: 'GET', + headers: { + 'Accept': 'application/vnd.github.v3+json', + 'Authorization': `Bearer ${accessToken}`, + }, + }); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + return response.json(); + } + + // Fetch releases from GitHub API or load from cache + let releases = []; + try { + if (cachedData.releases) { + console.log('Loading releases from cache...'); + releases = cachedData.releases; + } else { + console.log('Fetching releases from GitHub API...'); + const response = await fetch(apiUrl, { + method: 'GET', + headers: { + 'Accept': 'application/vnd.github+json', + 'Authorization': `Bearer ${accessToken}`, + 'X-GitHub-Api-Version': '2022-11-28', + }, + }); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + releases = await response.json(); + // Cache the fetched releases + cachedData.releases = releases; + fs.writeFileSync(cacheFilePath, JSON.stringify(cachedData, null, 2), 'utf-8'); + console.log(`Fetched releases saved to cache: ${cacheFilePath}`); + } + } catch (error) { + console.error('Error fetching GitHub releases:', error.message); + return; + } + + // Process the GitHub releases data here + for (const release of releases) { + const version = release.tag_name; + const releaseUrl = release.html_url; + const issueNumberMatch = release.body.match(/#(\d+)/); + const issueNumber = issueNumberMatch ? parseInt(issueNumberMatch[1], 10) : null; + + let issueLink = ''; + if (issueNumber) { + const issueDetails = await getIssueDetails(issueNumber); + issueLink = ` [Issue #${issueNumber}: ${issueDetails.title}](${issueDetails.html_url})`; + } + + const changes = release.body; + + let markdownContent = `---\nsidebar_position: ${counter}\n---\n# ${version}\n\nFor more details, [GitHub Issues](${releaseUrl})\n\nHighlighted Issue: ${issueLink}\n\n${changes}\n`; + + // Write to a separate markdown file for each version + const outputFilePath = path.join(outputDirectory, `changelog-${version}.mdx`); + fs.writeFileSync(outputFilePath, markdownContent, 'utf-8'); + + console.log(`Changelog for version ${version} has been exported to: ${outputFilePath}`); + + counter++; + } + + // Create _category_.json file + const categoryContent = { + label: 'Changelog-v2', + position: 5, + link: { + type: 'generated-index', + description: 'Changelog for Jan', + }, + }; + + fs.writeFileSync(categoryFilePath, JSON.stringify(categoryContent, null, 2), 'utf-8'); + + console.log(`_category_.json has been created at: ${categoryFilePath}`); +} + +module.exports = fetchData; diff --git a/docs/plugins/changelog-plugin/index.js b/docs/plugins/changelog-plugin/index.js new file mode 100644 index 000000000..4d4c28615 --- /dev/null +++ b/docs/plugins/changelog-plugin/index.js @@ -0,0 +1,30 @@ +const fetchData = require('./fetchData'); + +module.exports = function (context, options) { + const { siteConfig, isBuild } = context; + + // Fetch GitHub releases and generate markdown files + fetchData(siteConfig) + .then(() => { + console.log('Changelog data fetched successfully.'); + }) + .catch((error) => { + console.error('Error fetching GitHub releases:', error.message); + }); + + // Hook into Docusaurus lifecycle events + return { + name: 'changelog-plugin', + async onPreBuild() { + if (isBuild) { + // Fetch GitHub releases and generate markdown files during the build + // await fetchData(siteConfig); + } + }, + + async onPostBuild() { + // If you need additional actions after the build, you can include them here. + await fetchData(siteConfig); + }, + }; +};