Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/pyxis/github_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def octokit(instance = :release_tools)
CLIENT_CONFIGS[instance][:octokit] ||= create_octokit(instance)
end

def without_auto_pagination(octokit)
current_auto_paginate = octokit.instance_variable_get(:@auto_paginate)
octokit.instance_variable_set(:@auto_paginate, false)
yield octokit
ensure
octokit.instance_variable_set(:@auto_paginate, current_auto_paginate)
end

private

def create_octokit(instance)
Expand Down
89 changes: 56 additions & 33 deletions lib/pyxis/managed_versioning/component_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,7 @@ def execute

logger.info('Updating component version', current_version: current_version, new_version: new_version)

unless Pyxis::GlobalStatus.dry_run?
GithubClient.octokit.create_ref(
Project::Reticulum.github_path,
"refs/heads/#{update_branch}",
GithubClient.octokit.branch(Project::Reticulum.github_path, Project::Reticulum.default_branch).commit.sha
)

GithubClient.octokit.update_contents(
Project::Reticulum.github_path,
version_file,
"Update #{component.component_name} version to #{present_sha(new_version)}",
version_file_content.sha,
new_version,
branch: update_branch
)

new_version_link = "[#{present_sha(new_version)}](https://github.com/#{component.github_path}/commits/#{new_version})"

pr = GithubClient.octokit.create_pull_request(
Project::Reticulum.github_path,
Project::Reticulum.default_branch,
update_branch,
"Update #{component.component_name} version to #{present_sha(new_version)}",
<<~DESCRIPTION
Update #{component.component_name} to #{new_version_link} as part of managed versioning

#{Presenter::CommitRange.new(component, current_version, new_version).as_markdown}
DESCRIPTION
)
logger.info('Created pull request', pull_request_url: pr.html_url)

Pyxis::Services::AutoMergeService.new(Project::Reticulum, pr).execute
end
update_component_version(current_version, new_version) unless Pyxis::GlobalStatus.dry_run?

logger.info('Finished component updater', current_version: current_version, new_version: new_version)
end
Expand All @@ -86,6 +54,51 @@ def update_branch

private

def update_component_version(current_version, new_version)
GithubClient.octokit.create_ref(
Project::Reticulum.github_path,
"refs/heads/#{update_branch}",
GithubClient.octokit.branch(Project::Reticulum.github_path, Project::Reticulum.default_branch).commit.sha
)

update_title = "Update #{component.component_name} version to #{present_sha(new_version)}"

GithubClient.octokit.update_contents(
Project::Reticulum.github_path,
version_file,
update_title,
version_file_content.sha,
new_version,
branch: update_branch
)

new_version_link = "[#{present_sha(new_version)}](https://github.com/#{component.github_path}/commits/#{new_version})"

pr = GithubClient.octokit.create_pull_request(
Project::Reticulum.github_path,
Project::Reticulum.default_branch,
update_branch,
update_title,
<<~DESCRIPTION
Update #{component.component_name} to #{new_version_link} as part of managed versioning

#{Presenter::CommitRange.new(component, current_version, new_version).as_markdown}
DESCRIPTION
)
logger.info('Created pull request', pull_request_url: pr.html_url)

if version_update_loop?(update_title)
GithubClient.octokit.add_comment(
Project::Reticulum.github_path,
pr.number,
'@code0-tech/delivery This component had already been updated to this version in the past. ' \
'Please check why this component is getting updated to this version again.'
)
else
Pyxis::Services::AutoMergeService.new(Project::Reticulum, pr).execute
end
end

def update_branch_exists?
GithubClient.octokit.branch(Project::Reticulum.github_path, update_branch).name == update_branch
rescue Octokit::NotFound
Expand Down Expand Up @@ -122,6 +135,16 @@ def filter_for_passing_checks(commits)
filtered_commits
end

def version_update_loop?(update_title)
commits = GithubClient.without_auto_pagination(GithubClient.octokit) do |octokit|
octokit.list_commits(
Project::Reticulum.github_path,
path: version_file
)
end
commits.reverse.any? { |commit| commit.commit.message == update_title }
end

def present_sha(sha)
Presenter::CommitSha.new(sha).as_short
end
Expand Down