forked from Shopify/shipit-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary ------- At PowerHRG we desire to implement the ability to automatically deploy a branch as a new "review instance" of our application to our Kubernetes cluster when a Pull Request for that branch is opened - and subsequently clean up the instance when the PR is closed or merged - functionality similar to Heroku's Review Apps. However, we maintain a number of projects and do NOT want to expose this functionality for every repository. As we understand it, Shipit offers no mechanism at the repository level to control which repositories can dynamically provision stacks. It seems the Stack is the highest level concept which involves a repository, but this remains insufficient since the Stack also marries a particular branch and environment to the repository concept. What we _think_ we'd like is a higher level concept which represents the repository to which stacks could belong. This repository could then contain its own configuration for the project-level features we desire. This change set extracts the repository data from the stack concept while retaining the original stack API - all repository related functionality of the Stack is now delegated to the stack's Repository object. References ---------- - Shopify#960 - Shopify#963
- Loading branch information
1 parent
833c850
commit ae9b291
Showing
20 changed files
with
320 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Shipit | ||
class Repository < ApplicationRecord | ||
OWNER_MAX_SIZE = 39 | ||
private_constant :OWNER_MAX_SIZE | ||
|
||
NAME_MAX_SIZE = 100 | ||
private_constant :NAME_MAX_SIZE | ||
|
||
validates :name, uniqueness: {scope: %i(owner), case_sensitive: false, | ||
message: 'cannot be used more than once'} | ||
validates :owner, :name, presence: true, ascii_only: true | ||
validates :owner, format: {with: /\A[a-z0-9_\-\.]+\z/}, length: {maximum: OWNER_MAX_SIZE} | ||
validates :name, format: {with: /\A[a-z0-9_\-\.]+\z/}, length: {maximum: NAME_MAX_SIZE} | ||
|
||
has_many :stacks, dependent: :destroy | ||
|
||
def self.from_github_repo_name(github_repo_name) | ||
repo_owner, repo_name = github_repo_name.downcase.split('/') | ||
find_by(owner: repo_owner, name: repo_name) | ||
end | ||
|
||
def name=(n) | ||
super(n&.downcase) | ||
end | ||
|
||
def owner=(o) | ||
super(o&.downcase) | ||
end | ||
|
||
def http_url | ||
Shipit.github.url("#{owner}/#{name}") | ||
end | ||
|
||
def git_url | ||
"https://#{Shipit.github.domain}/#{owner}/#{name}.git" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateShipitRepositories < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :repositories do |t| | ||
t.string :owner, limit: 100, null: false | ||
t.string :name, limit: 39, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :repositories, ["owner", "name"], name: "repository_unicity", unique: true | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
db/migrate/20191209231307_add_repository_reference_to_stacks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class AddRepositoryReferenceToStacks < ActiveRecord::Migration[6.0] | ||
def up | ||
change_table(:stacks) do |t| | ||
t.references :repository | ||
end | ||
end | ||
|
||
def down | ||
change_column :stacks, :repo_name, :string, null: false | ||
change_column :stacks, :repo_owner, :string, null: false | ||
change_table(:stacks) do |t| | ||
t.remove_references :repository | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class BackfillRepositoryData < ActiveRecord::Migration[6.0] | ||
def up | ||
repositories = Shipit::Stack.distinct.select(:repo_owner, :repo_name).pluck(:repo_owner, :repo_name) | ||
repositories.each do |repo_owner, repo_name| | ||
repository = Shipit::Repository.create_or_find_by( | ||
owner: repo_owner, | ||
name: repo_name, | ||
) | ||
|
||
stacks = Shipit::Stack.where(repo_owner: repository.owner, repo_name: repository.name) | ||
stacks.update(repository: repository) | ||
end | ||
end | ||
|
||
def down | ||
Shipit::Repository.find_each do |repository| | ||
repository.stacks.update_all(repo_owner: repository.owner, repo_name: repository.name) | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
db/migrate/20191216163010_remove_repository_information_from_stacks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class RemoveRepositoryInformationFromStacks < ActiveRecord::Migration[6.0] | ||
def up | ||
change_column :stacks, :repository_id, :integer, null: false | ||
change_table(:stacks) do |t| | ||
t.remove_index ["repo_owner", "repo_name", "environment"] | ||
t.remove :repo_owner | ||
t.remove :repo_name | ||
t.index ["repository_id", "environment"], name: "stack_unicity", unique: true | ||
end | ||
end | ||
|
||
def down | ||
change_table(:stacks) do |t| | ||
t.column :repo_name, :string, limit: 100 | ||
t.column :repo_owner, :string, limit: 39 | ||
t.remove_index ["repository_id", "environment"] | ||
t.index ["repo_owner", "repo_name", "environment"], name: "stack_unicity", unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.