|
| 1 | +import dotenv from "dotenv"; |
| 2 | +dotenv.config(); |
| 3 | + |
| 4 | +if (process.env.ACCESS_TOKEN === undefined) { |
| 5 | + console.error("Please provide ACCESS_TOKEN in .env file"); |
| 6 | + process.exit(1); |
| 7 | +} |
| 8 | + |
| 9 | +import { Octokit } from "@octokit/rest"; |
| 10 | +import { readFileSync, writeFileSync } from "fs"; |
| 11 | +import { READMEFILE_PATH } from "./config.js"; |
| 12 | + |
| 13 | +const octokit = new Octokit({ |
| 14 | + auth: process.env.ACCESS_TOKEN, |
| 15 | +}); |
| 16 | + |
| 17 | +const getWorflowCount = async () => { |
| 18 | + const username = "pulkitxm"; |
| 19 | + const repoName = username; |
| 20 | + try { |
| 21 | + const response = await octokit.actions.listWorkflowRunsForRepo({ |
| 22 | + owner: username, |
| 23 | + repo: repoName, |
| 24 | + }); |
| 25 | + return response.data.total_count; |
| 26 | + } catch (error) { |
| 27 | + console.error(error.message); |
| 28 | + return 0; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +export async function updateWorkflowNumber() { |
| 33 | + const workflowCount = await getWorflowCount(); |
| 34 | + const readmeContent = readFileSync(READMEFILE_PATH, "utf-8"); |
| 35 | + |
| 36 | + // *Note: All the data displayed above is updated automatically via GitHub Actions. There have been **3** workflow runs so far.* |
| 37 | + const updatedContent = readmeContent.replace( |
| 38 | + /Note: All the data displayed above is updated automatically via GitHub Actions. There have been \*\*\d+\*\* workflow runs so far./, |
| 39 | + `Note: All the data displayed above is updated automatically via GitHub Actions. There have been **${workflowCount}** workflow runs so far.` |
| 40 | + ); |
| 41 | + |
| 42 | + writeFileSync(READMEFILE_PATH, updatedContent, "utf-8"); |
| 43 | + console.log( |
| 44 | + `README file updated with the latest workflow count(${workflowCount}).` |
| 45 | + ); |
| 46 | +} |
0 commit comments