Skip to content

Commit e699c20

Browse files
Create update-readme.yml
1 parent 16e3a14 commit e699c20

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Update
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
jobs:
8+
update-readme:
9+
runs-on: ubuntu-latest
10+
11+
permissions:
12+
contents: write
13+
packages: write
14+
15+
steps:
16+
# Check if the README file exists
17+
- name: Check for README file
18+
run: |
19+
if [ ! -f ./README.md ]; then
20+
echo "README file not found"
21+
exit 1
22+
else
23+
echo "README file found"
24+
fi
25+
26+
- name: Install dependencies
27+
run: |
28+
npm install axios fs
29+
30+
- name: Run the script
31+
uses: actions/github-script@v5
32+
with:
33+
script: |
34+
const axios = require("axios");
35+
const fs = require("fs");
36+
const os = require("os");
37+
38+
const owner = "kaal-coder";
39+
const repo = "hacktoberfest2023";
40+
const filePath = "./README.md";
41+
42+
const fetchDataAndUpdateReadme = async () => {
43+
try {
44+
// Fetch contributors' data from kaal-coder/hacktoberfest2023
45+
const response = await axios.get(
46+
`https://api.github.com/repos/${owner}/${repo}/contributors`
47+
);
48+
const contributors = response.data;
49+
50+
// Build a new list of contributors with images and names
51+
let contributorsList = "## Contributors to this Repository\n\n";
52+
contributors.forEach((contributor) => {
53+
contributorsList += `<a href="https://github.com/${contributor.login}" target="_blank"><img src="${contributor.avatar_url}" alt="${contributor.login}" style="border-radius: 50%; width: 50px; height: 50px;"></a>\n`;
54+
});
55+
56+
// Read your existing README content with consistent line endings
57+
let readmeContent = fs
58+
.readFileSync(filePath, "utf8")
59+
.replace(/\r\n/g, "\n");
60+
61+
// Check if the "Contributors to this Repository" section already exists
62+
const sectionExists = readmeContent.includes(
63+
"## Contributors to this Repository"
64+
);
65+
66+
if (sectionExists) {
67+
// If the section exists, replace it with the new contributors
68+
const startIndex = readmeContent.indexOf(
69+
"## Contributors to this Repository"
70+
);
71+
const endIndex = readmeContent.indexOf("##", startIndex + 1);
72+
readmeContent =
73+
readmeContent.slice(0, startIndex) +
74+
contributorsList +
75+
readmeContent.slice(endIndex);
76+
} else {
77+
// If the section doesn't exist, simply add it with the new contributors
78+
readmeContent += contributorsList;
79+
}
80+
81+
// Write the updated content with consistent line endings
82+
fs.writeFileSync(
83+
filePath,
84+
readmeContent.replace(/\n/g, os.EOL),
85+
"utf8"
86+
);
87+
console.log("Updated Readme.md with new contributors section");
88+
} catch (error) {
89+
console.error("Error:", error);
90+
}
91+
};
92+
93+
fetchDataAndUpdateReadme();
94+
95+
- name: Commit and Push Changes
96+
run: |
97+
git config user.name "${GITHUB_ACTOR}"
98+
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
99+
git add .
100+
git commit -m "Update README and dynamic content"
101+
git push origin HEAD:main # Replace 'main' with your branch name if different || { echo "Git push failed"; exit 0; }

0 commit comments

Comments
 (0)