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

Select branch to fetch / remove unreachable commits in rake task / add support for gitea webhook #23

Open
wants to merge 4 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
12 changes: 10 additions & 2 deletions app/controllers/git_mirror_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ def gitlab
head found ? 202 : 404
end

# process github webhook request
def github
event = request.headers["x-github-event"]
common_webhook("x-github-event")
end

def gitea
common_webhook("X-Gitea-Event")
end

# process github webhook request
private def common_webhook(event_header)
event = request.headers[event_header]
unless request.post? && event
head 400
return
Expand Down
38 changes: 33 additions & 5 deletions app/models/repository/git_mirror.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Repository::GitMirror < Repository::Git
before_validation :validate_and_normalize_url, on: [:create, :update]
before_validation :set_defaults, on: :create
after_validation :init_repo, on: :create
after_commit :fetch, on: :create
after_validation :validate_branches, on: [:create, :update]
after_commit :fetch, on: [:create, :update]

after_validation :update_remote_url, on: :update

Expand All @@ -18,6 +19,23 @@ class Repository::GitMirror < Repository::Git
repository.new_record? || RedmineGitMirror::Settings.url_change_allowed?
}

safe_attributes 'extra_info', :if => lambda {|repository, user|
repository.new_record? || RedmineGitMirror::Settings.branches_to_fetch_change_allowed?
}

safe_attributes 'branches_to_fetch', :if => lambda { |repository, user|
repository.new_record? || RedmineGitMirror::Settings.branches_to_fetch_change_allowed?
}

def branches_to_fetch
return "*" unless extra_info && extra_info["branches_to_fetch"]
extra_info["branches_to_fetch"]
end

def refspecs
branches_to_fetch.split(/,/).collect { |m|m.strip }.map { |branch| "+refs/heads/%s:refs/heads/%s" % [branch, branch] }
end

private def update_remote_url
return unless self.errors.empty?
return unless self.url_changed?
Expand Down Expand Up @@ -99,6 +117,14 @@ class Repository::GitMirror < Repository::Git
end
end

private def validate_branches
err = RedmineGitMirror::Git.fetch(root_url, url, refspecs, dry_run=true)
if err
errors.add :branches_to_fetch, err
return
end
end

private def set_defaults
return unless self.errors.empty? && !url.to_s.empty?

Expand All @@ -117,7 +143,7 @@ class Repository::GitMirror < Repository::Git
private def init_repo
return unless self.errors.empty?

err = RedmineGitMirror::Git.init(root_url, url)
err = RedmineGitMirror::Git.init(root_url, url, refspecs)
errors.add :url, err if err
end

Expand All @@ -132,14 +158,16 @@ def fetch

puts "Fetching repo #{url} to #{root_url}"

err = RedmineGitMirror::Git.fetch(root_url, url)
err = RedmineGitMirror::Git.fetch(root_url, url, refspecs)
Rails.logger.warn 'Err with fetching: ' + err if err

remove_unreachable_commits
if RedmineGitMirror::Settings.remove_unreachable_on_fetch?
remove_unreachable_commits
end
fetch_changesets(true)
end

private def remove_unreachable_commits
def remove_unreachable_commits
commits, e = RedmineGitMirror::Git.unreachable_commits(root_url)
if e
Rails.logger.warn 'Err when fetching unreachable commits: ' + e
Expand Down
20 changes: 20 additions & 0 deletions app/views/git_mirror/_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ For better look i use small hack, sorry about that
%>
</p>

<p>
<label for="branches_to_fetch_change_allowed">
Allow users to change the branches to fetch
</label>
<%= hidden_field_tag "settings[branches_to_fetch_change_allowed]", 0 %>
<%= check_box_tag "settings[branches_to_fetch_change_allowed]", 1, RedmineGitMirror::Settings.branches_to_fetch_change_allowed?,
:id => 'branches_to_fetch_change_allowed'
%>
</p>

<p>
<label for="remove_unreachable_on_fetch">
Remove unreachable commits when fetching commits from server
</label>
<%= hidden_field_tag "settings[remove_unreachable_on_fetch]", 0 %>
<%= check_box_tag "settings[remove_unreachable_on_fetch]", 1, RedmineGitMirror::Settings.remove_unreachable_on_fetch?,
:id => 'remove_unreachable_on_fetch'
%>
</p>

</fieldset>

