Skip to content

Commit

Permalink
#16 Added the 'crappy' badge
Browse files Browse the repository at this point in the history
This one is for you @nerfox22 ;-)
  • Loading branch information
Pierre-Henri Symoneaux committed Oct 27, 2017
1 parent 75114ac commit 3445a7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
46 changes: 37 additions & 9 deletions lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def initialize(short, title, description, &block)
end

def earned_by?(player)
@block.call(player)
# Return false if no evaluation predicate has been provided
@block && @block.call(player) || false
end
end

Expand All @@ -35,7 +36,8 @@ def earned_by?(player)
Badge.new('adventure', 'The adventurer', 'Submitted 1 Pull Request to a repository he does not own, out of <a href="https://github.com/ourtigarage">ourtigarage</a> organisation', &:contributed_out_of_org?),
Badge.new('novelist', 'The novelist', 'Wrote more than 100 words in a Pull Request\'s description', &:contribution_with_100_words?),
Badge.new('taciturn', 'The taciturn', 'Submitted a Pull Request with no description', &:contribution_with_no_word?),
Badge.new('pirate', 'The pirate', 'A lawless pirate who submitted Pull Requests to his own repositories. Cheater...', &:contribution_to_own_repos?)
Badge.new('pirate', 'The mighty pirate', 'A lawless pirate who submitted Pull Requests to his own repositories. Cheater...', &:contribution_to_own_repos?),
Badge.new('crap', 'The smelly code', 'Has a Pull Request marked as invalid. Probably some bad smelling code', &:invalid_contribs?)
].freeze

# The leaderboard root class, where the magic happens
Expand Down Expand Up @@ -82,7 +84,8 @@ def members

def query_users_data(usernames)
authors = usernames.map { |n| "author:#{n}" }.join ' '
query_filter = "is:pr #{authors} created:#{@event_date} -label:invalid"
# query_filter = "is:pr #{authors} created:#{@event_date} -label:invalid"
query_filter = "#{authors} created:#{@event_date}"
@github.search_issues(query_filter)
.items
.each_with_object({}) { |e, acc| (acc[e.user.login] ||= [e.user, []])[1] << e }
Expand All @@ -98,7 +101,7 @@ def query_missing_users_data(usernames, data)

def add_missing_users(usernames, data)
query_missing_users_data(usernames, data)
.each_with_object(data) { |e, acc| add_user(acc, e) }
.each_with_object(data) { |e, acc| acc[e.login] ||= [e, []] }
end

def members_data(usernames)
Expand All @@ -114,15 +117,17 @@ def get_member_from_github(username)

# A contest member from the participant list in landing page
class Member
attr_reader :username, :avatar, :profile, :contributions
attr_reader :username, :avatar, :profile, :contributions, :invalids, :issues

# Construct a user using the data fetched from GitHub
def initialize(github_user, contributions)
@username = github_user.login
@avatar = github_user.avatar_url
@profile = github_user.html_url
@repos_base_url
@contributions = contributions
@invalids = []
@contributions = []
@issues = []
add_contributions(contributions)
end

# Check if the user has completed the challenge
Expand Down Expand Up @@ -153,7 +158,10 @@ def ten_contributions?
end

def contributed_out_of_org?
contributions.any? { |c| !c.repository_url.start_with?(ORG_REPOS_URL) && !c.repository_url.start_with?("#{BASE_REPOS_URL}/#{@username}") }
contributions.any? do |c|
!c.repository_url.start_with?(ORG_REPOS_URL) &&
!c.repository_url.start_with?("#{BASE_REPOS_URL}/#{@username}")
end
end

def contribution_with_100_words?
Expand All @@ -165,7 +173,13 @@ def contribution_with_no_word?
end

def contribution_to_own_repos?
contributions.any? { |c| c.repository_url.start_with? "#{BASE_REPOS_URL}/#{@username}" }
contributions.any? do |c|
c.repository_url.start_with? "#{BASE_REPOS_URL}/#{@username}"
end
end

def invalid_contribs?
!invalids.empty?
end

def badges
Expand All @@ -179,4 +193,18 @@ def to_json(*_opts)
profile: @profile
}.to_json
end

private

def add_contributions(contributions)
contributions.each do |contrib|
if !contrib.pull_request
@issues << contrib
elsif contrib.labels.any? { |l| l.name == 'invalid' }
@invalids << contrib
else
@contributions << contrib
end
end
end
end
4 changes: 3 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
require 'sinatra'
require_relative 'lib/leaderboard'

# URL to the participant list file. Can be local or remote
PARTICIPANT_FILE = 'https://raw.githubusercontent.com/ourtigarage/hacktoberfest-summit/master/participants.md'.freeze
# The date for the event in the format YYYY[-MM[-DD]]
EVENT_DATE = '2017-10'.freeze

# Initialize the leaderboard
leaderboard = Leaderboard.new EVENT_DATE, PARTICIPANT_FILE

# Set listening port from env variable, or fallback to 80 as default
set :port, (ENV['PORT'] || 80).to_i
set :port, ENV['PORT'] || 80
# Set the static web content directory
set :public_folder, File.dirname(__FILE__) + '/static'

Expand Down
Binary file added static/img/badges/crap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<td>
<!-- List of badges earned -->
<% for badge in member.badges %>
<div class="popover popover-right">
<div class="popover popover-top">
<figure class="avatar avatar-xl">
<img src="img/badges/<%=badge.short%>.png" alt="<%=badge.short%>">
</figure>
Expand Down

0 comments on commit 3445a7a

Please sign in to comment.