Automatically add contributor badges (#1016)

* Basic proof of concept

* Use Octokit instead of HTTParty

* add rake task

* DEV Community iOS App should be checked too

* This only need to be computed once

* Add spec

* for consistency sake
This commit is contained in:
Frédéric Planté 2018-10-30 01:21:37 +01:00 committed by Ben Halpern
parent 1aa8e70a5b
commit 4a50c418c6
3 changed files with 39 additions and 0 deletions

View file

@ -33,6 +33,20 @@ module BadgeRewarder
award_badges(usernames, "dev-contributor", message_markdown)
end
def self.award_contributor_badges_from_github(since = 1.day.ago, message_markdown = "Thank you so much for your contributions!")
client = Octokit::Client.new
badge = Badge.find_by_slug("dev-contributor")
["thepracticaldev/dev.to", "thepracticaldev/DEV-ios"].each do |repo|
commits = client.commits repo, since: since.iso8601
authors_uids = commits.map { |c| c.author.id }
Identity.where(provider: "github").where(uid: authors_uids).each do |i|
BadgeAchievement.where(user_id: i.user_id, badge_id: badge.id).first_or_create(
rewarding_context_message_markdown: message_markdown,
)
end
end
end
def self.award_badges(usernames, slug, message_markdown)
User.where(username: usernames).each do |user|
BadgeAchievement.create(

View file

@ -92,6 +92,11 @@ task :award_contributor_badges, [:arg1] => :environment do |_t, args|
puts "Done!"
end
# this task is meant to be scheduled daily
task award_contributor_badges_from_github: :environment do
BadgeRewarder.award_contributor_badges_from_github
end
task remove_old_html_variant_data: :environment do
HtmlVariantTrial.where("created_at < ?", 1.week.ago).destroy_all
HtmlVariantSuccess.where("created_at < ?", 1.week.ago).destroy_all

View file

@ -45,6 +45,26 @@ RSpec.describe BadgeRewarder do
described_class.award_contributor_badges([user.username, user_other.username])
expect(BadgeAchievement.where(badge_id: badge.id).size).to eq(2)
end
describe "::award_contributor_badges_from_github" do
let(:my_ocktokit_client) { instance_double(Octokit::Client) }
let(:user) { create(:user) }
let(:stubbed_github_commit) do
[OpenStruct.new(author: OpenStruct.new(id: user.identities.first.uid))]
end
before do
allow(Octokit::Client).to receive(:new).and_return(my_ocktokit_client)
allow(my_ocktokit_client).to receive(:commits).and_return(stubbed_github_commit)
create(:badge, title: "DEV Contributor")
end
it "award contributor badge" do
described_class.award_contributor_badges_from_github
expect(user.badge_achievements.size).to eq(1)
end
end
end
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations