Add plugin to fetch GH releases page recent data

This commit is contained in:
Arista Indrajaya 2024-02-23 17:27:02 +07:00
parent 4e89732419
commit fcb909f957
3 changed files with 148 additions and 1 deletions

View File

@ -2,4 +2,5 @@ GTM_ID=xxxx
UMAMI_PROJECT_API_KEY=xxxx
UMAMI_APP_URL=xxxx
ALGOLIA_API_KEY=xxxx
ALGOLIA_APP_ID=xxxx
ALGOLIA_APP_ID=xxxx
GITHUB_ACCESS_TOKEN=xxxx

View File

@ -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;

View File

@ -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);
},
};
};