Skip to content

Commit

Permalink
feat: open deploy to take package-names
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemellander committed Dec 19, 2024
1 parent 3619c94 commit 24471e2
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 73 deletions.
6 changes: 6 additions & 0 deletions deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ inputs:
description: "path to package.json"
required: false
default: "package.json"
package-names:
description: "The names of the packages to update"
required: false
outputs:
json:
description: "The json output of the upgrade prs."
Expand All @@ -72,6 +75,7 @@ runs:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
Expand All @@ -89,6 +93,7 @@ runs:
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Find package_name
if: ${{ !inputs.package-names }}
id: find-package-name
shell: bash
run: |
Expand All @@ -113,6 +118,7 @@ runs:
run: bundle exec ruby ${{ github.action_path }}/run.rb
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
PACKAGE_NAMES: ${{ inputs.package-names }}
PACKAGE_NAME: ${{ steps.find-package-name.outputs.package-name }}
PACKAGE_VERSION: ${{ steps.find-version.outputs.version }}
OWNER: ${{ inputs.owner }}
Expand Down
18 changes: 12 additions & 6 deletions deploy/lib/deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ def repos
def report_results
return unless failed_repos.any?

raise "[PCO-Release]: Failed in the following repos:\n- #{failed_repos.map(&:name).join("\n- ")}"
failed_repos_list =
failed_repos.map { |r| "#{r.name}: #{r.package_name}" }.join("\n- ")
raise "[PCO-Release]: Failed in the following repos:\n- #{failed_repos_list}"
end

def package_name
config.package_name
def package_names
config.package_names
end

def version
Expand All @@ -58,18 +60,22 @@ def log(message)
end

def log_deployer_start
log "Updating #{package_name} to #{version} in the following repositories: #{repos.map(&:name).join(", ")}"
log "Updating #{package_names.join(", ")} to #{version} in the following repositories: #{repo_names.join(", ")}"
end

def log_repo_start(repo)
log "updating #{package_name} in #{repo.name}"
log "updating #{repo.package_name} in #{repo.name}"
end

def log_result(repo)
if repo.success?
log repo.success_message
else
log "Failed to update #{package_name} in #{repo.name}: #{repo.error_message}"
log "Failed to update #{repo.package_name} in #{repo.name}: #{repo.error_message}"
end
end

def repo_names
repos.map(&:name).uniq
end
end
6 changes: 3 additions & 3 deletions deploy/lib/deployer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Config
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
github_token:,
owner:,
package_name:,
version:,
package_names:,
change_method: "pr",
branch_name: "main",
automerge: false,
Expand All @@ -16,7 +16,6 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
)
@github_token = github_token
@owner = owner
@package_name = package_name
@version = version
@automerge = automerge
@only = only
Expand All @@ -26,11 +25,12 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
@branch_name = branch_name
@change_method = change_method
@allow_major = allow_major
@package_names = package_names
end

attr_reader :github_token,
:owner,
:package_name,
:package_names,
:version,
:automerge,
:branch_name,
Expand Down
11 changes: 4 additions & 7 deletions deploy/lib/deployer/repo.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Deployer
class Repo
def initialize(name, config:, updater: nil)
def initialize(name, package_name:, config:, updater: nil)
@name = name
@config = config
@package_name = package_name
@updater = updater || default_updater
end

Expand Down Expand Up @@ -35,7 +36,7 @@ def failure?
!success?
end

attr_reader :name, :error_message
attr_reader :name, :error_message, :package_name

private

Expand All @@ -44,7 +45,7 @@ def failure?
attr_writer :error_message

def default_updater
updater_class.new(name, config: config)
updater_class.new(name, config: config, package_name: package_name)
end

def updater_class
Expand All @@ -58,10 +59,6 @@ def updater_class
end
end

def package_name
config.package_name
end

def version
config.version
end
Expand Down
9 changes: 3 additions & 6 deletions deploy/lib/deployer/repo/base_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
class Deployer
class Repo
class BaseUpdater
def initialize(name, config:)
def initialize(name, config:, package_name:)
@name = name
@config = config
@package_name = package_name
end

def run
Expand All @@ -28,7 +29,7 @@ def pr_url

protected

attr_reader :name, :config
attr_reader :name, :config, :package_name

def make_changes
raise NotImplementedError
Expand Down Expand Up @@ -134,10 +135,6 @@ def owner
config.owner
end

def package_name
config.package_name
end

def version
config.version
end
Expand Down
6 changes: 5 additions & 1 deletion deploy/lib/deployer/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def initialize(repos)
attr_reader :repos

def to_json(_opts = {})
as_json.to_json
end

def as_json(_opts = {})
{
failed_repos:
failed_repos.map do |repo|
Expand All @@ -18,7 +22,7 @@ def to_json(_opts = {})
successful_repos.map do |repo|
{ name: repo.name, pr_number: repo.pr_number, pr_url: repo.pr_url }
end
}.to_json
}
end

def output_to_github
Expand Down
20 changes: 12 additions & 8 deletions deploy/lib/deployer/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ def initialize(config)
end

def find
find_repos.map { |repo| Repo.new(repo["name"], config: config) }
package_names.flat_map do |package_name|
find_repos(package_name).map do |repo|
Repo.new(repo["name"], package_name: package_name, config: config)
end
end
end

private
Expand All @@ -16,8 +20,8 @@ def owner
config.owner
end

