asana_service.rb 3.6 KB
Newer Older
J
Jeremy 已提交
1 2 3 4
# == Schema Information
#
# Table name: services
#
D
Dmitriy Zaporozhets 已提交
5 6 7 8 9 10 11 12 13
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
#  project_id            :integer
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
#  template              :boolean          default(FALSE)
14 15 16 17
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
18
#  note_events           :boolean          default(TRUE), not null
J
Jeremy 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#
require 'asana'

class AsanaService < Service
  prop_accessor :api_key, :restrict_to_branch
  validates :api_key, presence: true, if: :activated?

  def title
    'Asana'
  end

  def description
    'Asana - Teamwork without email'
  end

  def help
J
Jeremy 已提交
35 36 37 38 39
    'This service adds commit messages as comments to Asana tasks.
Once enabled, commit messages are checked for Asana task URLs
(for example, `https://app.asana.com/0/123456/987654`) or task IDs
starting with # (for example, `#987654`). Every task ID found will
get the commit comment added to it.
J
Jeremy 已提交
40 41 42

You can also close a task with a message containing: `fix #123456`.

J
Jeremy 已提交
43 44
You can find your Api Keys here:
http://developer.asana.com/documentation/#api_keys'
J
Jeremy 已提交
45 46 47 48 49 50 51 52
  end

  def to_param
    'asana'
  end

  def fields
    [
J
Jeremy 已提交
53 54 55
      {
        type: 'text',
        name: 'api_key',
M
Mike Wyatt 已提交
56
        placeholder: 'User Personal Access Token. User must have access to task, all comments will be attributed to this user.'
J
Jeremy 已提交
57 58 59 60
      },
      {
        type: 'text',
        name: 'restrict_to_branch',
M
Mike Wyatt 已提交
61
        placeholder: 'Comma-separated list of branches which will be automatically inspected. Leave blank to include all branches.'
J
Jeremy 已提交
62
      }
J
Jeremy 已提交
63 64 65
    ]
  end

66 67 68 69
  def supported_events
    %w(push)
  end

D
Douwe Maan 已提交
70
  def execute(data)
71
    return unless supported_events.include?(data[:object_kind])
72

J
Jeremy 已提交
73 74 75 76
    Asana.configure do |client|
      client.api_key = api_key
    end

D
Douwe Maan 已提交
77
    user = data[:user_name]
78
    branch = Gitlab::Git.ref_name(data[:ref])
J
Jeremy 已提交
79 80 81 82

    branch_restriction = restrict_to_branch.to_s

    # check the branch restriction is poplulated and branch is not included
83
    if branch_restriction.length > 0 && branch_restriction.index(branch).nil?
J
Jeremy 已提交
84 85 86 87 88 89
      return
    end

    project_name = project.name_with_namespace
    push_msg = user + ' pushed to branch ' + branch + ' of ' + project_name

D
Douwe Maan 已提交
90
    data[:commits].each do |commit|
J
Jeremy 已提交
91 92 93 94 95 96 97 98
      check_commit(' ( ' + commit[:url] + ' ): ' + commit[:message], push_msg)
    end
  end

  def check_commit(message, push_msg)
    task_list = []
    close_list = []

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    # matches either:
    # - #1234
    # - https://app.asana.com/0/0/1234
    # optionally preceded with:
    # - fix/ed/es/ing
    # - close/s/d
    # - closing
    issue_finder = /(fix\w*|clos[ei]\w*+)?\W*(?:https:\/\/app\.asana\.com\/\d+\/\d+\/(\d+)|#(\d+))/i

    message.scan(issue_finder).each do |tuple|
      # tuple will be
      # [ 'fix', 'id_from_url', 'id_from_pound' ]
      taskid = tuple[2] || tuple[1]
      task_list.push(taskid)

      if tuple[0]
        close_list.push(taskid)
      end
J
Jeremy 已提交
117 118 119 120
    end

    # post commit to every taskid found
    task_list.each do |taskid|
121
      task = Asana::Task.find(taskid)
J
Jeremy 已提交
122 123 124 125 126 127 128 129

      if task
        task.create_story(text: push_msg + ' ' + message)
      end
    end

    # close all tasks that had 'fix(ed/es/ing) #:id' in them
    close_list.each do |taskid|
130
      task = Asana::Task.find(taskid)
J
Jeremy 已提交
131 132 133 134 135 136 137

      if task
        task.modify(completed: true)
      end
    end
  end
end