Update README.md #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Hacktoberfest Leaderboard | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
update-leaderboard: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Check if the PR was merged | |
if: github.event.pull_request.merged == true | |
run: echo "PR merged" | |
- name: Set contributor points | |
id: set_points | |
run: | | |
POINTS=5 # Default to small (5 points) | |
if [[ $(echo "${{ github.event.pull_request.labels }}" | grep -q "medium") ]]; then | |
POINTS=10 | |
elif [[ $(echo "${{ github.event.pull_request.labels }}" | grep -q "large") ]]; then | |
POINTS=15 | |
fi | |
echo "POINTS=$POINTS" >> $GITHUB_ENV | |
- name: Get current leaderboard | |
uses: actions/github-script@v6 | |
id: leaderboard | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = 1; // Replace with your leaderboard issue number | |
const repo_owner = 'galaxy-bytes'; // Replace with your org or repo owner | |
const repo_name = 'main-test-repo'; // Replace with the repo containing the leaderboard issue | |
const { data: issue } = await github.rest.issues.get({ | |
owner: repo_owner, | |
repo: repo_name, | |
issue_number: issue_number | |
}); | |
return issue.body; | |
- name: Update leaderboard | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const contributor = github.actor; | |
const new_points = parseInt(process.env.POINTS); // Use the POINTS value from env | |
let leaderboard = ${{ steps.leaderboard.outputs.result }}; | |
// Function to update the leaderboard | |
function updateLeaderboard(contributor, new_points, leaderboard) { | |
let lines = leaderboard.split('\n'); | |
let updated = false; | |
// Process only valid table lines (those containing '|' characters) | |
let updatedLeaderboard = lines.map(line => { | |
if (line.includes('|')) { | |
let parts = line.split('|'); | |
if (parts.length > 3 && parts[3] !== undefined) { | |
let currentPoints = parseInt(parts[3].trim()) || 0; | |
let newTotalPoints = currentPoints + new_points; | |
parts[3] = ` ${newTotalPoints} `; | |
updated = true; | |
return parts.join('|'); | |
} | |
} | |
return line; | |
}); | |
// If the contributor was not found, add them | |
if (!updated) { | |
updatedLeaderboard.splice(3, 0, `| ${contributor} | 1 | ${new_points} |`); | |
} | |
// Sort leaderboard by points (ignoring header lines) | |
updatedLeaderboard = updatedLeaderboard.sort((a, b) => { | |
if (!a.includes('|') || !b.includes('|')) return 0; // Ignore non-table lines | |
let aPoints = parseInt(a.split('|')[3].trim()) || 0; | |
let bPoints = parseInt(b.split('|')[3].trim()) || 0; | |
return bPoints - aPoints; | |
}); | |
return updatedLeaderboard.join('\n'); | |
} | |
// Call the updateLeaderboard function and update the issue | |
let updated_leaderboard = updateLeaderboard(contributor, new_points, leaderboard); | |
await github.rest.issues.update({ | |
owner: 'galaxy-bytes', | |
repo: 'main-test-repo', | |
issue_number: 1, // Replace with the actual issue number | |
body: updated_leaderboard | |
}); |