Class: Lita::Handlers::Gitlab2jenkinsGhp

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(config) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/lita/handlers/gitlab2jenkins_ghp.rb', line 5

def self.default_config(config)
  config.room                  = '#test'
  config.group                 = 'group_name'
  config.gitlab_url            = 'http://example.gitlab'
  config.private_token         = 'orwejnweuf'
  config.jenkins_url           = 'http://example.jenkins'
  config.jenkins_hook          = '/gitlab/build'
end

Instance Method Details

#do_ci_change_status(request, response) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/lita/handlers/gitlab2jenkins_ghp.rb', line 70

def do_ci_change_status(request, response)
  Lita.logger.info "GitLab CI Project ID: #{request.env['router.params'][:id_project]}"
  Lita.logger.info "GitLab CI Commit SHA: #{request.env['router.params'][:sha_commit]}"
  Lita.logger.info "GitLab CI Token: #{request.params['token']}"
  format_message_ci(request.env['router.params'][:id_project][0], request.env['router.params'][:sha_commit][0])

rescue Exception => e
  Lita.logger.error "Could not domr_change_status: #{e.inspect}"
end

#do_img_jenkins(request, response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lita/handlers/gitlab2jenkins_ghp.rb', line 80

def do_img_jenkins(request, response)
  project_name = git_lab_data_project_info(request.env['router.params'][:id_project][0])['name']

  gkeys = @redis.keys("jenkins:#{project_name}:*")
  gkeys.each do |key|
    json_off = redis.get(key)
    jdata = symbolize parse_payload(json_off)
    job = jdata[:name]
    code_climate(job, response)
  end

  if gkeys.empty?
    nocode_climate(response)
  end

rescue Exception => e
  Lita.logger.error "Could not do_img_jenkins: #{e.inspect}"
end

#do_mr_change_status(request, response, json_body, data, project_name_params) ⇒ Object

do_mr_change_status(request, response, json_body, data, project_name_params)



60
61
62
63
64
65
66
67
68
# File 'lib/lita/handlers/gitlab2jenkins_ghp.rb', line 60

def do_mr_change_status(request, response, json_body, data, project_name_params) # do_mr_change_status(request, response, json_body, data, project_name_params)
  id_project = request.params['id_project'].to_s
  Lita.logger.info "Jenkins Proyect: #{id_project}"
  Lita.logger.info("Payload: #{json_body}")
  ci_status_setter(data, json_body, id_project, project_name_params)

rescue Exception => e
  Lita.logger.error "Could not domr_change_status: #{e.inspect}"
end

#receive(request, response) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/handlers/gitlab2jenkins_ghp.rb', line 20

def receive(request, response)
  routing_to = request.params['to_route'] || request.env['router.params'][:to_route][0].to_s
  routing_to_request_method = request.params['request_method'] || request.request_method
  if routing_to_request_method =='POST'
    json_body = request.params['payload'] || extract_json_from_request(request)
    data = symbolize parse_payload(json_body)
    if data.key? :object_attributes
      project_name_target = request.params['project_name_target'] || git_lab_data_project_info(data[:object_attributes][:target_project_id])['name']
      project_source_id_commit = request.params['project_source_id_commit'] || git_lab_data_branch_info(data[:object_attributes][:source_project_id], data[:object_attributes][:source_branch])['commit']['id']
    else
      project_name_target = ''
      project_source_id_commit = ''
    end
    if routing_to == 'gitlab'       #receive hook from gitlab to report commits and merge request event
      do_mr(json_body, data, project_name_target, project_source_id_commit)
    elsif routing_to == 'jenkins'   #receive hook from jenkins to report job status build
      project_name_params = request.params['project_name_params']
      do_mr_change_status(request, response, json_body, data, project_name_params)
    end
  elsif routing_to_request_method =='GET'
    if routing_to == 'ci_status'
      if request.params['ref'] == 'master'
        # if request.env['router.params'][:sha_commit][0].nil? false
        #   do_ci_change_status(request, response)
        do_img_jenkins(request, response)
      else
        message = do_ci_change_status(request, response)
        if message.to_s != 'true'
          response['status'] = message
          Lita.logger.info "CI Final Status: #{message.to_s}"
        end
        # do_img_jenkins(request, response)
      end
    end
  end
    # robot.send_message(target, message)
rescue Exception => e
  Lita.logger.error "Could not receive: #{e.inspect}"
end