gitlab-mirror-pull 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e343e194f74fb9e356de661a414b703f42071fdd1ff381b8b6bf8b08ab3721f
4
- data.tar.gz: cbcf48554695a76c1dde6079332275a20db59a2ed9c188999b6df74c348a48c4
3
+ metadata.gz: 8557f20c19f73f034576cde8d4dc3ed225ea955300d531039c6b98c60f54c3d6
4
+ data.tar.gz: 2912222346f47b693d93343c819e080abef2f393d0c4d225bee7c5ee2aa16126
5
5
  SHA512:
6
- metadata.gz: 0bdfa1755e8395ff06e64b8e7986c3764a0973cb047cb720389fc670570e0067cc697ffc5f54e4636b60646ec02fcdab96d29f94fa7a05d95ae6b124bffcf206
7
- data.tar.gz: 4690df4b67c1375dbf5ac055c0983a338d6205a97fb27d9508dced64f63b9d29d6ef16fbc4e25b9cb486b0316c35a93ffb94a2cf899e95b1139d613f1a04f0ed
6
+ metadata.gz: f0a6af061d5c4fe1404fa04b759655021df928b21cc2deb326544791a2823a0986aad86ac7a8f9fbebf53034825222f8fa2828d26d48d708ae3b27cedc6367f8
7
+ data.tar.gz: 6a817d57bb0d439611db9c44e76be08f31c2709064532464d2175fa27d106d0e5ff37879d655179464620e4c3776443a47d9bd37cfa715e1b7a6f44290a872e7
data/README.md CHANGED
@@ -9,6 +9,7 @@ Update your git repositories automatically when `remote` is set
9
9
  * Adds a command to fetch repositories
10
10
  * Run a tiny Webserver to integrate with Gitlabs webhooks
11
11
  * Adds init script to `/etc/init.d/gitlab-mirror-server`
12
+ * Send mail on error or just for reporting
12
13
 
13
14
  # Install (for development)
14
15
 
@@ -32,6 +33,28 @@ gem install gitlab-mirror-pull
32
33
 
33
34
  :warning: Make sure you run the script/server as `git` user. This is default for omnibus installation but may differ. In case you run the binary directly use this command `sudo -u git -H gitlab-mirror-pull`
34
35
 
36
+ ### Configuration (config.yml)
37
+
38
+ ```yaml
39
+ git:
40
+ path: "/usr/bin/git"
41
+ repos: "/var/opt/gitlab/git-data/repositories" # Default path of gitlab omnibus installation
42
+ ignore:
43
+ - "group-name/project-name" # Ignore single repo
44
+ - "group-name" # Ignore entire group
45
+ provider: # provider equals to "remote" set in your repository
46
+ - "github"
47
+ - "gitlab"
48
+ server:
49
+ port: 8088
50
+ bind: "localhost"
51
+ mail:
52
+ sender: "example@gmail.com"
53
+ receiver: "example@gmail.com"
54
+ send_on_error: false
55
+ send_report: false
56
+ ```
57
+
35
58
  ### Run
36
59
 