<fieldset class="box tabular" id="git_mirror_schemas">
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
en:
text_git_mirror_url_note: URL of remote repository.
text_git_mirror_branches_to_fetch_note: |-
Fetch specified branches (comma-separated, globbing allowed, e.g. "master,next*" for master and optional next and similar)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
match 'sys/git_mirror/fetch', to: 'git_mirror#fetch', via: [:get, :post]
match 'sys/git_mirror/gitlab', to: 'git_mirror#gitlab', via: [:post]
match 'sys/git_mirror/github', to: 'git_mirror#github', via: [:post]
match 'sys/git_mirror/gitea', to: 'git_mirror#gitea', via: [:post]
24 changes: 16 additions & 8 deletions lib/redmine_git_mirror/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def prune(path)
e
end

def init(clone_path, url)
def init(clone_path, url, refspec_list)
url = RedmineGitMirror::URL.parse(url)
RedmineGitMirror::SSH.ensure_host_known(url.host) if url.uses_ssh?

Expand All @@ -49,19 +49,23 @@ def init(clone_path, url)
return e if e
end

set_fetch_refs(clone_path, [
'+refs/heads/*:refs/heads/*',
'+refs/tags/*:refs/tags/*',
# uncomment next line if you want to show (gitlab) merge requests as braches in redmine
set_fetch_refs(clone_path, refspec_list + [
#'+refs/tags/*:refs/tags/*',
# # next line if you want to show (gitlab) merge requests as braches in redmine
# '+refs/merge-requests/*/head:refs/heads/MR-*',
])
end

def fetch(clone_path, url)
e = RedmineGitMirror::Git.init(clone_path, url)
def fetch(clone_path, url, branch_list, dry_run=false)
e = RedmineGitMirror::Git.init(clone_path, url, branch_list)
return e if e

_, e = git "--git-dir", clone_path, "fetch", "--prune", "--all"
unless dry_run
_, e = git "--git-dir", clone_path, "fetch", "--prune", "--all"
else
_, e = git "--git-dir", clone_path, "fetch", "--dry-run", "--prune", "--all"
end

e
end

Expand Down Expand Up @@ -125,6 +129,10 @@ def set_remote_url(clone_path, url)

msg.lines.first.strip
end

private def refspecs_from_branch_list(branch_list)
branch_list.map { |branch| '+refs/heads/%s:refs/heads/%s' % [branch, branch] }
end
end
end
end
8 changes: 8 additions & 0 deletions lib/redmine_git_mirror/patches/repositories_helper_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def git_mirror_field_tags(form, repository)
:disabled => !repository.safe_attribute?('url'),
) +
content_tag('em', l(:text_git_mirror_url_note), :class => 'info')
) +
content_tag('p', form.text_field(
:branches_to_fetch,
:size => 60,
:required => false,
:disabled => !repository.safe_attribute?('branches_to_fetch'), name: 'repository[extra_info][branches_to_fetch]'
) +
content_tag('em', l(:text_git_mirror_branches_to_fetch_note), :class => 'info')
)
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/redmine_git_mirror/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Settings
:url_change_allowed => false,
:prevent_multiple_clones => true,
:search_clones_in_all_schemes => true,
:branches_to_fetch_change_allowed => true,
}.freeze

class << self
Expand Down Expand Up @@ -37,6 +38,18 @@ def search_clones_in_all_schemes?
s == true || s.to_s == '1'
end

def branches_to_fetch_change_allowed?
s = self[:branches_to_fetch_change_allowed] || false

s == true || s.to_s == '1'
end

def remove_unreachable_on_fetch?
s = self[:remove_unreachable_on_fetch] || false

s == true || s.to_s == '1'
end

private def [](key)
key = key.intern if key.is_a?(String)
settings = Setting[:plugin_redmine_git_mirror] || {}
Expand Down
18 changes: 18 additions & 0 deletions lib/tasks/remove_unreachable_commits.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace :redmine do
namespace :plugins do
namespace :git_mirror do
desc <<-END_DESC
Clear tags table.

rake redmine:plugins:git_mirror:remove_unreachable_commits RAILS_ENV="production"
END_DESC

task :remove_unreachable_commits => :environment do
Repository::GitMirror.find_each do |repo|
puts "Removing unreachable commits from %s repo" % repo.identifier
repo.remove_unreachable_commits
end
end
end
end
end