Skip to content

Commit

Permalink
Update miq_bot for cross-repo-tests github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Feb 8, 2022
1 parent 6276c39 commit e0f7f28
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
29 changes: 17 additions & 12 deletions lib/github_service/commands/cross_repo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Commands
#
# - validate the command
# - create a branch
# - create a commit with the travis.yml changes
# - create a commit with the .github/workflows/ci.yaml changes
# - push said branch to the origin
# - create a pull request for that branch
#
Expand All @@ -37,7 +37,7 @@ module Commands
# @miq-bot cross-repo-test manageiq-api,manageiq-ui-classic#5678 \
# including Fryguy/more_core_extensions@feature,Fryguy/linux_admin@feature
#
# will create a commit with the .travis.yml changes:
# will create a commit with the .github/workflows/ci.yaml changes:
#
# @@ -14,6 +14,6 @@ matrix:
# fast_finish: true
Expand Down Expand Up @@ -113,8 +113,8 @@ def branch_name
def run_tests
ensure_test_repo_clone
create_cross_repo_test_branch
update_travis_yaml_content
commit_travis_yaml_changes
update_github_workflow_yaml_content
commit_github_workflow_yaml_changes
push_commit_to_remote
create_cross_repo_test_pull_request
end
Expand Down Expand Up @@ -270,25 +270,30 @@ def create_cross_repo_test_branch
#
# https://github.com/ManageIQ/manageiq/blob/06de0607/lib/git_worktree.rb#L102-L110
#
def update_travis_yaml_content
raw_yaml = rugged_repo.blob_at(branch_ref.target.oid, ".travis.yml").content
def update_github_workflow_yaml_content
raw_yaml = rugged_repo.blob_at(branch_ref.target.oid, ".github/workflows/ci.yaml").content
content = YAML.safe_load(raw_yaml)

content["env"] = {} unless content["env"]
content["env"]["global"] = ["REPOS=#{repos.join(',')}"]
content["env"]["matrix"] = test_repos.map { |repo| "TEST_REPO=#{repo}" }
content["jobs"] = test_repos.each_with_object({}) do |test_repo, result|
result[test_repo] = {
"uses" => "ManageIQ/manageiq-cross_repo/.github/workflows/manageiq_cross_repo.yaml@master",
"with" => {
"test-repo" => test_repo,
"repos" => repos.join(",")
}
}
end

entry = {}
entry[:path] = ".travis.yml"
entry[:path] = ".github/workflows/ci.yaml"
entry[:oid] = @rugged_repo.write(content.to_yaml, :blob)
entry[:mode] = 0o100644
entry[:mtime] = Time.now.utc

rugged_index.add(entry)
# rugged_index.write (don't do this...)
end

def commit_travis_yaml_changes
def commit_github_workflow_yaml_changes
bot = self.class.bot_name
author = {:name => issuer, :email => user_email(issuer), :time => Time.now.utc}
committer = {:name => bot, :email => self.class.bot_email, :time => Time.now.utc}
Expand Down
77 changes: 37 additions & 40 deletions spec/lib/github_service/commands/cross_repo_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

attr_reader :sandbox_dir, :cross_repo_remote, :cross_repo_clone

let(:suite_sandbox_dir) { Rails.root.join("spec", "tmp", "sandbox").to_s }
let(:travis_yml_path) { File.join(cross_repo_clone, ".travis.yml") }
let(:suite_sandbox_dir) { Rails.root.join("spec", "tmp", "sandbox").to_s }
let(:github_workflow_yml_path) { File.join(cross_repo_clone, ".github/workflow/ci.yaml") }

before do |example|
FileUtils.mkdir_p suite_sandbox_dir
Expand All @@ -33,31 +33,22 @@
# test_repo_url stubbed above.
stub_const("::Repo::BASE_PATH", Pathname.new(suite_sandbox_dir))

default_travis_yaml_content = <<~YAML
dist: xenial
language: ruby
rvm:
- 2.5.5
cache:
bundler: true
addons:
postgresql: '10'
apt:
packages:
- libarchive-dev
script: bundle exec manageiq-cross_repo
matrix:
fast_finish: true
env:
global:
- REPOS=
matrix:
- TEST_REPO=manageiq
default_github_workflow_yaml_content = <<~YAML
name: Cross Repo Tests
on: [push, pull_request]
jobs:
manageiq:
uses: agrare/manageiq-cross_repo/.github/workflows/manageiq_cross_repo.yaml@github_actions
with:
test-repo: ManageIQ/manageiq@master
repos: ManageIQ/manageiq@master
YAML

