Class: GithubRepo

Inherits:
Oxidized::Hook show all
Defined in:
lib/oxidized/hook/githubrepo.rb

Instance Attribute Summary

Attributes inherited from Oxidized::Hook

#cfg

Instance Method Summary collapse

Methods inherited from Oxidized::Hook

#log

Instance Method Details

#fetch_and_merge_remote(repo, creds) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/oxidized/hook/githubrepo.rb', line 48

def fetch_and_merge_remote(repo, creds)
  result = repo.fetch('origin', [repo.head.name], credentials: creds)
  log result.inspect, :debug

  their_branch = remote_branch(repo)

  unless their_branch
    log 'remote branch does not exist yet, nothing to merge', :debug
    return
  end

  result = repo.merge_analysis(their_branch.target_id)

  if result.include? :up_to_date
    log 'nothing to merge', :debug
    return
  end

  log "merging fetched branch #{their_branch.name}", :debug

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

  if merge_index.conflicts?
    log("Conflicts detected, skipping Rugged::Commit.create", :warn)
    return
  end

  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

#run_hook(ctx) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oxidized/hook/githubrepo.rb', line 8

def run_hook(ctx)
  unless ctx.node.repo
    log "Oxidized output is not git, can't push to remote", :error
    return
  end
  repo  = Rugged::Repository.new(ctx.node.repo)
  creds = credentials(ctx.node)
  url   = remote_repo(ctx.node)

  if url.nil? || url.empty?
    log "No repository defined for #{ctx.node.group}/#{ctx.node.name}", :error
    return
  end

  log "Pushing local repository(#{repo.path})..."
  log "to remote: #{url}"

  if repo.remotes['origin'].nil?
    repo.remotes.create('origin', url)
  elsif repo.remotes['origin'].url != url
    repo.remotes.set_url('origin', url)
  end
  remote = repo.remotes['origin']

  begin
    fetch_and_merge_remote(repo, creds)
    remote.push([repo.head.name], credentials: creds)
  rescue Rugged::NetworkError => e
    if e.message == 'unsupported URL protocol'
      log "Rugged does not support the git URL '#{url}'.", :warn
      unless Rugged.features.include?(:ssh)
        log 'You may need to install Rugged with ssh support ' \
            '(gem install rugged -- --with-ssh)', :warn
      end
    end
    # re-raise exception for the calling method
    raise
  end
end

#validate_cfg!Object

Raises:

  • (KeyError)


4
5
6
# File 'lib/oxidized/hook/githubrepo.rb', line 4

def validate_cfg!
  raise KeyError, 'hook.remote_repo is required' unless cfg.has_key?('remote_repo')
end