diff --git a/.github/scripts/add-contributor.js b/.github/scripts/add-contributor.js index f4ac0a5..3068d58 100644 --- a/.github/scripts/add-contributor.js +++ b/.github/scripts/add-contributor.js @@ -10,24 +10,43 @@ if (!username) { const readmePath = path.join(process.cwd(), 'README.md'); let readme = fs.readFileSync(readmePath, 'utf8'); -const contribSectionRegex = /(## Contributors\s*\n)((?:.|\n)*?)(\n## |\n# |\n$)/; +// Where to add the Contributors section (after "Thanks for being part of Codextream") +const thankYouMarker = "## 🙌 Thanks for being part of Codextream!"; + +// Contributor markdown line const contributorLine = `- [@${username}](https://github.com/${username})`; +// Check if already present if (readme.includes(contributorLine)) { console.log('Contributor already present. Skipping.'); process.exit(0); } -if (contribSectionRegex.test(readme)) { - // Add to existing Contributors section +const contributorsHeader = "## Contributors"; +const insertSection = `\n${contributorsHeader}\n${contributorLine}\n`; + +if (readme.includes(contributorsHeader)) { + // Add to existing Contributors section (append if not present) + const contribSectionRegex = /(## Contributors\s*\n)((?:.|\n)*?)(\n## |\n# |\n$)/; readme = readme.replace( contribSectionRegex, - (_, sectionHeader, sectionBody, sectionEnd) => - `${sectionHeader}${sectionBody}${contributorLine}\n${sectionEnd}` + (match, sectionHeader, sectionBody, sectionEnd) => { + if (sectionBody.includes(contributorLine)) { + return match; // Already present + } + return `${sectionHeader}${sectionBody}${contributorLine}\n${sectionEnd}`; + } ); +} else if (readme.includes(thankYouMarker)) { + // Add Contributors section after Thank You marker + const thankYouIndex = readme.indexOf(thankYouMarker); + const afterThankYouIndex = readme.indexOf('\n', thankYouIndex); + const before = readme.slice(0, afterThankYouIndex + 1); + const after = readme.slice(afterThankYouIndex + 1); + readme = before + insertSection + after; } else { - // Add new Contributors section at the end - readme += `\n## Contributors\n${contributorLine}\n`; + // Fallback: append at end + readme += insertSection; } fs.writeFileSync(readmePath, readme, 'utf8'); diff --git a/.github/workflows/add-contributor-on-merge.yml b/.github/workflows/add-contributor-on-merge.yml index 4bf5948..bff1767 100644 --- a/.github/workflows/add-contributor-on-merge.yml +++ b/.github/workflows/add-contributor-on-merge.yml @@ -1,5 +1,5 @@ name: Add Contributor to README -description: Automatically add a contributor to the README when a pull request is merged. + on: pull_request: types: @@ -26,7 +26,6 @@ jobs: env: CONTRIBUTOR_USERNAME: ${{ steps.get_author.outputs.username }} run: | - npm install fs path node .github/scripts/add-contributor.js - name: Commit changes