tmp_repo = GitRepoHelper::TmpRepo.generate @cross_repo_source do
add_file ".travis.yml", default_travis_yaml_content
commit "add .travis.yml"
add_file ".github/workflows/ci.yaml", default_github_workflow_yaml_content
commit "add .github/workflows/ci.yaml"
tag "v1.0"
end

Expand Down Expand Up @@ -378,7 +369,7 @@ def assert_execute(valid: true)

it "clones the repo as a bare repo" do
expect(Dir.exist?(cross_repo_clone)).to be_truthy
expect(File.exist?(travis_yml_path)).to be_falsey
expect(File.exist?(github_workflow_yml_path)).to be_falsey
end

it "creates a new branch (stays on master)" do
Expand All @@ -389,14 +380,20 @@ def assert_execute(valid: true)
expect(repo.head.name.sub(/^refs\/heads\//, '')).to eq "master"
end

it "updates the .travis.yml" do
repo = subject.rugged_repo
branch = repo.branches["origin/#{subject.branch_name}"]
travis_yml_content = repo.blob_at(branch.target.oid, ".travis.yml").content
content = YAML.safe_load(travis_yml_content)
it "updates the .github/workflows/ci.yaml" do
repo = subject.rugged_repo
branch = repo.branches["origin/#{subject.branch_name}"]
github_workflow_yml_content = repo.blob_at(branch.target.oid, ".github/workflows/ci.yaml").content
content = YAML.safe_load(github_workflow_yml_content)

expect(content["jobs"].count).to eq(2)
expect(content["jobs"].keys).to match_array(["foo", "bar"])

expect(content["jobs"]["foo"]["with"]["test-repo"]).to eq("foo")
expect(content["jobs"]["bar"]["with"]["test-repo"]).to eq("bar")

expect(content["env"]["global"]).to eq ["REPOS=repo1,repo2"]
expect(content["env"]["matrix"]).to eq ["TEST_REPO=foo", "TEST_REPO=bar"]
expect(content["jobs"]["foo"]["with"]["repos"]).to eq("repo1,repo2")
expect(content["jobs"]["bar"]["with"]["repos"]).to eq("repo1,repo2")
end

it "commits the changes" do
Expand All @@ -408,17 +405,17 @@ def assert_execute(valid: true)
expect(commit.committer[:name]).to eq("rspec_bot")
expect(commit.committer[:email]).to eq("no_bot_email@example.com")

commit_content = repo.blob_at(commit.oid, ".travis.yml").content
commit_content = repo.blob_at(commit.oid, ".github/workflows/ci.yaml").content

expect(commit.message).to eq <<~MSG
Running tests for NickLaMuro
From Pull Request: ManageIQ/bar#1234
MSG

expect(commit_content).to include "- REPOS=repo1,repo2"
expect(commit_content).to include "- TEST_REPO=foo"
expect(commit_content).to include "- TEST_REPO=bar"
expect(commit_content).to include "repos: repo1,repo2"
expect(commit_content).to include "test-repo: foo"
expect(commit_content).to include "test-repo: bar"
end

it "pushes the changes" do
Expand All @@ -429,7 +426,7 @@ def assert_execute(valid: true)

branch_name = subject.branch_name # branch name from cloned repo
branch = repo.branches["origin/#{branch_name}"]
commit_content = repo.blob_at(branch.target.oid, ".travis.yml").content
commit_content = repo.blob_at(branch.target.oid, ".github/workflows/ci.yaml").content

expect(branch).to_not be_nil
expect(branch.target.message).to eq <<~MSG
Expand All @@ -438,9 +435,9 @@ def assert_execute(valid: true)
From Pull Request: ManageIQ/bar#1234
MSG

expect(commit_content).to include "- REPOS=repo1,repo2"
expect(commit_content).to include "- TEST_REPO=foo"
expect(commit_content).to include "- TEST_REPO=bar"
expect(commit_content).to include "repos: repo1,repo2"
expect(commit_content).to include "test-repo: foo"
expect(commit_content).to include "test-repo: bar"
end
end

Expand Down

0 comments on commit e0f7f28

Please sign in to comment.