Skip to content

Update Pinned Posts #22

Update Pinned Posts

Update Pinned Posts #22

name: Update Pinned Posts
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
update-posts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Create and Run Update Script
run: |
node -e '
const fs = require("fs");
const https = require("https");
const ACCOUNT_ID = "01945090-d935-9184-aa31-a7d61df6bd61";
const API_URL = `https://grange.la/api/v1/accounts/${ACCOUNT_ID}/statuses?pinned=true`;
const FILE_PATH = "./src/pages/who/who.md";
const START_MARKER = "<!-- START_SECTION:pinned_post -->";
const END_MARKER = "<!-- END_SECTION:pinned_post -->";
console.log(`Checking file ${FILE_PATH} exists...`);
if (!fs.existsSync(FILE_PATH)) {
console.error(`File not found: ${FILE_PATH}`);
process.exit(1);
}
console.log("Starting update process...");
console.log(`Fetching pinned posts from: ${API_URL}`);
new Promise((resolve, reject) => {
https.get(API_URL, {
headers: {
"User-Agent": "GitHub-Action-Bot",
"Accept": "application/json"
}
}, (res) => {
console.log("API Response Status:", res.statusCode);
console.log("API Response Headers:", JSON.stringify(res.headers, null, 2));
if (res.statusCode !== 200) {
reject(new Error(`API request failed with status ${res.statusCode}`));
return;
}
let data = "";
res.on("data", chunk => data += chunk);
res.on("end", () => resolve(data));
}).on("error", reject);
})
.then(data => {
const posts = JSON.parse(data);
console.log(`Found ${posts.length} pinned posts`);
if (!Array.isArray(posts) || posts.length === 0) {
console.log("No pinned posts found");
process.exit(0);
}
const markdown = posts
.map(post => {
const cleaned = post.content.replace(/<[^>]*>/g, "").trim();
console.log(`Processing post: ${cleaned.substring(0, 50)}...`);
// Add link using the url field from the API
return `- [${cleaned}](${post.url})`;
})
.filter(content => content)
.join("\n");
console.log("Generated markdown:", markdown);
let content = fs.readFileSync(FILE_PATH, "utf8");
// Find start and end positions
const startPos = content.indexOf(START_MARKER);
const endPos = content.indexOf(END_MARKER) + END_MARKER.length;
if (startPos === -1 || endPos === -1) {
console.error("Markers not found in file");
process.exit(1);
}
// Create new content
const newContent =
content.substring(0, startPos) +
START_MARKER + "\n" +
markdown + "\n" +
END_MARKER +
content.substring(endPos);
if (newContent === content) {
console.log("No changes needed");
process.exit(0);
}
fs.writeFileSync(FILE_PATH, newContent);
console.log("File updated successfully");
})
.catch(error => {
console.error("Error:", error);
process.exit(1);
});
'
- name: Commit and Push Changes
run: |
git config --global user.name "GitHub Action Bot"
git config --global user.email "[email protected]"
git add ./src/pages/who/who.md
git status
git diff
git commit -m "Update pinned posts [skip ci]" && git push || echo "No changes to commit"