|
1 | | -name: Update |
| 1 | +name: Update README |
| 2 | + |
2 | 3 | on: |
3 | 4 | workflow_dispatch: |
4 | | - schedule: |
5 | | - - cron: '0 0 * * *' |
6 | 5 |
|
7 | 6 | jobs: |
8 | 7 | update-readme: |
9 | 8 | runs-on: ubuntu-latest |
10 | 9 |
|
11 | | - permissions: |
12 | | - contents: write |
13 | | - packages: write |
14 | | - |
15 | 10 | 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 |
| 11 | + - name: Checkout Repository |
| 12 | + uses: actions/checkout@v2 |
32 | 13 | 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; |
| 14 | + ref: main # Replace with the desired branch name |
49 | 15 |
|
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 | | - }; |
| 16 | + - name: Install Node.js and Dependencies |
| 17 | + uses: actions/setup-node@v2 |
| 18 | + with: |
| 19 | + node-version: '14' # Use the Node.js version you need |
92 | 20 |
|
93 | | - fetchDataAndUpdateReadme(); |
| 21 | + - name: Install Dependencies |
| 22 | + run: | |
| 23 | + npm install axios fs |
| 24 | + working-directory: ${{ github.workspace }} |
94 | 25 |
|
| 26 | + - name: Debug |
| 27 | + run: | |
| 28 | + echo "Contents of the workspace:" |
| 29 | + ls -la |
| 30 | + cat README.md |
| 31 | + working-directory: ${{ github.workspace }} |
| 32 | + |
| 33 | + - name: Run External Script |
| 34 | + run: node ./updateReadme.js |
| 35 | + |
95 | 36 | - name: Commit and Push Changes |
96 | 37 | run: | |
97 | 38 | git config user.name "${GITHUB_ACTOR}" |
98 | 39 | git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" |
99 | 40 | git add . |
100 | 41 | 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; } |
| 42 | + git push origin HEAD:main # Replace 'main' with your branch name if different |
| 43 | + working-directory: ${{ github.workspace }} |
0 commit comments