Class: Gitomator::Task::MakeRepos

Inherits:
BaseReposTask show all
Defined in:
lib/gitomator/task/make_repos.rb

Instance Attribute Summary collapse

Attributes inherited from BaseReposTask

#local_dir, #repos

Attributes inherited from BaseTask

#context, #logger

Instance Method Summary collapse

Methods inherited from BaseReposTask

#after_processing_all_repos, #before_processing_any_repos, #process_repo_error, #run

Methods inherited from BaseTask

#ci, #git, #hosting, #run, #tagging

Constructor Details

#initialize(context, repos, opts = {}) ⇒ MakeRepos

Returns a new instance of MakeRepos.

Parameters:

  • context
  • repos (Array<String>)
  • opts (Hash<Symbol,Object>) (defaults to: {})

Options Hash (opts):

  • :repo_properties (Hash<Symbol,Object>)
    • For example, :private, :description, :has_issues, etc.

  • :source_repo (String)
    • The name of a repo that will be the “starting point” of all the created repos.

  • :update_existing (Boolean)
    • Update existing repos, by pushing latest commit(s) from the source_repo.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitomator/task/make_repos.rb', line 23

def initialize(context, repos, opts={})
  super(context, repos)
  @opts  = opts

  @source_repo     = opts[:source_repo]
  @update_existing = opts[:update_existing] || false
  @repo_properties = opts[:repo_properties] || {}
  @repo_properties = @repo_properties.map {|k,v| [k.to_sym,v] }.to_h

  before_processing_any_repos do
    logger.info "About to create/update #{repos.length} repo(s) ..."

    if source_repo
      tmp_dir = Dir.mktmpdir('Gitomator_')
      Gitomator::Task::CloneRepos.new(context, [source_repo], tmp_dir).run()
      repo_name = hosting.resolve_repo_name(source_repo)
      @source_repo_local_root = File.join(tmp_dir, repo_name)
    end
  end
end

Instance Attribute Details

#repo_propertiesObject (readonly)

Returns the value of attribute repo_properties.



12
13
14
# File 'lib/gitomator/task/make_repos.rb', line 12

def repo_properties
  @repo_properties
end

#source_repoObject (readonly)

Returns the value of attribute source_repo.



10
11
12
# File 'lib/gitomator/task/make_repos.rb', line 10

def source_repo
  @source_repo
end

#source_repo_local_rootObject (readonly)

Returns the value of attribute source_repo_local_root.



13
14
15
# File 'lib/gitomator/task/make_repos.rb', line 13

def source_repo_local_root
  @source_repo_local_root
end

#update_existingObject (readonly)

Returns the value of attribute update_existing.



11
12
13
# File 'lib/gitomator/task/make_repos.rb', line 11

def update_existing
  @update_existing
end

Instance Method Details

#process_repo(repo_name, index) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitomator/task/make_repos.rb', line 46

def process_repo(repo_name, index)
  repo = hosting.read_repo(repo_name)

  # If the repo doesn't exist, create it ...
  if repo.nil?
    logger.debug "Creating new repo #{repo_name} ..."
    repo = hosting.create_repo(repo_name, repo_properties)
    push_commits(repo, source_repo_local_root)

  # If the repo exists, we might need to push changes, or update its properties
  else
    if update_existing
      push_commits(repo, source_repo_local_root)
    end
    update_properties_if_needed(repo, repo_properties)
  end
end

#push_commits(hosted_repo, local_repo) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/gitomator/task/make_repos.rb', line 65

def push_commits(hosted_repo, local_repo)
  if local_repo
    logger.debug "Pushing commits from #{local_repo} to #{hosted_repo.name} "
    git.set_remote(local_repo, hosted_repo.name, hosted_repo.url, {create: true})
    git.command(local_repo, "push #{hosted_repo.name} HEAD:master")
  end
end

#update_properties_if_needed(repo, props) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/gitomator/task/make_repos.rb', line 74

def update_properties_if_needed(repo, props)
  p = repo.properties
  diff = props.select {|k,v| p.has_key?(k) && p[k] != v}
  unless(diff.empty?)
    logger.debug "Updating #{repo.name} properties #{diff}"
    hosting.update_repo(repo.name, diff)
  end
end