|  | 
|  | 1 | +name: Update Contributors | 
|  | 2 | + | 
|  | 3 | +on: | 
|  | 4 | +  schedule: | 
|  | 5 | +    - cron: '0 0 * * *' # Runs daily at midnight UTC | 
|  | 6 | +  push: | 
|  | 7 | +    branches: | 
|  | 8 | +      - dev | 
|  | 9 | +  workflow_dispatch: # Allows manual trigger | 
|  | 10 | + | 
|  | 11 | +jobs: | 
|  | 12 | +  update-contributors: | 
|  | 13 | +    runs-on: ubuntu-latest | 
|  | 14 | +    steps: | 
|  | 15 | +      - name: Checkout repository | 
|  | 16 | +        uses: actions/checkout@v4 | 
|  | 17 | +        with: | 
|  | 18 | +          fetch-depth: 0 # Fetch all history to access commit details | 
|  | 19 | + | 
|  | 20 | +      - name: Get Contributors, Lines Added, and Commits | 
|  | 21 | +        id: contributors | 
|  | 22 | +        uses: actions/github-script@v7 | 
|  | 23 | +        with: | 
|  | 24 | +          script: | | 
|  | 25 | +            // Initialize map to store net lines and commits per contributor | 
|  | 26 | +            const contributorStats = {}; | 
|  | 27 | +
 | 
|  | 28 | +            // Fetch commits for the dev branch | 
|  | 29 | +            for await (const { data: commits } of github.paginate.iterator( | 
|  | 30 | +              github.rest.repos.listCommits, | 
|  | 31 | +              { | 
|  | 32 | +                owner: context.repo.owner, | 
|  | 33 | +                repo: context.repo.repo, | 
|  | 34 | +                sha: 'dev', // Target the dev branch | 
|  | 35 | +                per_page: 100 | 
|  | 36 | +              } | 
|  | 37 | +            )) { | 
|  | 38 | +              for (const commit of commits) { | 
|  | 39 | +                const author = commit.author?.login || commit.commit.author.name; | 
|  | 40 | +                if (!author) continue; | 
|  | 41 | +
 | 
|  | 42 | +                // Fetch commit details to get stats | 
|  | 43 | +                const commitDetails = await github.rest.repos.getCommit({ | 
|  | 44 | +                  owner: context.repo.owner, | 
|  | 45 | +                  repo: context.repo.repo, | 
|  | 46 | +                  ref: commit.sha | 
|  | 47 | +                }); | 
|  | 48 | +
 | 
|  | 49 | +                const stats = commitDetails.data.stats || { additions: 0, deletions: 0 }; | 
|  | 50 | +                const netLines = stats.additions - stats.deletions; | 
|  | 51 | +
 | 
|  | 52 | +                if (!contributorStats[author]) { | 
|  | 53 | +                  contributorStats[author] = { netLines: 0, commits: 0, login: author }; | 
|  | 54 | +                } | 
|  | 55 | +                contributorStats[author].netLines += netLines; | 
|  | 56 | +                contributorStats[author].commits += 1; | 
|  | 57 | +              } | 
|  | 58 | +            } | 
|  | 59 | +
 | 
|  | 60 | +            // Format contributors list as a table | 
|  | 61 | +            const contributorList = Object.values(contributorStats) | 
|  | 62 | +              .filter(contributor => contributor.netLines > 0 || contributor.commits > 0) // Include contributors with commits or net lines | 
|  | 63 | +              .sort((a, b) => b.netLines - a.netLines) // Sort by net lines descending | 
|  | 64 | +              .map(contributor => { | 
|  | 65 | +                return `| [${contributor.login}](https://github.com/${contributor.login}) | ${contributor.netLines} | ${contributor.commits} |`; | 
|  | 66 | +              }) | 
|  | 67 | +              .join('\n'); | 
|  | 68 | +
 | 
|  | 69 | +            // Add table headers | 
|  | 70 | +            const table = contributorList.length > 0 | 
|  | 71 | +              ? `| Contributor | Net Lines Added | Commits |\n|-------------|-----------------|---------|\n${contributorList}` | 
|  | 72 | +              : 'No contributors with commits or net lines added.'; | 
|  | 73 | +
 | 
|  | 74 | +            return table; | 
|  | 75 | +
 | 
|  | 76 | +      - name: Update README | 
|  | 77 | +        run: | | 
|  | 78 | +          if [ -z "${{ steps.contributors.outputs.result }}" ] || [ "${{ steps.contributors.outputs.result }}" = "No contributors with commits or net lines added." ]; then | 
|  | 79 | +            echo "No contributors with commits or net lines added." | 
|  | 80 | +            exit 0 | 
|  | 81 | +          fi | 
|  | 82 | +          if grep -q "## Contributors" README.md; then | 
|  | 83 | +            sed -i '/## Contributors/,/##/c\## Contributors\n\n${{ steps.contributors.outputs.result }}\n\n##' README.md | 
|  | 84 | +          else | 
|  | 85 | +            echo -e "\n## Contributors\n\n${{ steps.contributors.outputs.result }}\n" >> README.md | 
|  | 86 | +          fi | 
|  | 87 | +          git config user.name "GitHub Action" | 
|  | 88 | +          git config user.email "[email protected]" | 
|  | 89 | +          git add README.md | 
|  | 90 | +          if git diff-index --quiet HEAD; then | 
|  | 91 | +            echo "No changes to commit" | 
|  | 92 | +            exit 0 | 
|  | 93 | +          fi | 
|  | 94 | +          git commit -m "Update contributors table with net lines and commits" | 
|  | 95 | +          git push | 
|  | 96 | +        env: | 
|  | 97 | +          GITHUB_TOKEN: ${{ secrets.CUSTOM_GITHUB_TOKEN }} | 
0 commit comments