Skip to content

Instantly share code, notes, and snippets.

@wake
Last active December 21, 2019 07:47
Show Gist options
  • Save wake/9827563 to your computer and use it in GitHub Desktop.
Save wake/9827563 to your computer and use it in GitHub Desktop.
Custom Gitlab webhook

How to add issue comment (note) webhook

Edit file app/services/notification_service.rb

add below code to the end of function new_note

    # ignore gitlab service messages starts with _Status changed to closed
    return true if note.note =~ /\A_Status changed to closed/

    if note.noteable_type.underscore == 'issue'
      issue = note.noteable
      project = issue.project
      issue.project.execute_hooks({
        object_kind: 'issue_note',
        object_attributes: {
          issue: issue.attributes,
          repo: project.repository.path_with_namespace,
          author: note.author.name,
          note: note.note
        }
      }, :issue_hooks)
    end

How to add more info to issue webhook

Edit file app/models/concerns/issuable.rb

Replace function to_hook_data as below

  def to_hook_data
    {
      object_kind: self.class.name.underscore,
      object_attributes:
      {
        issue: self.attributes,
        repo: self.project.repository.path_with_namespace,
        author: self.author_name
      }
    }
  end

Available attributes

# Issue

{
  id:
  title:
  assignee_id:
  author_id:
  project_id:
  created_at:
  updated_at:
  position:
  branch_name:
  description:
  milestone_id:
  state:
  iid:
}

# Author

{
  id:
  email:
  encrypted_password: 
  reset_password_token:
  reset_password_sent_at:
  remember_created_at:
  sign_in_count:
  current_sign_in_at:
  last_sign_in_at:
  current_sign_in_ip: 
  last_sign_in_ip: 
  created_at:
  updated_at:
  name:
  admin:
  projects_limit:
  skype:
  linkedin:
  twitter:
  authentication_token:
  theme_id:
  bio:
  failed_attempts:
  locked_at:
  extern_uid:
  provider:
  username:
  can_create_group:
  can_create_team:
  state: active
  color_scheme_id:
  notification_level:
  password_expires_at:
  created_by_id:
  avatar:
  confirmation_token:
  confirmed_at:
  confirmation_sent_at:
  unconfirmed_email:
  hide_no_ssh_key:
}

# Note

{
  id:
  note:
  noteable_type:
  author_id:
  created_at:
  updated_at:
  project_id:
  attachment:
  line_code:
  commit_id:
  noteable_id:
  st_diff:
  system:
}

Webhook when issue commented

Reference files

# Issue observer

Ref: app/observers/issue_observer.rb

def after_create(issue)
  notification.new_issue(issue, current_user)
  issue.create_cross_references!(issue.project, current_user)
  execute_hooks(issue)
end
def execute_hooks(issue)
  issue.project.execute_hooks(issue.to_hook_data, :issue_hooks)
end

# Note observer

Ref: app/observers/note_observer.rb

def after_create(note)
  notification.new_note(note)
   unless note.system?
    # Create a cross-reference note if this Note contains GFM that names an
    # issue, merge request, or commit.
      note.references.each do |mentioned|
      Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
    end
  end
end

# Get issue from a note

Ref: app/mailers/emails/note.rb

def note_issue_email(recipient_id, note_id)
  @note = Note.find(note_id)
  @issue = @note.noteable
  @project = @note.project
  mail(to: recipient(recipient_id), subject: subject("Note for issue ##{@issue.iid}"))
end

# Project.execute_hooks

Ref: app/models/project.rb

def execute_hooks(data, hooks_scope = :push_hooks)
  hooks.send(hooks_scope).each do |hook|
    hook.async_execute(data)
  end
end

Ref: app/models/web_hook.rb

def async_execute(data)
  Sidekiq::Client.enqueue(ProjectWebHookWorker, id, data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment