feat: add branches

This commit is contained in:
Adrien Waksberg 2018-08-05 12:37:35 +02:00
parent b9e8dc5ef2
commit 7fce3d67e8
11 changed files with 82 additions and 14 deletions

6
app/models/branch.rb Normal file
View file

@ -0,0 +1,6 @@
class Branch < ApplicationRecord
belongs_to :software
has_many :versions
validates :name, presence: true
validates :name, uniqueness: { scope: :software }
end

View file

@ -1,4 +1,4 @@
class Software < ApplicationRecord class Software < ApplicationRecord
has_many :versions has_many :branches
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
end end

View file

@ -1,6 +1,6 @@
class Version < ApplicationRecord class Version < ApplicationRecord
belongs_to :software belongs_to :branch
validates :number, presence: true validates :number, presence: true
validates :date, presence: true validates :date, presence: true
validates :number, uniqueness: { scope: :software } validates :number, uniqueness: { scope: :branch }
end end

View file

@ -5,6 +5,8 @@
<%= link_to 'Delete', software_path(@software), method: :delete, class: 'uk-icon-link', data: { confirm: 'Are you sure?' } %> <%= link_to 'Delete', software_path(@software), method: :delete, class: 'uk-icon-link', data: { confirm: 'Are you sure?' } %>
</p> </p>
<h3><%= @software.name %></h3> <h3><%= @software.name %></h3>
<% @software.branches.order(name: :desc).each do |branch| %>
<h4><%= branch.name %></h4>
<table class="uk-table uk-table-striped"> <table class="uk-table uk-table-striped">
<thead> <thead>
<tr> <tr>
@ -12,10 +14,11 @@
<th>Date</th> <th>Date</th>
</tr> </tr>
</thead> </thead>
<% @software.versions.order(date: :desc).each do |version| %> <% branch.versions.order(date: :desc).each do |version| %>
<tr> <tr>
<td><%= version.number %></td> <td><%= version.number %></td>
<td><%= version.date.strftime('%Y-%m-%d') %></td> <td><%= version.date.strftime('%Y-%m-%d') %></td>
</tr> </tr>
<% end %> <% end %>
</table> </table>
<% end %>

View file

@ -0,0 +1,10 @@
class CreateBranches < ActiveRecord::Migration[5.2]
def change
create_table :branches do |t|
t.string :name, null: false
t.references :software, foreign_key: true
t.timestamps
end
end
end

View file

@ -0,0 +1,8 @@
class ChangeReferenceToVersion < ActiveRecord::Migration[5.2]
def change
remove_reference :versions, :software, index: true, foreign_key: true
change_table :versions do |t|
t.references :branch, foreign_key: true
end
end
end

View file

@ -10,7 +10,15 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_07_29_191829) do ActiveRecord::Schema.define(version: 2018_08_05_081706) do
create_table "branches", force: :cascade do |t|
t.string "name", null: false
t.integer "software_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["software_id"], name: "index_branches_on_software_id"
end
create_table "github_repositories", force: :cascade do |t| create_table "github_repositories", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
@ -26,15 +34,16 @@ ActiveRecord::Schema.define(version: 2018_07_29_191829) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "repository_type" t.string "repository_type"
t.string "branch_pattern"
end end
create_table "versions", force: :cascade do |t| create_table "versions", force: :cascade do |t|
t.string "number", null: false t.string "number", null: false
t.datetime "date", null: false t.datetime "date", null: false
t.integer "software_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["software_id"], name: "index_versions_on_software_id" t.integer "branch_id"
t.index ["branch_id"], name: "index_versions_on_branch_id"
end end
end end

View file

@ -14,6 +14,20 @@ namespace :repositories do
end.body end.body
end end
def branch(software_id, branches, version)
name = version.match(/^v?(?<branch>\d+\.\d+)(\.\d+)?$/)[:branch]
return branches[name] if branches.include?(name)
branch = Branch.new(
name: name,
software_id: software_id
)
branch.save
branches[name] = branch.id
branch.id
end
desc 'Update version from github repositories' desc 'Update version from github repositories'
task github: :environment do task github: :environment do
headers = { headers = {
@ -24,7 +38,8 @@ namespace :repositories do
GithubRepository.all.each do |repo| GithubRepository.all.each do |repo|
begin begin
software = Software.find(repo.software_id) software = Software.find(repo.software_id)
versions = software.versions.all.map(&:number) branches = software.branches.all.map { |b| [b.name, b.id] }.to_h
versions = software.branches.all.map { |b| b.versions.all.map(&:number) }
tags = tags =
JSON.parse( JSON.parse(
get_url_content( get_url_content(
@ -36,19 +51,20 @@ namespace :repositories do
tags.each do |tag| tags.each do |tag|
next if versions.include?(tag['name']) next if versions.include?(tag['name'])
branch_id = branch(software.id, branches, tag['name'])
date = date =
JSON.parse( JSON.parse(
get_url_content(tag['commit']['url'], headers) get_url_content(tag['commit']['url'], headers)
)['committer']['date'] )['commit']['committer']['date']
Version.new( puts Version.new(
software_id: software.id, branch_id: branch_id,
number: tag['name'], number: tag['name'],
date: date date: date
).save ).save
end end
rescue => e rescue => e
puts e puts e.strace
next next
end end
end end

9
test/fixtures/branches.yml vendored Normal file
View file

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: 1.0
software: two
two:
name: 1.1
software: two

View file

@ -3,9 +3,9 @@
one: one:
number: 1.0.0 number: 1.0.0
date: 2018-07-22 20:30:46 date: 2018-07-22 20:30:46
software: two branch: one
two: two:
number: 1.1.0 number: 1.1.0
date: 2018-12-22 20:30:46 date: 2018-12-22 20:30:46
software: two branch: two

View file

@ -0,0 +1,7 @@
require 'test_helper'
class BranchTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end