Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remote branch #3208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- dlink: added support for 'enable admin' before getting configuration, if enable=true (@as8net)
- dlinknextgen: strip uptime and ntp update time from config
- Updated slackdiff.rb to use slack_ruby_client instead of slack-api (@Punicaa)
- remote_repo: Add ability to specify remote branch name (@ianbarrere)

### Fixed
- fixed prompt for vyos/vyatta to allow logins with non-priviliged accounts. Fixes #3111 (@h-lopez)
Expand Down
11 changes: 11 additions & 0 deletions docs/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ hooks:
privatekey: /root/.ssh/id_rsa
```

A remote branch can be given if different from the default of "master" like so:

```yaml
hooks:
push_to_remote:
type: githubrepo
events: [post_store]
remote_repo: git@git.intranet:oxidized/test.git
remote_branch: main
```

## Hook type: awssns

The `awssns` hook publishes messages to AWS SNS topics. This allows you to notify other systems of device configuration changes, for example a config orchestration pipeline. Multiple services can subscribe to the same AWS topic.
Expand Down
29 changes: 23 additions & 6 deletions lib/oxidized/hook/githubrepo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,38 @@ def run_hook(ctx)
end
remote = repo.remotes['origin']

fetch_and_merge_remote(repo, creds)

remote.push([repo.head.name], credentials: creds)
remote_branch = cfg.has_key?('remote_branch') ? cfg.remote_branch : 'master'
log("Using remote branch name #{remote_branch}", :debug)
fetch_and_merge_remote(repo, creds, remote_branch)

# can't seem to push to a different branch using Rugged::Repository, so we create a
# ref of the branch we want to push to here and clean up after
if repo.references.each_name.include? 'refs/heads/' + remote_branch
cleanup = false
ref = repo.references["refs/heads/" + remote_branch]
else
cleanup = true
ref = repo.references.create("refs/heads/" + remote_branch, repo.head.target_id)
end
pushed = remote.push([ref.name], credentials: creds)
if pushed == {}
log("Push successful", :debug)
else
log("Unexpected response from remote repo: #{pushed}", :warn)
end
repo.references.delete(ref.name) if cleanup == true
end

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

unless result[:total_deltas].positive?
log "nothing received after fetch", :debug
return
end

their_branch = repo.branches["origin/master"]
their_branch = repo.branches["origin/" + remote_branch]

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

Expand Down
6 changes: 3 additions & 3 deletions spec/hook/githubrepo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0))
repo.expects(:branches).never
repo.expects(:head).returns(repo_head)
_(gr.fetch_and_merge_remote(repo, credentials)).must_be_nil
_(gr.fetch_and_merge_remote(repo, credentials, 'master')).must_be_nil
end

describe "when there is update considering conflicts" do
Expand All @@ -60,7 +60,7 @@
their_branch.expects(:name).returns("origin/master")
merge_index.expects(:conflicts?).returns(true)
Rugged::Commit.expects(:create).never
_(gr.fetch_and_merge_remote(repo, credentials)).must_be_nil
_(gr.fetch_and_merge_remote(repo, credentials, 'master')).must_be_nil
end

it "should merge when there is no conflict" do
Expand All @@ -75,7 +75,7 @@
tree: "tree",
message: "Merge remote-tracking branch 'origin/master'",
update_ref: "HEAD").returns(1)
_(gr.fetch_and_merge_remote(repo, credentials)).must_equal 1
_(gr.fetch_and_merge_remote(repo, credentials, 'master')).must_equal 1
end
end
end
Expand Down
Loading