-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathgitlab.rb
More file actions
183 lines (159 loc) · 4.25 KB
/
Copy pathgitlab.rb
File metadata and controls
183 lines (159 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Yegor Bugayenko
# SPDX-License-Identifier: MIT
require 'gitlab'
require 'faraday'
require 'net/http'
require_relative '../git_repo'
require_relative '../clients/gitlab'
#
# Gitlab repo
# API: https://github.com/NARKOZ/gitlab
#
class GitlabRepo
ERRORS = [
Gitlab::Error::Error,
Faraday::Error,
Net::OpenTimeout,
Net::ReadTimeout,
Errno::ECONNREFUSED,
SocketError
].freeze
attr_reader :repo, :name
def initialize(client, json, config = {})
@name = 'github'
@client = client
@config = config
@json = json
@repo = git_repo(json, config)
end
def issue(issue_id)
hash = JSON.parse(
@client.issue(@repo.name, issue_id).to_hash.to_json,
symbolize_names: true
)
number, title = hash[:milestone].values_at(:id, :title) if hash[:milestone]
{
state: hash[:state],
author: hash[:author],
milestone: {
number: number,
title: title
}
}
rescue *ERRORS => e
raise "Can't read GitLab issue #{issue_id}: #{e.message}"
end
def close_issue(issue_id)
@client.close_issue(@repo.name, issue_id)
rescue *ERRORS => e
raise "Can't close GitLab issue #{issue_id}: #{e.message}"
end
def create_issue(data)
options = data.reject { |k| k == :title }
hash = JSON.parse(
@client.create_issue(@repo.name, data[:title], options).to_hash.to_json,
symbolize_names: true
)
{ number: hash[:iid], html_url: hash[:web_url] }
end
def update_issue(issue_id, data)
@client.edit_issue(@repo.name, issue_id, data)
end
def labels
result = []
@client.labels(@repo.name).each_page do |page|
page.each do |label|
result << JSON.parse(
label.to_hash.to_json,
symbolize_names: true
)
end
end
result
end
def add_label(label, color)
@client.add_label(@repo.name, label, color)
end
def add_labels_to_an_issue(issue_id, labels)
options = { labels: labels }
@client.edit_issue(@repo.name, issue_id, options)
end
def add_comment(issue_id, comment)
@client.create_issue_note(@repo.name, issue_id, comment)
rescue *ERRORS => e
raise "Can't comment on GitLab issue #{issue_id}: #{e.message}"
end
def create_commit_comment(sha, comment)
hash = JSON.parse(
@client.create_commit_comment(@repo.name, sha, comment).to_hash.to_json,
symbolize_names: true
)
hash[:html_url] = "https://gitlab.com/#{@repo.name}/commit/#{sha}"
hash
end
def list_commits
commits = []
@client.commits(@repo.name).each_page do |page|
page.each do |commit|
commits << { sha: commit.id, html_url: commit.web_url }
end
end
commits
end
def user(username)
hash = JSON.parse(
@client.user(username).to_hash.to_json,
symbolize_names: true
)
hash[:email] = hash[:public_email]
hash
end
def star
@client.star_project(@repo.name)
end
def exists?
hash = JSON.parse(
@client.project(@repo.name).to_hash.to_json,
symbolize_names: true
)
hash[:private] = hash[:visibility] == 'private'
true
rescue Gitlab::Error::NotFound => e
puts "Repository #{@repo.name} is not available: #{e.message}"
false
rescue Gitlab::Error::Forbidden => e
puts "Repository #{@repo.name} is not accessible: #{e.message}"
false
end
def repository_link
"https://gitlab.com/#{@repo.name}"
end
def collaborators_link
"https://gitlab.com/#{@repo.name}/project_members"
end
def file_link(file)
"https://gitlab.com/#{@repo.name}/blob/#{@repo.master}/#{file})"
end
def puzzle_link_for_commit(sha, file, start, stop)
"https://gitlab.com/#{@repo.name}/blob/#{sha}/#{file}#L#{start}-L#{stop}"
end
def issue_link(issue_id)
"https://gitlab.com/#{@repo.name}/issues/#{issue_id}"
end
private
def git_repo(json, config)
uri = json['project']['url']
name = json['project']['path_with_namespace']
target = json['ref']
default_branch = json['project']['default_branch']
head_commit_hash = json['checkout_sha']
GitRepo.new(
uri: uri,
name: name,
target: target,
id_rsa: config['id_rsa'],
master: default_branch,
head_commit_hash: head_commit_hash
)
end
end