Skip to content

Commit

Permalink
Manual action
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierapivideo committed Sep 3, 2024
1 parent f41fa81 commit 2d0d59d
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 91 deletions.
139 changes: 96 additions & 43 deletions .github/actions/update-algolia/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8851,67 +8851,120 @@ const algoliasearch = __nccwpck_require__(930);
const crypto = __nccwpck_require__(6417);

function extractMetadata(content, metadataName) {
const regex = new RegExp(`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`, 'gm');
const match = content.match(regex);
const regex = new RegExp(
`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`,
"gm"
);
const match = content.match(regex);

const res = regex.exec(content)
const res = regex.exec(content);

if (res && res[1]) {
return res[1].trim();
} else {
return null;
}
if (res && res[1]) {
return res[1].trim();
} else {
return null;
}
}

function generateObjectID(file) {
return crypto.createHash('md5').update("documentation_" + file).digest('hex');
return crypto
.createHash("md5")
.update("documentation_" + file)
.digest("hex");
}

function getModifiedFiled() {
const addedFiles = JSON.parse(process.env.ADDED_FILES);
const deletedFiles = JSON.parse(process.env.DELETED_FILES);
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES);

return {
filesToIndex: [...addedFiles, ...modifiedFiles].filter(
(file) => file.endsWith(".md") && !file.startsWith("_partials/")
),
deletedFiles,
};
}

function getAllFiles() {
const rootDir = process.env.GITHUB_WORKSPACE;

const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = `${dir}/${file}`;
if (fs.statSync(filePath).isDirectory()) {
filelist = walkSync(filePath, filelist);
} else {
if (filePath.endsWith(".md") && filePath.indexOf("_partials/") === -1) {
filelist = [...filelist, filePath];
}
}
});
return filelist;
};

return {
filesToIndex: walkSync(rootDir),
deletedFiles: [],
};
}

try {
const addedFiles = JSON.parse(process.env.ADDED_FILES);
const deletedFiles = JSON.parse(process.env.DELETED_FILES);
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES);
const algoliaIndex = process.env.ALGOLIA_INDEX;
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID;
const algoliaApiKey = process.env.ALGOLIA_API_KEY;

if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) {
throw new Error("Missing Algolia configuration")
}
const algoliaIndex = process.env.ALGOLIA_INDEX;
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID;
const algoliaApiKey = process.env.ALGOLIA_API_KEY;

const client = algoliasearch(algoliaAppId, algoliaApiKey)
console.log(process.env);

const index = client.initIndex(algoliaIndex)
if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) {
throw new Error("Missing Algolia configuration");
}

const filesToIndex = [...addedFiles, ...modifiedFiles].filter(file =>
file.endsWith('.md') && !file.startsWith('_partials/')
);
const client = algoliasearch(algoliaAppId, algoliaApiKey);

for (const file of filesToIndex) {
const content = fs.readFileSync(file, 'utf8');
const index = client.initIndex(algoliaIndex);

const title = extractMetadata(content, 'title');
const description = extractMetadata(content, 'description');
const { filesToIndex, deletedFiles } =
process.env.MODE === "reindexation" ? getAllFiles() : getModifiedFiled();

if (title) {
let slug = file.replace('.md', '')
if (slug.endsWith('/README')) {
slug = slug.replace('/README', '');
}
const record = { objectID: generateObjectID(file), type: "documentation", preview: description, title: title, content: [content], slug }
index.saveObject(record).wait()
} else {
console.log(`No title found for ${file}. Skipping...`);
}
}
console.log(filesToIndex);

for (const file of deletedFiles) {
index.deleteObject(generateObjectID(file)).wait()
for (const file of filesToIndex) {
const content = fs.readFileSync(file, "utf8");

const title = extractMetadata(content, "title");
const description = extractMetadata(content, "description");

if (title) {
let slug = file.replace(".md", "");
if (slug.endsWith("/README")) {
slug = slug.replace("/README", "");
}
const record = {
objectID: generateObjectID(file),
type: "documentation",
preview: description,
title: title,
content: [content],
slug,
};
try {
index.saveObject(record).wait();
} catch (error) {
console.log(`Error while indexing ${file}: ${error.message}`);
}
} else {
console.log(`No title found for ${file}. Skipping...`);
}
}

for (const file of deletedFiles) {
index.deleteObject(generateObjectID(file)).wait();
}

core.setOutput("response", "ok");
core.setOutput("response", "ok");
} catch (error) {
core.setFailed(error.message);
core.setFailed(error.message);
}

})();
Expand Down
147 changes: 99 additions & 48 deletions .github/actions/update-algolia/index.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,122 @@
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const algoliasearch = require('algoliasearch');
const crypto = require('crypto');
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs");
const algoliasearch = require("algoliasearch");
const crypto = require("crypto");

