Class: Lita::Handlers::GitlabInteractive

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/gitlab_interactive.rb

Instance Method Summary collapse

Instance Method Details

#get_default_issue(response) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/lita/handlers/gitlab_interactive.rb', line 116

def get_default_issue(response)
  room = response.message.source.room
  project_id = redis.get("default_room:#{room}")
  issue = get_issue_of_project(project_id, response.matches[0][0])
  project = get_project_from_id(project_id)
  response.reply "##{issue['iid']}: #{issue['title']} #{project['web_url']}/issues/#{issue['iid']}"
end

#get_default_mr(response) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/lita/handlers/gitlab_interactive.rb', line 124

def get_default_mr(response)
  room = response.message.source.room
  project_id = redis.get("default_room:#{room}")
  mr = get_mr_of_project(project_id, response.matches[0][0])
  project = get_project_from_id(project_id)
  response.reply "##{mr['iid']}: #{mr['title']}: #{mr['state']} #{project['web_url']}/merge_requests/#{mr['iid']}"
end

#get_default_project(response) ⇒ Object



109
110
111
112
113
114
# File 'lib/lita/handlers/gitlab_interactive.rb', line 109

def get_default_project(response)
  room = response.message.source.room
  project_id = redis.get("default_room:#{room}")
  project = get_project_from_id(project_id)
  response.reply project['name_with_namespace']
end

#get_default_sha(response) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/lita/handlers/gitlab_interactive.rb', line 132

def get_default_sha(response)
  room = response.message.source.room
  project_id = redis.get("default_room:#{room}")
  sha = get_sha_of_project(project_id, response.matches[0][0])
  project = get_project_from_id(project_id)
  response.reply "##{sha['id']}: #{sha['author_name']}\n#{sha['message']}#{project['web_url']}/commit/#{sha['id']}"
end

#get_issue_of_project(project_id, issue_id) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/lita/handlers/gitlab_interactive.rb', line 74

def get_issue_of_project(project_id, issue_id)
  connection = Faraday.new config.host, :ssl => {:verify => false}
  project_response = connection.get do |req|
    req.url '/api/v3/projects/' + project_id.to_s + '/issues?iid=' + issue_id
    req.headers['PRIVATE-TOKEN'] = config.token
  end
  hash = JSON.parse(project_response.body)
  hash[0]
end

#get_mr_of_project(project_id, mr_id) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/lita/handlers/gitlab_interactive.rb', line 55

def get_mr_of_project(project_id, mr_id)
  connection = Faraday.new config.host, :ssl => {:verify => false}
  project_response = connection.get do |req|
    req.url '/api/v3/projects/' + project_id.to_s + '/merge_requests?iid=' + mr_id
    req.headers['PRIVATE-TOKEN'] = config.token
  end
  hash = JSON.parse(project_response.body)
  hash[0]
end

#get_project_from_id(project_id) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/lita/handlers/gitlab_interactive.rb', line 84

def get_project_from_id(project_id)
  connection = Faraday.new config.host, :ssl => {:verify => false}
  project_response = connection.get do |req|
    req.url '/api/v3/projects/' + project_id.to_s
    req.headers['PRIVATE-TOKEN'] = config.token
  end
  hash = JSON.parse(project_response.body)
  hash
end

#get_project_id(project) ⇒ Object



98
99
100
# File 'lib/lita/handlers/gitlab_interactive.rb', line 98

def get_project_id(project)
  redis.get(project)
end

#get_project_id_from_path(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lita/handlers/gitlab_interactive.rb', line 41

def get_project_id_from_path(path)
  id = get_project_id(path)
  return id if id
  connection = Faraday.new config.host, :ssl => {:verify => false}
  project_response = connection.get do |req|
    req.url '/api/v3/projects/' + path.gsub('/', '%2F')
    req.headers['PRIVATE-TOKEN'] = config.token
  end
  hash = JSON.parse(project_response.body)
  id = hash["id"]
  set_project_id(path, id)
  id
end

#get_project_issue(response) ⇒ Object



34
35
36
37
38
39
# File 'lib/lita/handlers/gitlab_interactive.rb', line 34

def get_project_issue(response)
  project_id = get_project_id_from_path(response.matches[0][0])
  project = get_project_from_id(project_id)
  issue = get_issue_of_project(project_id, response.matches[0][1])
  response.reply "##{issue['iid']}: #{issue['title']} #{project['web_url']}/issues/#{issue['iid']}"
end

#get_sha_of_project(project_id, sha) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/lita/handlers/gitlab_interactive.rb', line 65

def get_sha_of_project(project_id, sha)
  connection = Faraday.new config.host, :ssl => {:verify => false}
  project_response = connection.get do |req|
    req.url '/api/v3/projects/' + project_id.to_s + '/repository/commits/' +sha
    req.headers['PRIVATE-TOKEN'] = config.token
  end
  hash = JSON.parse(project_response.body)
end

#set_default_project(response) ⇒ Object



102
103
104
105
106
107
# File 'lib/lita/handlers/gitlab_interactive.rb', line 102

def set_default_project(response)
  project_id = get_project_id_from_path(response.matches[0][0])
  room = response.message.source.room
  redis.set("default_room:#{room}", project_id)
  response.reply("OK I've made #{response.matches[0][0]} the default project")
end

#set_project_id(project, id) ⇒ Object



94
95
96
# File 'lib/lita/handlers/gitlab_interactive.rb', line 94

def set_project_id(project, id)
  redis.set(project, id)
end