Class: GitLab::Issue

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Issue

Returns a new instance of Issue.



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

def initialize(params = {})
  @title = params[:title]
  @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.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def assignee_id
  @assignee_id
end

#branchObject

Returns the value of attribute branch.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def branch
  @branch
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def description
  @description
end

#iidObject

Returns the value of attribute iid.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def iid
  @iid
end

#labelsObject

Returns the value of attribute labels.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def labels
  @labels
end

#obj_gitlabObject

Returns the value of attribute obj_gitlab.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def obj_gitlab
  @obj_gitlab
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def status
  @status
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/GitLab/issue.rb', line 3

def title
  @title
end

Class Method Details

.allObject



94
95
96
97
# File 'lib/GitLab/issue.rb', line 94

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

.find_by(search) ⇒ Object



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

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]
  if issue_json
    issue = GitLab::Issue.new
    issue.set_data issue_json
  else
    raise "Issue not found #{search.keys[0]}"
  end
end

.find_by_branch(branch) ⇒ Object



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

def self.find_by_branch(branch)
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?search=#{branch}"
  issue_json = GitLab.request_get(url)[0]
  if issue_json
    issue = GitLab::Issue.new
    issue.set_data issue_json
  else
    raise "Issue not found #{branch}. \nCheck if exist the label default_branch in the body description"
  end
end

.from_list(list_name) ⇒ Object



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

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 = self.new
    issue.set_data(obj)

    issues << issue
  end
  issues
end

.pingObject



99
100
101
102
# File 'lib/GitLab/issue.rb', line 99

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

Instance Method Details

#add_comment(note) ⇒ Object



132
133
134
135
136
# File 'lib/GitLab/issue.rb', line 132

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

#closeObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/GitLab/issue.rb', line 45

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

#commentsObject



7
8
9
# File 'lib/GitLab/issue.rb', line 7

def comments
  @comments
end

#comments=(obj) ⇒ Object



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

def comments=obj
  @comments << obj
end

#createObject



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

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"]
  print "Issue created with success!\n".green
  print "URL: #{issue_json["web_url"]}\n\n"
end


149
150
151
152
# File 'lib/GitLab/issue.rb', line 149

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



127
128
129
130
# File 'lib/GitLab/issue.rb', line 127

def list_tasks
  # a.description.match(/(\* \~changelog .*\n)+/).to_a
  description.match(/\* \~tasks .*\n?/).to_s.gsub('* ~tasks ', '') rescue nil
end

#merge_requestsObject



117
118
119
120
# File 'lib/GitLab/issue.rb', line 117

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

#msg_changelogObject



122
123
124
125
# File 'lib/GitLab/issue.rb', line 122

def msg_changelog
  # a.description.match(/(\* \~changelog .*\n)+/).to_a
  description.match(/\* \~changelog .*\n?/).to_s.gsub('* ~changelog ', '') rescue nil
end

#set_data(obj) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/GitLab/issue.rb', line 138

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

#set_default_branch(branch) ⇒ Object



24
25
26
# File 'lib/GitLab/issue.rb', line 24

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

#updateObject



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

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)
  print "Issue updated with success!\n".green
end