37
60
  ```bash
@@ -7,6 +7,23 @@ ignore:
7
7
  provider: # provider equals to "remote" set in your repository
8
8
  - "github"
9
9
  - "gitlab"
10
+ api:
11
+ url: "https://gitlab.example.org/"
12
+ token: "XXXXXXXXXXX"
13
+ pipeline:
14
+ enabled: false
15
+ trigger:
16
+ -
17
+ repo: "user_group/repo_1" # Run pipeline for single project
18
+ branch: "master" # Branch to trigger pipeline
19
+ -
20
+ repo: "user_group/repo_1" # Run pipeline for single project
21
+ branch: "user_group" # Branch to trigger pipeline
10
22
  server:
11
23
  port: 8088
12
24
  bind: "localhost"
25
+ mail:
26
+ sender: "example@gmail.com"
27
+ receiver: "example@gmail.com"
28
+ send_on_error: false
29
+ send_report: false
@@ -2,6 +2,7 @@ require 'optparse'
2
2
  require 'git'
3
3
  require 'logger'
4
4
  require 'yaml'
5
+ require 'mail'
5
6
 
6
7
  # Fetch Gitlab repositories
7
8
  class GitlabMirrorPull
@@ -19,6 +20,33 @@ class GitlabMirrorPull
19
20
  @log = Logger.new(STDOUT)
20
21
  @log.level = log_level
21
22
  @config = YAML.load_file(config)
23
+
24
+ end
25
+
26
+ def clean_html(email_body = '')
27
+ email_body.gsub(/<\/?[^>]*>/, ' ').gsub(/\n\n+/, '\n').gsub(/^\n|\n$/, ' ')
28
+ end
29
+
30
+ def send_mail(text)
31
+ email_body_text = self.clean_html(text)
32
+ email_body_html = text
33
+ sender = "#{@config['mail']['sender']}"
34
+ receiver = "#{@config['mail']['receiver']}"
35
+ mail = Mail.new do
36
+ from "#{sender}"
37
+ to "#{receiver}"
38
+ subject 'Gitlab Mirror Pull'
39
+ text_part do
40
+ body "#{email_body_text}"
41
+ end
42
+
43
+ html_part do
44
+ content_type 'text/html; charset=UTF-8'
45
+ body "#{email_body_html}"
46
+ end
47
+ end
48
+ mail.delivery_method :sendmail
49
+ mail.deliver!
22
50
  end
23
51
 
24
52
  # Prepare list of repositories
@@ -41,6 +69,33 @@ class GitlabMirrorPull
41
69
 
42
70
  end
43
71
 
72
+ # Trigger Pipeline if changes fetched and repo set in @confif['pipeline']['trigger']
73
+ #
74
+ # @param <String> fetch contains returned value of 'git fetch'
75
+ # @param <String> namespace of the project e.g. group-name/your-project
76
+ def trigger_pipeline(fetch, namespace)
77
+ to_trigger = self.pipeline_to_trigger(namespace)
78
+
79
+ if !fetch.to_s.empty? && to_trigger != false
80
+ repo_encoded = url_encode(namespace)
81
+ gitlab_api_project = Gitlab.client(endpoint: "#{@config['api']['url']}/api/v4", private_token: @config['api']['token'])
82
+ gitlab_api_project.create_pipeline("#{repo_encoded}", "#{to_trigger}")
83
+ end
84
+ end
85
+
86
+ # Check config if pipeline should trigger
87
+ #
88
+ # @param <String> repo_namespace of the project e.g. group-name/your-project
89
+ # @return <Boolean> true/false
90
+ def pipeline_to_trigger(repo_namespace)
91
+ @config['pipeline']['trigger'].each do |trigger|
92
+ if repo_namespace.include?("#{trigger["repo"]}")
93
+ return trigger['branch']
94
+ end
95
+ end
96
+ return false
97
+ end
98
+
44
99
  # Fetch repositories return by `repositories_to_fetch`
45
100
  #
46
101
  # @param [Array<String>] repos with absolute path to repositories you want to fetch
@@ -51,7 +106,9 @@ class GitlabMirrorPull
51
106
  Git.configure do |config|
52
107
  config.binary_path = "#{@config['git']['path']}"
53
108
  end
109
+
54
110
  @return_repos = []
111
+ @error_repos = ""
55
112
  # Loop through repos and fetch it
56
113
  repos_to_fetch = repos.nil? ? self.repositories_to_fetch : repos
57
114
  repos_to_fetch.each do |repo|
@@ -62,13 +119,33 @@ class GitlabMirrorPull
62
119
  # Determine which "remote" to fetch e.g. "git fetch github"
63
120
  if @config['provider'].include?("#{remote}")
64
121
  @log.info("Fetching remote #{remote} in #{repo}")
65
- g.remote(remote).fetch
66
- @return_repos << repo
122
+ begin
123
+ g.remote(remote).fetch
124
+ @return_repos << repo
125
+ rescue => e
126
+ @error_repos << "<b>Failed to fetch remote #{remote} in #{repo}</b>\n"
127
+ @error_repos << "<pre>#{e.message}</pre>"
128
+ end
67
129
  end
68
130
  end
69
131
  end
70
132
  end
133
+
134
+ # Prepare text for error mail
135
+ if !@error_repos.empty? && @config['mail']['send_on_error'] == true
136
+ mail = @error_repos.to_s
137
+ text = "<h1>Failed to fetch some repositories:</h1>\n#{mail}"
138
+ self.send_mail(text)
139
+ end
140
+
141
+ # Prepare text for report mail
142
+ if @config['mail']['send_report'] == true
143
+ mail = @return_repos.join("<br>")
144
+ mail_error = @error_repos.empty? ? '<br><br>Yey, no update failed!..' : "<h1>Repos failed to fetch:</h1>\n #{@error_repos.to_s}"
145
+ text = "<h1>Repos updated:</h1>\n#{mail} #{mail_error}"
146
+ self.send_mail(text)
147
+ end
148
+
71
149
  @return_repos
72
150
  end
73
-
74
151
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-mirror-pull
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ochorocho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-25 00:00:00.000000000 Z
11
+ date: 2018-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -50,8 +50,8 @@ dependencies:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: 2.0.3
53
- description: Checks for repositories with a set remote and run git fetch on these
54
- ...
53
+ description: 'Checks for gitlab repositories with a set remote and run git fetch on
54
+ these. Features: send mail for reports/error, webhook integration, trigger pipeline'
55
55
  email: rothjochen@gmail.com
56
56
  executables:
57
57
  - gitlab-mirror-pull