class GitLab::Issue

Attributes

assignee_id[RW]
branch[RW]
description[RW]
iid[RW]
labels[RW]
obj_gitlab[RW]
status[RW]
title[RW]

Public Class Methods

all() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(params = {}) click to toggle source
# 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
ping() click to toggle source
# File lib/GitLab/issue.rb, line 99
def self.ping
  url = "projects/#{$GITLAB_PROJECT_ID}/issues?"
  issue_json = GitLab.request_get(url)&.first
end

Public Instance Methods

add_comment(note) click to toggle source
# 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
close() click to toggle source
# 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
comments() click to toggle source
# File lib/GitLab/issue.rb, line 7
def comments
  @comments
end
comments=(obj) click to toggle source
# File lib/GitLab/issue.rb, line 11
def comments=obj
  @comments << obj
end
create() click to toggle source
# 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
list_tasks() click to toggle source
# 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_requests() click to toggle source
# 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_changelog() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/GitLab/issue.rb, line 24
def set_default_branch branch
  @description = "* ~default_branch #{branch}\n" + @description
end
update() click to toggle source
# 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