def package_name
config.package_name
def package_names
config.package_names
end

def only
Expand All @@ -28,24 +32,24 @@ def client
config.client
end

def find_repos
def find_repos(package_name)
repos = client.org_repos(owner)
return repos.select { |repo| only.include?(repo.name) } if only.any?

select_packages_that_consume_package(repos)
select_packages_that_consume_package(repos, package_name)
end

def select_packages_that_consume_package(repos)
def select_packages_that_consume_package(repos, package_name)
repos.select do |repo|
next false if repo["archived"]
next true if config.include.include?(repo["name"])
next false if config.exclude.include?(repo["name"])

consumer_of_package?(repo)
consumer_of_package?(repo, package_name)
end
end

def consumer_of_package?(repo)
def consumer_of_package?(repo, package_name)
response =
client.contents("#{owner}/#{repo["name"]}", path: "package.json")
contents = JSON.parse(Base64.decode64(response.content))
Expand Down
43 changes: 26 additions & 17 deletions deploy/run.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
require_relative "./lib/deployer"

config =
Deployer::Config.new(
github_token: ENV["GITHUB_TOKEN"],
package_name: ENV["PACKAGE_NAME"],
version: ENV["PACKAGE_VERSION"],
owner: ENV["OWNER"],
only: ENV["ONLY"].split(","),
automerge: ENV["AUTOMERGE"] == "true",
upgrade_commands: JSON.parse(ENV["UPGRADE_COMMANDS"]),
branch_name: ENV["BRANCH_NAME"],
change_method: ENV["CHANGE_METHOD"],
include: ENV["INCLUDE"].split(","),
exclude: ENV["EXCLUDE"].split(","),
allow_major: ENV["ALLOW_MAJOR"] == "true"
)
reporter = Deployer.new(config).run
COMMON_CONFIG = {
github_token: ENV["GITHUB_TOKEN"],
version: ENV["PACKAGE_VERSION"],
owner: ENV["OWNER"],
only: ENV["ONLY"].split(","),
automerge: ENV["AUTOMERGE"] == "true",
upgrade_commands: JSON.parse(ENV["UPGRADE_COMMANDS"]),
branch_name: ENV["BRANCH_NAME"],
change_method: ENV["CHANGE_METHOD"],
include: ENV["INCLUDE"].split(","),
exclude: ENV["EXCLUDE"].split(","),
allow_major: ENV["ALLOW_MAJOR"] == "true"
}

reporter.output_to_github
def run_for_packages
config = Deployer::Config.new(**COMMON_CONFIG, package_names: package_names)
reporter = Deployer.new(config).run
reporter.output_to_github
end

def package_names
return ENV["PACKAGE_NAMES"].split(",") if ENV["PACKAGE_NAMES"]

[ENV["PACKAGE_NAME"]]
end

run_for_packages
4 changes: 2 additions & 2 deletions deploy/spec/deployer/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
Deployer::Config.new(
github_token: "",
owner: "planningcenter",
package_name: "@planningcenter/tapestry-react",
package_names: ["@planningcenter/tapestry-react"],
version: "1.0.1"
)

expect(config.github_token).to eq("")
expect(config.owner).to eq("planningcenter")
expect(config.package_name).to eq("@planningcenter/tapestry-react")
expect(config.package_names).to eq(["@planningcenter/tapestry-react"])
expect(config.version).to eq("1.0.1")
expect(config.automerge).to eq(false)
expect(config.only).to eq([])
Expand Down
22 changes: 13 additions & 9 deletions deploy/spec/deployer/repo/base_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
instance_double(
Deployer::Config,
version: "1.0.1",
package_name: "test"
package_names: ["test"]
)
updater = described_class.new("test", config: config)
updater =
described_class.new("repo", config: config, package_name: "test")
allow(File).to receive(:exist?).and_return(true)
allow(YAML).to receive(:load_file).and_return(
"upgrade_command" =>
Expand All @@ -27,11 +28,12 @@
instance_double(
Deployer::Config,
version: "1.0.1",
package_name: "test",
package_names: ["test"],
upgrade_commands: {
}
)
updater = described_class.new("test", config: config)
updater =
described_class.new("repo", config: config, package_name: "test")
allow(File).to receive(:exist?).and_return(true)
allow(YAML).to receive(:load_file).and_return({})

Expand All @@ -45,12 +47,13 @@
instance_double(
Deployer::Config,
version: "1.0.1",
package_name: "test",
package_names: ["test"],
upgrade_commands: {
"test" => "some other upgrade"
"repo" => "some other upgrade"
}
)
updater = described_class.new("test", config: config)
updater =
described_class.new("repo", config: config, package_name: "test")
allow(File).to receive(:exist?).and_return(false)

expect(updater.send(:upgrade_command)).to eq(
Expand All @@ -65,12 +68,13 @@
instance_double(
Deployer::Config,
version: "1.0.1",
package_name: "test",
package_names: ["test"],
upgrade_commands: {
"other" => "some other upgrade"
}
)
updater = described_class.new("test", config: config)
updater =
described_class.new("repo", config: config, package_name: "test")
allow(File).to receive(:exist?).and_return(false)

expect(updater.send(:upgrade_command)).to eq("yarn upgrade [email protected]")
Expand Down
Loading

0 comments on commit 24471e2

Please sign in to comment.