Class: GitLabParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitall/parsers/gitlab.rb

Overview

GitLab Event Parsing

Class Method Summary collapse

Class Method Details

.parse(json) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gitall/parsers/gitlab.rb', line 9

def GitLabParser.parse(json)
  j        = RecursiveOpenStruct.new(json)
  response = []
  kind     = j.object_kind
  case kind
  when 'note'
    repo  = j.project.path_with_namespace
    ntype = j.object_attributes.noteable_type
    case ntype
    when 'MergeRequest'
      mr_note  = j.object_attributes.note
      mr_url   = j.object_attributes.url
      mr_title = j.merge_request.title
      mr_id    = j.merge_request.iid
      mr_user  = j.user.name
      response << "[#{repo}] #{mr_user} commented on Merge Request ##{mr_id} \u2014 #{mr_note}"
      response << "'#{mr_title}' => #{mr_url}"
    when 'Commit'
      c_note = j.object_attributes.note
      c_sha  = j.commit.id[0...7]
      c_url  = j.object_attributes.url
      c_user = j.user.name
      response << "[#{repo}] #{c_user} commented on commit (#{c_sha}) \u2014 #{c_note} <#{c_url}>"
    when 'Issue'
      i_id    = j.issue.iid
      i_url   = j.object_attributes.url
      i_msg   = j.object_attributes.note
      i_title = j.issue.title
      i_user  = j.user.name
      response << "[#{repo}] #{i_user} commented on Issue ##{i_id} (#{i_title}) \u2014 #{i_msg} <#{i_url}>"
    else
      # type code here
    end
  when 'issue'
    i_repo   = j.project.path_with_namespace
    i_user   = j.user.name
    i_un     = j.user.username
    i_id     = j.object_attributes.iid
    i_title  = j.object_attributes.title
    i_action = j.object_attributes.action
    i_url    = j.object_attributes.url
    response << "[#{i_repo}] #{i_user}(#{i_un}) #{i_action} issue ##{i_id} - #{i_title} <#{i_url}>"
  when 'merge_request'
    mr_name      = j.user.name
    mr_user      = j.user.username
    mr_url       = j.url
    mr_spath     = j.object_attributes.source.path_with_namespace
    mr_sbranch   = j.object_attributes.source_branch
    mr_tpath     = j.object_attributes.target.path_with_namespace
    mr_tbranch   = j.object_attributes.target_branch
    mr_lcmessage = j.object_attributes.last_commit.message
    mr_lcsha     = j.object_attributes.last_commit.id[0...7]
    response << "#{mr_name}(#{mr_user}) opened a merge request. #{mr_spath}[#{mr_sbranch}] ~> #{mr_tpath}[#{mr_tbranch}]"
    response << "[#{mr_lcsha}] \u2014 #{mr_lcmessage} <#{mr_url}>"
  when 'push' # comes to
    # shove
    branch   = j.ref.split('/')[-1]
    commits  = j.commits
    added    = 0
    removed  = 0
    modified = 0
    commits.each do |h|
      added    += h['added'].length
      removed  += h['removed'].length
      modified += h['modified'].length
    end
    owner        = j.project.namespace
    project      = j.project.name
    pusher       = j.user_name
    commit_count = j.total_commits_count
    repo_url     = j.project.web_url
    response << "[#{owner}/#{project}] #{pusher} pushed #{commit_count} commit(s) [+#{added}/-#{removed}#{modified}] to [#{branch}] at <#{repo_url}>"
    if commits.length > 3
      coms = commits[0..2]
      coms.each do |n|
        id     = n['id']
        msg    = n['message'].lines[0]
        author = n['author']['name']
        response << "#{author}#{msg.chomp} [#{id[0...7]}]"
      end
      response << "and #{commits.from(3).length} commits..."
    else
      commits.each do |n|
        id     = n['id'][0...7]
        msg    = n['message'].lines[0]
        author = n['author']['name']
        response << "#{author}#{msg.chomp} [#{id}]"
      end
    end
  when 'tag_push'
    owner        = j.project.namespace
    project      = j.project.name
    pusher       = j.user_name
    commit_count = j.total_commits_count
    repo_url     = j.project.web_url
    tag = j.ref.split('/')[-1]
    response << "[#{owner}/#{project}] #{pusher} added/modified/removed tag #{tag}"
    response << "[#{owner}/#{project}] Release: <https://gitlab.com/#{owner}/#{project}/releases/tag/#{tag}>"

  else
    # type code here
  end
  return response
end