Class: GitLab::Issue

Inherits:
Object
  • Object
show all
Includes:
TtyIntegration
Defined in:
lib/GitLab/issue.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TtyIntegration

#bar, #cmd, #error, #green, #pastel, #prompt, #red, #success, #table, #yellow

Constructor Details

#initialize(params = {}) ⇒ Issue

Returns a new instance of Issue.



16
17
18
19
20
21
22
23
24
# File 'lib/GitLab/issue.rb', line 16

def initialize(params = {})
  @title = params[:title]
  @parent_branch_name = params[:parent_branch_name]
  @labels = params[:labels] || []
  @description = params[:description]
  @branch = params[:branch]
  @comments = []
  @assignee_id = GitLab::User.me['id']
end

Instance Attribute Details

#assignee_idObject

Returns the value of attribute assignee_id.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def assignee_id
  @assignee_id
end

#branchObject

Returns the value of attribute branch.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def branch
  @branch
end

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def description
  @description
end

#iidObject

Returns the value of attribute iid.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def iid
  @iid
end

#labelsObject

Returns the value of attribute labels.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def labels
  @labels
end

#obj_gitlabObject

Returns the value of attribute obj_gitlab.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def obj_gitlab
  @obj_gitlab
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def status
  @status
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def title
  @title
end

#web_urlObject

Returns the value of attribute web_url.



5
6
7
# File 'lib/GitLab/issue.rb', line 5

def web_url
  @web_url
end

Class Method Details

.allObject



115
116
117
118
# File 'lib/GitLab/issue.rb', line 115

def self.all
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?state=opened"
  GitLab.request_get(url)
end

.find_by(search) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/GitLab/issue.rb', line 75

def self.find_by(search)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?search=#{search.values[0]}&in=#{search.keys[0]}&state=opened"
  issue_json = GitLab.request_get(url)[0]
  raise "Issue not found #{search.keys[0]}" unless issue_json

  issue = GitLab::Issue.new
  issue.set_data issue_json
end

.find_by_branch(branch) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/GitLab/issue.rb', line 93

def self.find_by_branch(branch)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?search='~default_branch #{branch}'"
  issue_json = GitLab.request_get(URI::Generic::DEFAULT_PARSER.escape(url))[0]
  unless issue_json
    raise "Issue não encontrada #{branch}. \nVerifique se existe a label 'default_branch' na descrição da issue"
  end

  issue = GitLab::Issue.new
  issue.set_data issue_json
end

.find_by_id(id) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/GitLab/issue.rb', line 84

def self.find_by_id(id)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?iids[]=#{id}"
  issue_json = GitLab.request_get(url)[0]
  raise "Issue not found #{search.keys[0]}" unless issue_json

  issue = GitLab::Issue.new
  issue.set_data issue_json
end

.find_by_parent_branch(branch) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/GitLab/issue.rb', line 104

def self.find_by_parent_branch(branch)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?search='~parent #{branch}'"
  issue_json = GitLab.request_get(url)[0]
  unless issue_json
    raise "Issue não encontrada #{branch}. \nVerifique se existe a label 'default_branch' na descrição da issue"
  end

  issue = GitLab::Issue.new
  issue.set_data issue_json
end

.from_list(list_name) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/GitLab/issue.rb', line 125

def self.from_list(list_name)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?labels=#{list_name}&state=opened"
  issues = []
  issues_gitlab = GitLab.request_get(url)
  issues_gitlab.each do |obj|
    issue = new
    issue.set_data(obj)

    issues << issue
  end
  issues
end

.pingObject



120
121
122
123
# File 'lib/GitLab/issue.rb', line 120

def self.ping
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?"
  issue_json = GitLab.request_get(url)&.first
end

Instance Method Details

#add_comment(note) ⇒ Object



159
160
161
162
163
# File 'lib/GitLab/issue.rb', line 159

def add_comment(note)
  comment = GitLab::Comment.new(issue_iid: @iid, body: note)
  @comments << comment
  comment.create
end

#closeObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/GitLab/issue.rb', line 48

def close
  params = {}
  params.merge!(title: @title)
  params.merge!(state_event: 'close')
  params.merge!(description: @description.to_s)
  params.merge!(labels: @labels.join(','))

  url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}"
  GitLab.request_put(url, params)
  # print "Issue '#{@title}' closed with success!\n".green
end

#createObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/GitLab/issue.rb', line 30

def create
  params = {}
  params.merge!(title: @title)
  params.merge!(description: @description.to_s)
  params.merge!(labels: @labels.join(','))
  params.merge!(assignee_id: @assignee_id)

  # label = params.fetch(:label) || ''
  # assignee_id = params.fetch(:assignee_id) || ''
  # print "\nCreate new GitLab issue \n\n".yellow
  url = "projects/#{$GITLAB_PROJECT_ID}/issues"
  issue_json = GitLab.request_post(url, params)
  @iid = issue_json['iid']
  @web_url = issue_json['web_url']
  self
  # success("Issue created with success!\nURL: #{issue_json["web_url"]}")
end


181
182
183
184
# File 'lib/GitLab/issue.rb', line 181

def create_link_issue(target_issue_iid)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}/links?target_project_id=#{$GITLAB_PROJECT_ID}&target_issue_iid=#{target_issue_iid}"
  GitLab.request_post(url, params)
end

#list_tasksObject



151
152
153
154
155
156
157
# File 'lib/GitLab/issue.rb', line 151

def list_tasks
  # a.description.match(/(\* \~changelog .*\n)+/).to_a

  description.match(/\* ~tasks .*\n?/).to_s.gsub('* ~tasks ', '')
rescue StandardError
  nil
end

#merge_requestsObject



138
139
140
141
# File 'lib/GitLab/issue.rb', line 138

def merge_requests
  url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{iid}/closed_by"
  GitLab.request_get(url)
end

#msg_changelogObject



143
144
145
146
147
148
149
# File 'lib/GitLab/issue.rb', line 143

def msg_changelog
  # a.description.match(/(\* \~changelog .*\n)+/).to_a

  description.match(/\* ~changelog .*\n?/).to_s.gsub('* ~changelog ', '')
rescue StandardError
  nil
end

#set_data(obj) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/GitLab/issue.rb', line 165

def set_data(obj)
  @iid          = obj['iid']
  @title        = obj['title']
  @labels       = obj['labels']
  @description  = obj['description']
  @assignee_id  = obj['assignees'][0]['id']
  @branch       = begin
    obj['description'].match(/\* ~default_branch .*\n?/).to_s.gsub('* ~default_branch ',
                                                                   '').chomp.strip
  rescue StandardError
    nil
  end
  @obj_gitlab = obj
  self
end

#set_default_branch(branch) ⇒ Object



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

def set_default_branch(branch)
  @description = "* ~default_branch #{branch}\n" + @description
end

#updateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/GitLab/issue.rb', line 60

def update
  params = {}
  params.merge!(title: @title)
  params.merge!(description: @description.to_s)
  params.merge!(labels: @labels.join(','))
  params.merge!(assignee_id: @assignee_id)

  # label = params.fetch(:label) || ''
  # assignee_id = params.fetch(:assignee_id) || ''
  # print "\nUpdate GitLab issue\n\n".yellow
  url = "projects/#{$GITLAB_PROJECT_ID}/issues/#{@iid}"
  GitLab.request_put(url, params)
  # prompt.say(pastel.cyan("\nIssue updated with success!"))
end