Module: GitlabApi

Included in:
GitlabKernel
Defined in:
lib/git/error.rb,
lib/git/gitlab.rb

Defined Under Namespace

Modules: Error

Constant Summary collapse

API_VERSION =
"v3"
USER_AGENT =
"nyonyonyonyonyo! Gitlab nyo!!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/git/gitlab.rb', line 10

def client
  @client
end

#repositoryObject (readonly)

Returns the value of attribute repository.



10
11
12
# File 'lib/git/gitlab.rb', line 10

def repository
  @repository
end

Instance Method Details

#authorizeObject



35
36
37
# File 'lib/git/gitlab.rb', line 35

def authorize
  @client.user
end

#create_merge_request(title, assign, source, target = "master") ⇒ Object



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
# File 'lib/git/gitlab.rb', line 39

def create_merge_request(title, assign, source, target = "master")
  pid = @repository.config["gitlab.projectid"].to_i

  if pid == 0
    raise "Please set 'git config gitlab.projectid ${Gitlab Project id}'"
  end

  mr_title = if title == nil
    @repository.head.name
  else
    title
  end

  mr_source = if source == nil
    @repository.head.name
  else
    source
  end

  assignee_id = if assign == nil
    0
  else
    @client.users.select { |user|
      user.username == assign
    }[0].id
  end

  begin
    mergerequest = if assignee_id > 0
      @client.create_merge_request(pid, mr_title, :source_branch => mr_source, :target_branch => target, :assignee_id => assignee_id)
    else
      @client.create_merge_request(pid, mr_title, :source_branch => mr_source, :target_branch => target)
    end
  rescue Gitlab::Error::NotFound => e
    raise Error::MergeRequestError, "Failed Create Merge Request"
  end

  project_url = @client.project(pid).web_url
  mergerequest_url = project_url + "/merge_requests/" + mergerequest.id.to_s

  mergerequest_url
end

#initializeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git/gitlab.rb', line 12

def initialize
  @repository = Grit::Repo.new(`git rev-parse --show-toplevel`.chomp)
  config = @repository.config

  url = config["gitlab.url"]
  if url == nil
    raise "Plase set 'git config gitlab.url ${Gitlab URL}'"
  end

  token = config["gitlab.token"]
  if token == nil
    raise "Please set 'git config gitlab.token ${Gitlab Token}'"
  end

  Gitlab.configure do |config|
    config.endpoint       = "#{url}/api/#{API_VERSION}"
    config.private_token  = token
    config.user_agent     = USER_AGENT
  end

  @client = Gitlab.client
end