Class: QAT::Devel::GitLab::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/qat/devel/lib/gitlab/client.rb

Overview

Defines methods related to milestones.

Since:

  • 6.0.0

Defined Under Namespace

Classes: IssueNotFoundError, IssueReleaseError, MilestoneNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = nil, token = nil, project_id = nil) ⇒ Client

New QAT::Devel::GitLab::Client

Since:

  • 6.0.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qat/devel/lib/gitlab/client.rb', line 15

def initialize(endpoint = nil, token = nil, project_id = nil)
  @endpoint = endpoint || ENV['GITLAB_ENDPOINT']
  raise ArgumentError.new "No definition of mandatory environment variable 'GITLAB_ENDPOINT'" unless @endpoint

  @token = token || ENV['GITLAB_TOKEN']
  raise ArgumentError.new "No definition of mandatory environment variable 'GITLAB_TOKEN'" unless @token

  @project_id = project_id || ENV['CI_PROJECT_ID']
  raise ArgumentError.new "No definition of mandatory environment variable 'CI_PROJECT_ID'" unless @project_id

  @client     = Gitlab.client endpoint:      @endpoint,
                              private_token: @token
  @issues     = {}
  @milestones = {}
end

Instance Attribute Details

#clientObject (readonly)

Since:

  • 6.0.0



11
12
13
# File 'lib/qat/devel/lib/gitlab/client.rb', line 11

def client
  @client
end

#issuesObject (readonly)

Since:

  • 6.0.0



11
12
13
# File 'lib/qat/devel/lib/gitlab/client.rb', line 11

def issues
  @issues
end

Instance Method Details

#all_milestone_issues_closed?(milestone_id) ⇒ Boolean

Checks closed state on issues of a given milestone id.

Examples:

client.all_milestone_issues_closed?(531)

Parameters:

  • milestone_id (Integer)

    The ID of a milestone.

Returns:

  • (Boolean)

Since:

  • 6.0.0



71
72
73
74
75
76
77
# File 'lib/qat/devel/lib/gitlab/client.rb', line 71

def all_milestone_issues_closed?(milestone_id)
  issues = get_all_milestone_issues(milestone_id)

  issues.all? do |issue|
    issue.state == 'closed'
  end
end

#create_issue(project, title, options = {}) ⇒ Array

Create a new issue

Examples:

client.create_issue(123, "New Issue", {description: "Test"})

Parameters:

  • project (Integer)

    The ID of a project.

  • title (String)

    The title of new issue.

  • options (Hash) (defaults to: {})

    The hash of options.

Returns:

  • (Array)

    Values of new issue

Since:

  • 6.0.0



89
90
91
# File 'lib/qat/devel/lib/gitlab/client.rb', line 89

def create_issue(project, title, options = {})
  client.create_issue(project, title, options)
end

#create_issue_release(milestone) ⇒ Array

Create a new Issue Release

Examples:

client.create_issue(123, "New Issue", {description: "Test"})

Parameters:

  • milestone (String)

    The name of milestone

Returns:

  • (Array)

    Values of new issue release

Raises:

Since:

  • 6.0.0



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
# File 'lib/qat/devel/lib/gitlab/client.rb', line 101

def create_issue_release(milestone)
  milestone_id = get_milestone_id(milestone)
  raise(IssueReleaseError, "Found issues with Milestone #{milestone} open!") unless all_milestone_issues_closed?(milestone_id)

  issues      = get_all_milestone_issues(milestone_id)
  description = nil
  issues.all? do |issue|
    i           = 0
    labels_text = nil
    issue.labels.all? do |label|
      if i == 0
        labels_text = "~#{label}"
        i           = i + 1
      else
        labels_text = "#{labels_text} ~#{label}"
      end
    end
    description =
        "" "#{description}
- #{labels_text} ##{issue.iid} #{issue.title}" ""
  end
  gemspec      = Dir.glob('../*.gemspec').first
  gemspec_spec = Gem::Specification::load(gemspec)
  gemspec_spec.name.to_s

  description =
      "" "## #{gemspec_spec.name.to_s} 2.0.0

**Milestone** \"%#{milestone}\"

### Issues solved in this release
#{description}
      " ""

  create_issue(@project_id, "Release #{milestone}", options = { description: description })
end

#create_release_tag(tag_name, release_note) ⇒ Object

Create a release tag

Parameters:

  • tag_name (String)

    tag name

  • release_note (String)

    release note content

Since:

  • 6.0.0



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/qat/devel/lib/gitlab/client.rb', line 147

def create_release_tag(tag_name, release_note)
  # GitLab gem post to create a tag is not correct.
  # Field containing release note content should be 'release_description' and not 'description'

  response = client.post("/projects/#{client.url_encode(@project_id)}/repository/tags",
                         body: { tag_name:            tag_name,
                                 ref:                 'master',
                                 message:             "Release #{tag_name}",
                                 release_description: release_note })
  puts "\nTAG Creation Response:\n#{response.to_h}\n"
end

#delete_issue(issue_id) ⇒ Object

Remove a issue

Parameters:

  • issue_id (Integer)

    The ID of a issue.

Since:

  • 6.0.0



140
141
142
# File 'lib/qat/devel/lib/gitlab/client.rb', line 140

def delete_issue(issue_id)
  client.delete_issue(@project_id, issue_id)
end

#get_all_milestone_issues(milestone_id) ⇒ Array

Gets the issues of a given milestone id.

Examples:

client.get_all_milestone_issues(531)

Parameters:

  • milestone_id (Integer)

    The ID of a milestone.

Returns:

  • (Array)

Raises:

Since:

  • 6.0.0



57
58
59
60
61
62
# File 'lib/qat/devel/lib/gitlab/client.rb', line 57

def get_all_milestone_issues(milestone_id)
  return @issues[milestone_id] if @issues[milestone_id]
  raise(IssueNotFoundError, "Found 0 issues of Milestone with title #{title}!") unless @issues
  @issues[milestone_id] = client.milestone_issues(@project_id, milestone_id)

end

#get_milestone_id(title) ⇒ Integer

Gets the id of a given milestone title.

Examples:

client.get_milestone_id('2.0.0')

Parameters:

  • title (String)

    The title of a milestone.

Returns:

  • (Integer)

    id

Raises:

Since:

  • 6.0.0



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qat/devel/lib/gitlab/client.rb', line 38

def get_milestone_id(title)
  # log.debug "Getting milestone id for: #{title}"

  @milestones = client.milestones(@project_id)

  milestone = @milestones.select { |milestone| milestone.title == title }.first

  raise(MilestoneNotFoundError, "Milestone with title #{title} not found in GitLab!") unless milestone

  milestone.id
end

#get_tags(options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Gets a list of project repository tags.

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options. Optional, default is nil.

Options Hash (options):

  • :page (Integer)

    The page number.

  • :per_page (Integer)

    The number of results per page.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

Since:

  • 6.0.0



165
166
167
# File 'lib/qat/devel/lib/gitlab/client.rb', line 165

def get_tags(options = {})
  client.tags(@project_id, options)
end