Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions .github/scripts/add-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/add-contributor-on-merge.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
Loading