将数据从哈希数组传递到 Slack 消息

Pass data from array of hashes to Slack message

我正在开发一个 slack 机器人,它从 Jira 项目向用户发送直接消息,并向他们分配有待完成的任务。我正在使用 Jira-Ruby gem 从 Jira 获取所有数据。

根据 gem 文档,我设置了一个 Jira 连接:

def project_board(project_key)
  client = JIRA::Client.new(options)
  client.Project.find(project_key)
end

并且我创建了一种方法来仅获取没有 done 状态

的已分配门票
def fetch_data
  project = project_board
  project.issues.map do |issue|
    next unless issue.fields.dig('status', 'name') != 'Done' && !issue.fields.dig('assignee', 'name').nil?

    {
      key: issue.key,
      name: issue.fields.dig('assignee', 'name'),
      status: issue.fields.dig('status', 'name')
    }
  end.compact
end

这给了我一个哈希数组:

=> [{:key=>"48", :name=>"john.john1", :status=>"Waiting for DevOps"},
 {:key=>"46", :name=>"john.john3", :status=>"In Progress"},
 {:key=>"45", :name=>"eric.forman", :status=>"Waiting for DevOps"},
 {:key=>"42", :name=>"john.john3", :status=>"Waiting for DevOps"},
 {:key=>"40", :name=>"john.john3", :status=>"Waiting for DevOps"},
 {:key=>"39", :name=>"eric.forman", :status=>"Waiting for DevOps"}]

如何将这些数据传递给松弛消息,使其看起来像下面这样?

message to john.john1

"Hi john.john1, here is the list of your today tasks:
-------------------------------------------
48 - With status: Waiting for DevOps
link https://company_name.atlassian.net/48
"

message to eric.forman:

"Hi eric.forman, here is the list of your today tasks:
-------------------------------------------
id: 45
status: Waiting for DevOps
link: https://company_name.atlassian.net/45
-------
id: 39
status: Waiting for DevOps
link: https://company_name.atlassian.net/45
"

等等

我建议您在格式化之前将 fetch_data 的 return 值更改为:

def fetch_data
  # Redacted for brevity ...
  end.compact.group_by { |task| task[:name] }
end

这将 return 像

这样的散列
{
 "john.john1"=>[
   {:key=>"48", :name=>"john.john1", :status=>"Waiting for DevOps"}
 ], 
 "john.john3"=>[
   {:key=>"46", :name=>"john.john3", :status=>"In Progress"}, 
   {:key=>"42", :name=>"john.john3", :status=>"Waiting for DevOps"}, 
   {:key=>"40", :name=>"john.john3", :status=>"Waiting for DevOps"}
 ], 
 "eric.forman"=>[
   {:key=>"45", :name=>"eric.forman", :status=>"Waiting for DevOps"},
   {:key=>"39", :name=>"eric.forman", :status=>"Waiting for DevOps"}
 ]
}

这应该可以更简单地遍历每个用户:

fetch_data.each do |user, task_list|
  messages = []
  messages << "Hi #{user}, here is the list of your tasks:"
  messages << "-------------------------------------------"
  task_list.each do |task|
  # format each task line
  end

  method_to_send_to_slack(messagges.join("\n"))
end