function extractMetadata(content, metadataName) {
const regex = new RegExp(`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`, 'gm');
const match = content.match(regex);
const regex = new RegExp(
`---\\s*[\\s\\S]*?${metadataName}:(\\s*.*?)\\s*\\n\\s*[\\s\\S]*?^---`,
"gm"
);
const match = content.match(regex);

const res = regex.exec(content)
const res = regex.exec(content);

if (res && res[1]) {
return res[1].trim();
} else {
return null;
}
if (res && res[1]) {
return res[1].trim();
} else {
return null;
}
}

function generateObjectID(file) {
return crypto.createHash('md5').update("documentation_" + file).digest('hex');
return crypto
.createHash("md5")
.update("documentation_" + file)
.digest("hex");
}

function getModifiedFiled() {
const addedFiles = JSON.parse(process.env.ADDED_FILES);
const deletedFiles = JSON.parse(process.env.DELETED_FILES);
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES);

return {
filesToIndex: [...addedFiles, ...modifiedFiles].filter(
(file) => file.endsWith(".md") && !file.startsWith("_partials/")
),
deletedFiles,
};
}

function getAllFiles() {
const rootDir = process.env.GITHUB_WORKSPACE;

const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = `${dir}/${file}`;
if (fs.statSync(filePath).isDirectory()) {
filelist = walkSync(filePath, filelist);
} else {
if (filePath.endsWith(".md") && filePath.indexOf("_partials/") === -1) {
filelist = [...filelist, filePath];
}
}
});
return filelist;
};

return {
filesToIndex: walkSync(rootDir),
deletedFiles: [],
};
}

try {
const addedFiles = JSON.parse(process.env.ADDED_FILES);
const deletedFiles = JSON.parse(process.env.DELETED_FILES);
const modifiedFiles = JSON.parse(process.env.MODIFIED_FILES);
const algoliaIndex = process.env.ALGOLIA_INDEX;
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID;
const algoliaApiKey = process.env.ALGOLIA_API_KEY;
const algoliaIndex = process.env.ALGOLIA_INDEX;
const algoliaAppId = process.env.ALGOLIA_APPLICATION_ID;
const algoliaApiKey = process.env.ALGOLIA_API_KEY;

console.log(process.env);
console.log(process.env);

if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) {
throw new Error("Missing Algolia configuration")
}
if (!algoliaIndex || !algoliaAppId || !algoliaApiKey) {
throw new Error("Missing Algolia configuration");
}

const client = algoliasearch(algoliaAppId, algoliaApiKey)
const client = algoliasearch(algoliaAppId, algoliaApiKey);

const index = client.initIndex(algoliaIndex)
const index = client.initIndex(algoliaIndex);

const filesToIndex = [...addedFiles, ...modifiedFiles].filter(file =>
file.endsWith('.md') && !file.startsWith('_partials/')
);
const { filesToIndex, deletedFiles } =
process.env.MODE === "reindexation" ? getAllFiles() : getModifiedFiled();

for (const file of filesToIndex) {
const content = fs.readFileSync(file, 'utf8');
console.log(filesToIndex);

const title = extractMetadata(content, 'title');
const description = extractMetadata(content, 'description');
for (const file of filesToIndex) {
const content = fs.readFileSync(file, "utf8");

if (title) {
let slug = file.replace('.md', '')
if (slug.endsWith('/README')) {
slug = slug.replace('/README', '');
}
const record = { objectID: generateObjectID(file), type: "documentation", preview: description, title: title, content: [content], slug }
index.saveObject(record).wait()
} else {
console.log(`No title found for ${file}. Skipping...`);
}
}
const title = extractMetadata(content, "title");
const description = extractMetadata(content, "description");

for (const file of deletedFiles) {
index.deleteObject(generateObjectID(file)).wait()
if (title) {
let slug = file.replace(".md", "");
if (slug.endsWith("/README")) {
slug = slug.replace("/README", "");
}
const record = {
objectID: generateObjectID(file),
type: "documentation",
preview: description,
title: title,
content: [content],
slug,
};
try {
index.saveObject(record).wait();
} catch (error) {
console.log(`Error while indexing ${file}: ${error.message}`);
}
} else {
console.log(`No title found for ${file}. Skipping...`);
}
}

for (const file of deletedFiles) {
index.deleteObject(generateObjectID(file)).wait();
}

core.setOutput("response", "ok");
core.setOutput("response", "ok");
} catch (error) {
core.setFailed(error.message);
core.setFailed(error.message);
}

0 comments on commit 2d0d59d

Please sign in to comment.