summaryrefslogtreecommitdiff
path: root/lib/oxidized/hook
diff options
context:
space:
mode:
authorytti <saku@ytti.fi>2016-01-04 18:50:54 +0200
committerytti <saku@ytti.fi>2016-01-04 18:50:54 +0200
commit0eeba91b426c8b4a6335a88da9c65ba38e5fac15 (patch)
tree02ba46c4f7d43b5721f3eb7de38599e0e019e5cf /lib/oxidized/hook
parentf112dfa0c604ae1c990f6411a002806924c00bf3 (diff)
parente41f7b429901eb38ad785ad1fc2527dd41f35959 (diff)
Merge pull request #250 from Shopify/master0.10.0
refactoring, test coverage and github hook
Diffstat (limited to 'lib/oxidized/hook')
-rw-r--r--lib/oxidized/hook/githubrepo.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb
new file mode 100644
index 0000000..d10b51e
--- /dev/null
+++ b/lib/oxidized/hook/githubrepo.rb
@@ -0,0 +1,57 @@
+class GithubRepo < Oxidized::Hook
+ def validate_cfg!
+ cfg.has_key?('remote_repo') or raise KeyError, 'remote_repo is required'
+ end
+
+ def run_hook(ctx)
+ 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)
+
+ remote.push([repo.head.name], credentials: credentials)
+ end
+
+ def fetch_and_merge_remote(repo)
+ result = repo.fetch('origin', [repo.head.name], credentials: credentials)
+ log result.inspect, :debug
+
+ unless result[:total_deltas] > 0
+ log "nothing recieved after fetch", :debug
+ return
+ end
+
+ their_branch = repo.branches["origin/master"]
+
+ 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
+
+ private
+
+ def credentials
+ @credentials ||= if cfg.has_key?('username') && cfg.has_key?('password')
+ log "Using https auth", :debug
+ Rugged::Credentials::UserPassword.new(username: cfg.username, password: cfg.password)
+ else
+ log "Using ssh auth", :debug
+ Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
+ end
+ end
+
+end