Skip to content

Commit

Permalink
add a new feature support course property from the original GitHub La…
Browse files Browse the repository at this point in the history
…b config
  • Loading branch information
wildcard committed Nov 17, 2022
1 parent 13c1273 commit e3adb91
Show file tree
Hide file tree
Showing 7 changed files with 1,541 additions and 50 deletions.
56 changes: 49 additions & 7 deletions generate-github-skills-readme-from-lab-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,42 @@ function isPathtoRelativeMdFile(path) {
return path.endsWith('.md') && !path.startsWith('https://') && RegExp('(./)?[a-zA-Z0-9/]*.md').test(path)
}

async function fetchCourseDetails(course, {octokit, reporter}) {
const [owner, repo] = course.split('/');
owner ||= process.env.GITHUB_REPOSITORY_OWNER;

try {
const courseConfigResponse = await octokit.rest.repos.getContent({
owner,
repo,
path: 'config.yml'
});

const courseConfig = parse(Buffer.from(courseConfigResponse.data.content, 'base64').toString());

const link = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}`;
const title = courseConfig.title;
const description = courseConfig.description;

return { link, title, description };
} catch (error) {
reporter.info(`failed to fetch course details for ${course}`)
reporter.error(error);
return null;
}
}

async function generateReadmeFromConfig(
configPath='config.yml',
courseDetailsPath='course-details.md',
readmePath='./README.md',
rootPath='./',
consoleErr = console.error,
octokit = null,
reporter = console,
// ADDON: Options
{
inlineMDlinks
} = {}) {
console.log('root:', rootPath);
configPath = path.resolve(rootPath, configPath);
courseDetailsPath = path.resolve(rootPath, courseDetailsPath);
readmePath = path.resolve(rootPath, readmePath);
Expand All @@ -85,14 +110,30 @@ async function generateReadmeFromConfig(
let labConfigSteps = labConfig.steps;
// ADDON: files & inline md links
labConfigSteps = await Promise.all(labConfigSteps.map(async (step, index) => {
const { link, file, inlineMDlink } = step;
const { link, file, course, inlineMDlink } = step;

let mdFileContent = null;
if (((inlineMDlinks || inlineMDlink) && (link && isPathtoRelativeMdFile(link))) || file) {
const filePath = file || (inlineMDlinks || inlineMDlink) && link;
mdFileContent = await fs.readFile(path.resolve(rootPath, filePath), 'utf8');
let courseStep = false;
if (course) {
const courseDetails = await fetchCourseDetails(course, { octokit, reporter });
if (courseDetails) {
const { link: courseLink, title: courseTitle, description: courseDescription } = courseDetails;

step.link ||= courseLink;
step.title ||= courseTitle;
step.description ||= courseDescription;

courseStep = true;
}
} else {
if (((inlineMDlinks || inlineMDlink) && (link && isPathtoRelativeMdFile(link))) || file) {
const filePath = file || (inlineMDlinks || inlineMDlink) && link;
mdFileContent = await fs.readFile(path.resolve(rootPath, filePath), 'utf8');
}
}

// TODO: YouTube thumbnail fetcher

return {...step, index, mdFileContent, noLink: (inlineMDlinks || inlineMDlink)}
}))
labConfigSteps.sort((a, b) => a.index - b.index);
Expand All @@ -114,9 +155,10 @@ async function generateReadmeFromConfig(
await fs.writeFile(readmePath, _readmeTemplate)
return _readmeTemplate;
} catch (error) {
consoleErr('README.md GitHub Skill format file creating error: ', error);
reporter.error('README.md GitHub Skill format file creating error: ', error);
throw error;
}

}


Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const core = require('@actions/core');
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();

const { generateReadmeFromConfig } = require('./generate-github-skills-readme-from-lab-config');

async function run() {
Expand All @@ -11,7 +14,7 @@ async function run() {

core.info(`Generating in ${rootPath}: ${readmePath} from ${configPath}, ${courseDetailsPath} ...`);

const readmeContent = await generateReadmeFromConfig(configPath, courseDetailsPath, readmePath, rootPath, core.error, {
const readmeContent = await generateReadmeFromConfig(configPath, courseDetailsPath, readmePath, rootPath, octokit, core, {
inlineMDlinks
});

Expand Down
Loading

0 comments on commit e3adb91

Please sign in to comment.