在jira票证上创建远程注释

时间:2017-05-18 00:17:20

标签: ruby jira gerrit

我有一个用Ruby编写的脚本,当我的gerrit服务器上更新了ref时,它将更新jira服务器。我对Ruby很新,很困惑。

当对gerrit服务器进行推送时,如何触发对jira问题的评论。以下是我到目前为止编写的代码:

#!/usr/local/bin/ruby
#
#ref-updated hook for gerrit
#
#ref-updated --oldrev <old rev> --newrev <new rev> --refname <ref name> --project <project name> --submitter <submitter>
#
# Called whenever a ref has been updated.
#
# * Checks if the branch is hotfix, and emails the hotfix team
# * Updates JIRA with the commit message, and maybe flips the status
#
#

require File.join(File.dirname(__FILE__), 'common.rb')

require "rubygems"
require "net/smtp"
require 'net/http'
require 'net/https'
require 'uri'
require "jira-ruby"

class Gerrit < HookBase

  #Jira information for the jira4r stuff
  JIRA_SITE = XXXXXXX
  JIRA_USERNAME = XXXXXXX
  JIRA_PASSWORD = XXXXXXX

  attr_reader :oldrev, :newrev, :refname, :project, :submitter, :commit_message, :author, :branch, :tag


  def initialize(options)
    opts = {
      :username => JIRA_USERNAME,
      :password => JIRA_PASSWORD,
      :site     => JIRA_SITE,
      :context_path => '',
      :auth_type => :basic,
      :read_timeout => 120
    }
    @client = JIRA::Client.new(options.nil? ? opts : options)
  end

  client = JIRA::Client.new(options)

  def print_commit_info
    puts "OLDREV:           #{@oldrev}"
    puts "NEWREV:           #{@newrev}"
    puts "REFNAME:          #{@refname}"
    puts "PROJECT:          #{@project}"
    puts "SUBMITTER:        #{@submitter}"
  end

  def generate_comment
    comment_str = ""
    comment_str << "Commit:       #{self.newrev}\n"
    comment_str << "Author:       #{self.author}\n"
    comment_str << "Repository:   #{self.project}\n"
    if self.branch != ""
      comment_str << "branch:       #{self.branch}\n"
    elsif self.tag != ""
      comment_str << "tag:          #{self.tag}\n"
    end
    comment_str << "\n#{self.commit_message}\n"

    comment_str
  end

下一部分代码我需要帮助确定如何使用jira-ruby而不是jira4r进行推送。

  def send_jira_comment_and_status
    comment = Jira4R::V2::RemoteComment.new
    comment.author = JIRA_USERNAME
    comment.body = generate_comment

    #find issue key and new_status. add comment to jira
    if @commit_message =~ /^[a-zA-Z]+-[0-9]+/
      issue_string = @commit_message.match(/(.*?):/)[1]
      issue_string.split(",").each do |snippet|
        snippet.strip!
        snippet =~ /([A-Z]+-[0-9]+)\s?(resolved|reopen|closed)?/i
        key = $1
        new_status = $2

        if !key.nil?
          next if key =~ /^NOJIRA/i

          jira.addComment(key, comment)

          if !new_status.nil?
            #define the workflow and send new status if possible to jira
            status_string = case new_status
                            when "resolved" then "Ready for Test"
                            when "closed"   then "Close"
                            when "reopen"   then "Reopen"
                            end

            if status = jira.getAvailableActions(key).find { |a| a.name == status_string }
              jira.progressWorkflowAction(key, status.id.to_s, [])
            end
          end
        end
  rescue Exception => e
    puts "JIRA update failed"
    puts e.message
    puts e.backtrace.inspect
  end

如果您有任何想法,请提供帮助。

更新:

  def generate_comment
    comment_str = ""
    comment_str << "Commit:       #{self.newrev}\n"
    comment_str << "Author:       #{self.author}\n"
    comment_str << "Repository:   #{self.project}\n"
    if self.branch != ""
      comment_str << "branch:       #{self.branch}\n"
    elsif self.tag != ""
      comment_str << "tag:          #{self.tag}\n"
    end
    comment_str << "\n#{self.commit_message}\n"

    comment_str
  end

  def send_jira_comment_and_status
    #find issue key and new_status. add comment to jira
    if @commit_message =~ /^[a-zA-Z]+-[0-9]+/
      issue_string = @commit_message.match(/(.*?):/)[1]
      issue_string.split(",").each do |snippet|
        snippet.strip!
        snippet =~ /([A-Z]+-[0-9]+)\s?(resolved|reopen|closed)?/i
        key = $1
        new_status = $2

        if !key.nil?
          next if key =~ /^NOJIRA/i
# comment = JIRA::RemoteComment.new
          issue = @client.Issue.find(issue_string)
          comment = issue.comments.build
          comment.author = JIRA_USERNAME
          comment.body = generate_comment
          comment.save 

我对如何生成新评论以及如何建立实际连接感到困惑?我也是Ruby的新手,所以我不知道我的语法是否正确。我至少想知道逻辑是否有意义。谢谢!

0 个答案:

没有答案