Skip to content

Commit

Permalink
Support linking non-track repos to track for reputation tokens (#7051)
Browse files Browse the repository at this point in the history
* Add command to retrieve track ID

* Use command to retrieve track id

* Move to track

* Simplify

* Map non-default repo names to tracks

* Update test/models/track_test.rb

Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>

* Fix track mapping

* Fix integration test

---------

Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>
  • Loading branch information
ErikSchierboom and SleeplessByte committed Sep 4, 2024
1 parent 6fb5a8c commit 3c61ce7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 21 deletions.
17 changes: 14 additions & 3 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def self.for!(param)
find_by!(slug: param)
end

def self.for_repo(repo)
slug = repo.gsub(/-(test-runner|analyzer|representer)$/, '').split('/').last
find_by(slug:)
def self.for_repo(repo) = find_by(slug: slug_from_repo(repo))
def self.id_for_repo(repo) = where(slug: slug_from_repo(repo)).pick(:id)

def self.slug_from_repo(repo)
name = repo.split('/').last
TRACK_HELPER_REPOS[name] || name.gsub(TRACK_REPO_PREFIXES, '').gsub(TRACK_REPO_SUFFIXES, '')
end

def to_param = slug
Expand Down Expand Up @@ -190,4 +193,12 @@ def github_team_name = slug
}.with_indifferent_access.freeze

INFRASTRUCTURE_DURATION_S = 1

TRACK_REPO_PREFIXES = /^(codemirror-lang|eslint-config|babel-preset|highlightjs)-/i
TRACK_REPO_SUFFIXES = /-(test-runner|analyzer|representer|lib-jest-extensions|lib-static-analysis|docker-base)$/i

TRACK_HELPER_REPOS = {
"dotnet-tests" => "csharp",
"eslint-config-tooling" => "typescript"
}.freeze
end
6 changes: 1 addition & 5 deletions app/models/user/reputation_tokens/code_contribution_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ class User::ReputationTokens::CodeContributionToken < User::ReputationToken

before_validation on: :create do
self.earned_on = self.merged_at || Time.current unless earned_on

unless track
normalized_repo = repo.gsub(/-(test-runner|analyzer|representer)$/, '')
self.track_id = Track.where(repo_url: "https://github.com/#{normalized_repo}").pick(:id)
end
self.track_id = Track.id_for_repo(repo) unless track
end

def guard_params = "PR##{pr_node_id}"
Expand Down
6 changes: 1 addition & 5 deletions app/models/user/reputation_tokens/code_merge_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ class User::ReputationTokens::CodeMergeToken < User::ReputationToken

before_validation on: :create do
self.earned_on = self.merged_at || Time.current unless earned_on

unless track
normalized_repo = repo.gsub(/-(test-runner|analyzer|representer)$/, '')
self.track_id = Track.where(repo_url: "https://github.com/#{normalized_repo}").pick(:id)
end
self.track_id = Track.id_for_repo(repo) unless track
end

def guard_params = "PR##{pr_node_id}"
Expand Down
6 changes: 1 addition & 5 deletions app/models/user/reputation_tokens/code_review_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ class User::ReputationTokens::CodeReviewToken < User::ReputationToken

before_validation on: :create do
self.earned_on = self.merged_at || self.closed_at || Time.current unless earned_on

unless track
normalized_repo = repo.gsub(/-(test-runner|analyzer|representer)$/, '')
self.track_id = Track.where(repo_url: "https://github.com/#{normalized_repo}").pick(:id)
end
self.track_id = Track.id_for_repo(repo) unless track
end

def guard_params = "PR##{pr_node_id}"
Expand Down
50 changes: 50 additions & 0 deletions test/models/track_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,56 @@ class TrackTest < ActiveSupport::TestCase
assert_nil Track.for_repo("exercism/configlet")
end

test ".id_for_repo with track repo with repo url" do
track = create :track, repo_url: "https://github.com/exercism/ruby"
assert_equal track.id, Track.id_for_repo(track.repo_url)
end

test ".id_for_repo with track repo with repo name" do
track = create :track, repo_url: "https://github.com/exercism/ruby"
assert_equal track.id, Track.id_for_repo("exercism/ruby")
end

%w[test-runner analyzer representer].each do |suffix|
test ".id_for_repo with #{suffix} repo with repo url" do
track = create :track, repo_url: "https://github.com/exercism/ruby"
assert_equal track.id, Track.id_for_repo("#{track.repo_url}-#{suffix}")
end

test ".id_for_repo with #{suffix} repo with repo name" do
track = create :track, repo_url: "https://github.com/exercism/ruby"
assert_equal track.id, Track.id_for_repo("exercism/ruby-#{suffix}")
end
end

[
["exercism/babel-preset-javascript", "javascript"],
["exercism/babel-preset-typescript", "typescript"],
["exercism/javascript-lib-jest-extensions", "javascript"],
["exercism/dotnet-tests", "csharp"],
["exercism/nim-docker-base", "nim"],
["exercism/codemirror-lang-arturo", "arturo"],
["exercism/codemirror-lang-phix", "phix"],
["exercism/codemirror-lang-gleam", "gleam"],
["exercism/codemirror-lang-wren", "wren"],
["exercism/codemirror-lang-elixir", "elixir"],
["exercism/highlightjs-arturo", "arturo"],
["exercism/highlightjs-gdscript", "gdscript"],
["eslint-config-typescript", "typescript"],
["eslint-config-javascript", "javascript"],
["eslint-config-tooling", "typescript"],
["javascript-lib-static-analysis", "javascript"]
].each do |(repo, slug)|
test ".id_for_repo with cross-track repo #{repo} is linked to #{slug}" do
track = create(:track, slug:)
assert_equal track.id, Track.id_for_repo(repo)
end
end

test ".id_for_repo with non-track or track tooling repo" do
assert_nil Track.id_for_repo("exercism/configlet")
end

test "representations" do
track = create :track
exercise_1 = create(:practice_exercise, track:)
Expand Down
13 changes: 10 additions & 3 deletions test/system/components/journey/reputation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ class ReputationTest < ApplicationSystemTestCase

test "searches contributions" do
user = create :user
track = create :track, title: "Ruby"
exercise = create :concept_exercise
track = create :track, slug: "ruby", title: "Ruby"
other_track = create :track, slug: "nim", title: "Nim"
exercise = create(:concept_exercise, track:)
other_exercise = create(:concept_exercise, track: other_track)
contribution_token = create(:user_code_contribution_reputation_token,
user:,
level: :large,
Expand All @@ -67,6 +69,10 @@ class ReputationTest < ApplicationSystemTestCase
user:,
track:,
exercise:)
other_contribution_token = create(:user_code_review_reputation_token,
user:,
track: other_track,
exercise: other_exercise)

use_capybara_host do
sign_in!(user)
Expand All @@ -75,7 +81,8 @@ class ReputationTest < ApplicationSystemTestCase
end

assert_text strip_tags(review_token.text)
assert_no_text strip_tags(contribution_token.text)
assert_text strip_tags(contribution_token.text)
assert_no_text strip_tags(other_contribution_token.text)
end

test "filters contributions" do
Expand Down

0 comments on commit 3c61ce7

Please sign in to comment.