Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.89 KB | None | 0 0
  1. post '/' do
  2.     json = JSON.parse(request.body.read.to_s)
  3.     notification_type = json['object_kind']
  4.    
  5.     case notification_type
  6.     when 'push'
  7.       push_repo = Cinch::Formatting.format(:green, json['repository']['name'])
  8.       push_refs = Cinch::Formatting.format(:brown, json['ref'])
  9.       push_refs.slice! "refs/heads/"
  10.       push_author = Cinch::Formatting.format(:red, json['user_name'])
  11.       if json['total_commits_count'] > 5
  12.         before_hash = json['before'][0..7].gsub(/\n/, "")
  13.         after_hash = json['after'][0..7].gsub(/\n/, "")
  14.         repo_page = json['repository']['homepage']
  15.         push_url = shorten("#{repo_page}/compare/#{before_hash}...#{after_hash}")
  16.         say "(#{push_repo} * #{after_hash} * #{push_refs}): #{push_author} pushed #{json['total_commits_count']} commits: #{push_url}"
  17.         break
  18.       end
  19.       json['commits'].each do |commit|
  20.         commit_message = commit['message'][0..80].gsub(/\n/, " ")
  21.        
  22.         if commit_message.length > 80
  23.             puts commit_message.concat("...")
  24.         end
  25.        
  26.         commit_url = shorten( commit['url'] )
  27.        
  28.         commit_author = Cinch::Formatting.format(:red, commit['author']['name'])
  29.         commit_hash = commit['id'][0..7].gsub(/\n/, "")
  30.         if (commit_author != push_author)
  31.             commit_author = push_author + " (#{commit_author})"
  32.         end
  33.         say "(#{push_repo} * #{commit_hash} * #{push_refs}): #{commit_author} committed: #{commit_message} #{commit_url}"
  34.       end
  35.     when 'tag_push'
  36.       say "[#{json['repository']['name']}] #{json['user_name']} | New Tag: #{json['ref']}"
  37.     when 'issue'
  38.         issue_url = shorten(json['object_attributes']['url'])
  39.         say "#{json['user']['name']} #{json['object_attributes']['state']} issue ##{json['object_attributes']['iid']}: #{json['object_attributes']['title']} #{issue_url}" if json['object_attributes']['action'] != 'update'
  40.     when 'note'
  41.         note_url = shorten(json['object_attributes']['url'])
  42.         note_name = Cinch::Formatting.format(:red, json['user']['name'])
  43.         note_com_name = Cinch::Formatting.format(:purple, json['commit']['author']['name'])
  44.        
  45.         say "#{note_name} commented on #{note_com_name}'s commit #{note_url}"
  46.     when 'build'
  47.         if (json['build_status'] == 'failed')
  48.             repo = Cinch::Formatting.format(:green, json['repository']['name'])
  49.             refs = Cinch::Formatting.format(:brown, json['ref'])
  50.             refs.slice! "refs/heads/"
  51.             homepage = json['repository']['homepage']
  52.             builds = json['build_name']
  53.             build_url = shorten(homepage + '/builds/' + json['build_id'].to_s)
  54.             commit_url = shorten(homepage + '/commit/' + json['commit']['sha'])
  55.             say "(#{repo} * #{refs}): #{builds} for commit #{commit_url} FAILED | Build details: #{build_url}"
  56.         end
  57.     end
  58.     status 200
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement