summaryrefslogtreecommitdiff
path: root/lib/oxidized/hook/githubrepo.rb
blob: 25b2cd04516d87b761f788f982e4c926e8a6ec30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class GithubRepo < Oxidized::Hook
  def validate_cfg!
    cfg.has_key?('remote_repo') or raise KeyError, 'remote_repo is required'
  end

  def run_hook(ctx)
    credentials = Rugged::Credentials::UserPassword.new(username: cfg.username, password: cfg.password)
    repo = Rugged::Repository.new(Oxidized.config.output.git.repo)
    log "Pushing local repository(#{repo.path})..."
    remote = repo.remotes['origin'] || repo.remotes.create('origin', cfg.remote_repo)
    log "to remote: #{remote.url}"

    fetch_and_merge_remote(repo, credentials)

    remote.push([repo.head.name], credentials: credentials)
  end

  def fetch_and_merge_remote(repo, credentials)
    repo.fetch('origin', [repo.head.name], credentials: credentials)

    their_branch = repo.branches["origin/master"] or return

    merge_index = repo.merge_commits(repo.head.target_id, their_branch.target_id)

    log("Conflicts detected", :warn) if merge_index.conflicts?

    Rugged::Commit.create(repo, {
      parents: [repo.head.target, their_branch.target],
      tree: merge_index.write_tree(repo),
      message: "Merge remote-tracking branch '#{their_branch.name}'",
      update_ref: "HEAD"
    })
  end
end