Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
feat: implement leaderboard badger
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc authored and kellertk committed Sep 27, 2022
1 parent 121a17a commit 2f6384b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 50 deletions.
29 changes: 14 additions & 15 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ jobs:
permissions:
pull-requests: write
steps:
- uses: kaizencc/github-merit-badger@main
id: badger
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
badges: '[first,second,third]'
thresholds: '[0,1,2]'
badge-descriptions: '[you are first!,you are second!,you are third!]'
badge-type: 'achievement'
ignore-usernames: '[Tianyi-W]'
# - uses: kaizencc/github-merit-badger@main
# id: contributor-badge-hotlist
# id: badger
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# labels: '1 :trophy:,top 3 :fire:,top 5 :sunglasses:'
# buckets: '1,2,2'
# label-meanings: 'You''re top 2 and you''re not number 2!|You are on the podium!|You have broken into the top 5!'
# days: 30
# category: 'hotlist'
# badges: '[first,second,third]'
# thresholds: '[0,1,2]'
# badge-descriptions: '[you are first!,you are second!,you are third!]'
# badge-type: 'achievement'
# ignore-usernames: '[Tianyi-W]'
- uses: kaizencc/github-merit-badger@main
id: contributor-badge-hotlist
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
badges: '[1 :trophy:,top 3 :fire:,top 5 :sunglasses:]'
thresholds: '[1,2,3]'
badge-descriptions: '[You''re top 2 and you''re not number 2!|You are on the podium!|You have broken into the top 5!]'
badge-type: 'leaderboard'
52 changes: 36 additions & 16 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 3 additions & 12 deletions src/achievement-badger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@ export class AchievementBadger extends Badger {
const pullRequests = await this.getRelevantPullRequests(username);

console.log(JSON.stringify(pullRequests));
const badgeIndex = this.determineBadge(pullRequests);
const badgeIndex = this.determineBadge(this.determineThreshold(pullRequests));
await this.addLabel(badgeIndex);
await this.writeComment(badgeIndex, username);
}

public determineBadge(pullRequests: any[]): number {
public determineThreshold(pullRequests: any[]): number {
const mergedPulls = pullRequests.length;

console.log(`We found ${mergedPulls} pull requests`);

for (let i = 0; i < this.badges.length; i++) {
if (this.badges[i].threshold < mergedPulls) {
continue;
}
return i;
}

return this.badges.length-1;
return mergedPulls;
}
}
15 changes: 13 additions & 2 deletions src/badger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,23 @@ export abstract class Badger {

public abstract runBadgerWorkflow(): Promise<void>;

public abstract determineBadge(pullRequests: any[]): number;
public abstract determineThreshold(pullRequests: any[], username?: string): number;

protected ignoreThisUsername(username: string) {
return this.ignoreUsernames?.includes(username);
}

protected determineBadge(thresholdNumber: number) {
for (let i = 0; i < this.badges.length; i++) {
if (this.badges[i].threshold < thresholdNumber) {
continue;
}
return i;
}

return this.badges.length-1;
}

protected async getGitHubUsername() {
const { data } = await this.octokit.rest.issues.get({
owner: this.repo.owner,
Expand Down Expand Up @@ -146,4 +157,4 @@ function daysToDate(days: number): Date {

function beginsWithVowel(word: string) {
return ['a', 'e', 'i', 'o', 'u'].includes(word[0]);
}
}
27 changes: 23 additions & 4 deletions src/leaderboard-badger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@ export class LeaderboardBadger extends Badger {
}

const pullRequests = await this.getRelevantPullRequests();
const badgeIndex = this.determineBadge(pullRequests);
const badgeIndex = this.determineBadge(this.determineThreshold(pullRequests, username));
await this.addLabel(badgeIndex);
await this.writeComment(badgeIndex, username);
}

public determineBadge(_pullRequests: any[]): number {
console.log('Sorry, this feature has not been implemented yet');
return 0;
public determineThreshold(pullRequests: any[], username?: string): number {
const usernames: Record<string, number> = {};
for (const pull of pullRequests) {
if (usernames[pull.user.login]) {
usernames[pull.user.login]++;
} else {
usernames[pull.user.login] = 1;
}
}

const sortedUsernames = Object.entries(usernames);
sortedUsernames.sort((a, b) => b[1] - a[1]);

const index = sortedUsernames.findIndex(([usr, _amount]) => username === usr);

console.log('index', index);

// contributor has no prior merged PRs
if (!index) { return sortedUsernames.length; }

console.log(sortedUsernames);
return index - 1;
}
}

0 comments on commit 2f6384b

Please sign in to comment.