Class: Integrations::Asana

Inherits:
Integration show all
Defined in:
app/models/integrations/asana.rb

Constant Summary collapse

PERSONAL_ACCESS_TOKEN_TEST_URL =
'https://app.asana.com/api/1.0/users/me'
TASK_URL_TEMPLATE =
'https://app.asana.com/api/1.0/tasks/%{task_gid}'
STORY_URL_TEMPLATE =
'https://app.asana.com/api/1.0/tasks/%{task_gid}/stories'

Constants included from Base::Integration

Base::Integration::BASE_ATTRIBUTES, Base::Integration::BASE_CLASSES, Base::Integration::DEV_INTEGRATION_NAMES, Base::Integration::INSTANCE_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::INTEGRATION_NAMES, Base::Integration::PROJECT_AND_GROUP_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::PROJECT_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::SECTION_TYPE_CONFIGURATION, Base::Integration::SECTION_TYPE_CONNECTION, Base::Integration::SECTION_TYPE_TRIGGER, Base::Integration::SNOWPLOW_EVENT_ACTION, Base::Integration::SNOWPLOW_EVENT_LABEL, Base::Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Integration

#activate!, #activate_disabled_reason, #activated?, #api_field_names, #async_execute, #attributes, #attribution_notice, #category, #chat?, #ci?, #configurable_events, #deactivate!, #default_test_event, #description, #dup, #editable?, #event_channel_names, #event_names, #fields, #form_fields, #group_level?, #help, #inheritable?, #initialize_properties, #instance_level?, #json_fields, #manual_activation?, #operating?, #parent, #project_level?, #reencrypt_properties, #reset_updated_properties, #secret_fields, #sections, #supported_events, #supports_data_fields?, #testable?, #title, #to_database_hash, #to_param, #toggle!, #updated_properties

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.descriptionObject



30
31
32
# File 'app/models/integrations/asana.rb', line 30

def self.description
  s_('AsanaService|Add commit messages as comments to Asana tasks.')
end

.helpObject



34
35
36
# File 'app/models/integrations/asana.rb', line 34

def self.help
  build_help_page_url('user/project/integrations/asana.md', s_('Add commit messages as comments to Asana tasks.'))
end

.supported_eventsObject



42
43
44
# File 'app/models/integrations/asana.rb', line 42

def self.supported_events
  %w[push]
end

.titleObject



26
27
28
# File 'app/models/integrations/asana.rb', line 26

def self.title
  'Asana'
end

.to_paramObject



38
39
40
# File 'app/models/integrations/asana.rb', line 38

def self.to_param
  'asana'
end

Instance Method Details

#check_commit(message, push_msg) ⇒ Object



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
# File 'app/models/integrations/asana.rb', line 62

def check_commit(message, push_msg)
  # matches either:
  # - #1234
  # - https://app.asana.com/0/{project_gid}/{task_gid}
  # optionally preceded with:
  # - fix/ed/es/ing
  # - close/s/d
  # - closing
  issue_finder = %r{(?:https://app\.asana\.com/\d+/\w+/(\w+)|#(\w+))}i
  proceded_keyword_finder = %r{(fix\w*|clos[ei]\w*)\s*\z}i

  message.split(issue_finder).each_slice(2) do |prepended_text, task_id|
    next unless task_id

    begin
      story_on_task_url = format(STORY_URL_TEMPLATE, task_gid: task_id)
      Gitlab::HTTP.post(story_on_task_url, headers: { "Authorization" => "Bearer #{api_key}" }, body: { text: "#{push_msg} #{message}" })

      if prepended_text.match?(proceded_keyword_finder)
        task_url = format(TASK_URL_TEMPLATE, task_gid: task_id)
        Gitlab::HTTP.put(task_url, headers: { "Authorization" => "Bearer #{api_key}" }, body: { completed: true })
      end
    rescue StandardError => e
      log_error(e.message)
      next
    end
  end
end

#execute(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/integrations/asana.rb', line 46

def execute(data)
  return unless supported_events.include?(data[:object_kind])

  branch = Gitlab::Git.ref_name(data[:ref])

  return unless branch_allowed?(branch)

  user = data[:user_name]
  project_name = project.full_name

  data[:commits].each do |commit|
    push_msg = s_("AsanaService|%{user} pushed to branch %{branch} of %{project_name} ( %{commit_url} ):") % { user: user, branch: branch, project_name: project_name, commit_url: commit[:url] }
    check_commit(commit[:message], push_msg)
  end
end

#test(_) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/models/integrations/asana.rb', line 91

def test(_)
  result = Gitlab::HTTP.get(PERSONAL_ACCESS_TOKEN_TEST_URL, headers: { "Authorization" => "Bearer #{api_key}" })

  if result.success?
    { success: true }
  else
    { success: false, result: result.message }
  end
end