Skip to content

Commit

Permalink
feat(contribs): automatic contributor table creation (#1946)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
FonduemangVI and actions-user authored May 22, 2024
1 parent bcff6aa commit 353b0d6
Show file tree
Hide file tree
Showing 5 changed files with 597 additions and 9 deletions.
54 changes: 54 additions & 0 deletions .github/scripts/update-contributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const axios = require('axios');
const fs = require('fs');

// Indentation function
function indentString(string, indentation) {
return string.split('\n').map(line => indentation + line).join('\n').trim();
}

let contributors = '<div style="display: flex; flex-wrap: wrap;">';
let index = 0;
let page = 1;

function fetchPage() {
axios.get(`https://api.github.com/repos/TRaSH-Guides/Guides/contributors?per_page=100&page=${page}`)
.then((response) => {
if (response.data.length === 0) {
// No more contributors, write the file
contributors += '\n</div>';
contributors = indentString(contributors, '');

fs.writeFileSync('CONTRIBUTORS.md', `<!-- editorconfig-checker-disable-file -->\n\n## Contributors\n\n<!-- readme: contributors -start -->\n${contributors}\n<!-- readme: contributors -end -->\n`);
return;
}

response.data.forEach((user, i) => {
// Exclude bots and actions-user
if (user.type === 'Bot' || user.login.toLowerCase().includes('bot') || user.login === 'actions-user' || user.login === 'mynameisbogdan') return;

// Determine row index for background color
const rowIndex = Math.floor(index / 5);
const bgColor = (rowIndex % 2 === 0) ? '#1e2129' : '#303850';

const userHtml = `
<div style="flex: 1 1 20%; background-color: ${bgColor}; border: 1px solid #373a42; padding: 10px; text-align: center;">
<img src="${user.avatar_url}&v=4" style="width: 50px; border-radius: 50%;" alt="${user.login}">
<br>
<b><a href="${user.html_url}" style="color: #ffa500;">${user.login}</a></b>
</div>`;

contributors += '\n' + indentString(userHtml, ' ');

index++;
});

// Fetch the next page
page++;
fetchPage();
})
.catch((error) => {
console.error(`Could not fetch contributors: ${error}`);
});
}

fetchPage();
10 changes: 5 additions & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Build and Deploy Docs

on:
push:
branches:
- master
pull_request:
workflow_run:
workflows: [Update contributors]
types:
- completed

jobs:
build:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
run: mkdocs build

deploy:
if: github.event_name == 'push' && contains(fromJson('["refs/heads/master", "refs/heads/main"]'), github.ref)
if: github.event.workflow_run.event == 'push' && contains(fromJson('["master", "main"]'), github.event.workflow_run.head_branch)
needs: build
name: Deploy docs
runs-on: ubuntu-latest
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/update_contributors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Update contributors

on:
push:
branches:
- master
pull_request:

permissions:
contents: write

jobs:
update_contributors:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "20"

- name: Install dependencies
run: npm install axios

- name: Update CONTRIBUTORS.md
run: |
node .github/scripts/update-contributors.js
- name: Commit and push if it's not up to date
run: |
git diff
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git commit -am "Update CONTRIBUTORS.md" || exit 0
git push
Loading

0 comments on commit 353b0d6

Please sign in to comment.