Class: GitlabMirrorPull

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_mirror_pull.rb

Overview

Fetch Gitlab repositories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = File.join(File.dirname(__FILE__), "../config.yml"), log_level = Logger::ERROR) ⇒ Object

Initialize class

Parameters:

  • config (defaults to: File.join(File.dirname(__FILE__), "../config.yml"))

    Path to config file (e.g. ../config.example.yml)

  • log_level (defaults to: Logger::ERROR)

    Set log level. Possible values: ‘Logger::INFO`, `Logger::WARN`, `Logger::ERROR`, `Logger::DEBUG`



19
20
21
22
23
24
# File 'lib/gitlab_mirror_pull.rb', line 19

def initialize(config = File.join(File.dirname(__FILE__), "../config.yml"), log_level = Logger::ERROR)
  @log = Logger.new(STDOUT)
  @log.level = log_level
  @config = YAML.load_file(config)

end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/gitlab_mirror_pull.rb', line 10

def config
  @config
end

#log_levelObject

Returns the value of attribute log_level.



10
11
12
# File 'lib/gitlab_mirror_pull.rb', line 10

def log_level
  @log_level
end

Instance Method Details

#clean_html(email_body = '') ⇒ Object



26
27
28
# File 'lib/gitlab_mirror_pull.rb', line 26

def clean_html(email_body = '')
  email_body.gsub(/<\/?[^>]*>/, ' ').gsub(/\n\n+/, '\n').gsub(/^\n|\n$/, ' ')
end

#fetch_repositories(repos = nil) ⇒ Object

Fetch repositories return by ‘repositories_to_fetch`

Parameters:

  • repos (Array<String>) (defaults to: nil)

    with absolute path to repositories you want to fetch

Returns:

  • Logging infos on fetched repos



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gitlab_mirror_pull.rb', line 104

def fetch_repositories(repos = nil)
  # Init git settings
  Git.configure do |config|
    config.binary_path = "#{@config['git']['path']}"
  end

  @return_repos = []
  @error_repos = ""
  # Loop through repos and fetch it
  repos_to_fetch = repos.nil? ? self.repositories_to_fetch : repos
  repos_to_fetch.each do |repo|
    if File.directory?(repo)
      # Get branches
      g = Git.bare("#{repo}", :log => @log)
      g.remotes.each do |remote|
        # Determine which "remote" to fetch e.g. "git fetch github"
        if @config['provider'].include?("#{remote}")
          @log.info("Fetching remote #{remote} in #{repo}")
          begin
            g.remote(remote).fetch
            @return_repos << repo
          rescue => e
            @error_repos << "<b>Failed to fetch remote #{remote} in #{repo}</b>\n"
            @error_repos << "<pre>#{e.message}</pre>"
          end
        end
      end
    end
  end

  # Prepare text for error mail
  if !@error_repos.empty? && @config['mail']['send_on_error'] == true
    mail = @error_repos.to_s
    text = "<h1>Failed to fetch some repositories:</h1>\n#{mail}"
    self.send_mail(text)
  end

  # Prepare text for report mail
  if @config['mail']['send_report'] == true
    mail = @return_repos.join("<br>")
    mail_error = @error_repos.empty? ? '<br><br>Yey, no update failed!..' : "<h1>Repos failed to fetch:</h1>\n #{@error_repos.to_s}"
    text = "<h1>Repos updated:</h1>\n#{mail} #{mail_error}"
    self.send_mail(text)
  end

  @return_repos
end

#pipeline_to_trigger(repo_namespace) ⇒ Boolean

Check config if pipeline should trigger

Parameters:

  • repo_namespace (String)

    of the project e.g. group-name/your-project

Returns:

  • (Boolean)

    true/false



90
91
92
93
94
95
96
97
# File 'lib/gitlab_mirror_pull.rb', line 90

def pipeline_to_trigger(repo_namespace)
  @config['pipeline']['trigger'].each do |trigger|
    if repo_namespace.include?("#{trigger["repo"]}")
      return trigger['branch']
    end
  end
  return false
end

#repositories_to_fetchObject

Prepare list of repositories

Returns:

  • List of repositories to update using ‘git fetch`. Excludes `*.wiki` and repositories defined in `config.yml -> git -> repos`



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gitlab_mirror_pull.rb', line 56

def repositories_to_fetch
  # Find all .git Repositories - Ignore *.wiki.git
  repos = Dir.glob("#{@config['git']['repos']}/*/*{[!.wiki]}.git")

  # Build up array of NOT ignored repositories
  delete_path = []
  @config['ignore'].each do |ignored|
    path = File.join(@config['git']['repos'], ignored)
    delete_path += repos.grep /^#{path}/
    repos.delete(delete_path)
  end

  return repos - delete_path

end

#send_mail(text) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab_mirror_pull.rb', line 30

def send_mail(text)
  email_body_text = self.clean_html(text)
  email_body_html = text
  sender = "#{@config['mail']['sender']}"
  receiver = "#{@config['mail']['receiver']}"
  mail = Mail.new do
    from "#{sender}"
    to "#{receiver}"
    subject 'Gitlab Mirror Pull'
    text_part do
      body "#{email_body_text}"
    end

    html_part do
      content_type 'text/html; charset=UTF-8'
      body "#{email_body_html}"
    end
  end
  mail.delivery_method :sendmail
  mail.deliver!
end

#trigger_pipeline(fetch, namespace) ⇒ Object

Trigger Pipeline if changes fetched and repo set in @confif[‘trigger’]

Parameters:

  • fetch (String)

    contains returned value of ‘git fetch’

  • namespace (String)

    of the project e.g. group-name/your-project



76
77
78
79
80
81
82
83
84
# File 'lib/gitlab_mirror_pull.rb', line 76

def trigger_pipeline(fetch, namespace)
  to_trigger = self.pipeline_to_trigger(namespace)

  if !fetch.to_s.empty? && to_trigger != false
    repo_encoded = url_encode(namespace)
    gitlab_api_project = Gitlab.client(endpoint: "#{@config['api']['url']}/api/v4", private_token: @config['api']['token'])
    gitlab_api_project.create_pipeline("#{repo_encoded}", "#{to_trigger}")
  end
end