From 4a50c418c641d3276b8de9e6e3c09e6eb6096dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Plant=C3=A9?= Date: Tue, 30 Oct 2018 01:21:37 +0100 Subject: [PATCH] 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 --- app/labor/badge_rewarder.rb | 14 ++++++++++++++ lib/tasks/fetch.rake | 5 +++++ spec/labor/badge_rewarder_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/app/labor/badge_rewarder.rb b/app/labor/badge_rewarder.rb index 2e887baac..7a9e6badc 100644 --- a/app/labor/badge_rewarder.rb +++ b/app/labor/badge_rewarder.rb @@ -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( diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake index 7605f39b6..0d231e4d5 100644 --- a/lib/tasks/fetch.rake +++ b/lib/tasks/fetch.rake @@ -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 diff --git a/spec/labor/badge_rewarder_spec.rb b/spec/labor/badge_rewarder_spec.rb index 2d420642b..e1d6b9a55 100644 --- a/spec/labor/badge_rewarder_spec.rb +++ b/spec/labor/badge_rewarder_spec.rb @@ -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