推送代码后,Pivotal Tracker故事未得到更新

时间:2019-04-19 20:21:22

标签: tfs githooks pivotaltracker

我们有一个与GitHub集成的关键跟踪器,当我们过去使用故事ID进行提交时,它用于使用提交消息更新故事状态来更新故事。

我们最近从GitHub迁移到Team Foundation Server,并且该集成不再起作用。

好像还没有integration App存在。

有没有编程的方式来做到这一点?

1 个答案:

答案 0 :(得分:0)

创建名为pre-push的文件(不带任何扩展名),并将其放在存储库中的./.git/hooks/文件夹下。如果它是有效的存储库,则该文件夹应该已经存在。将以下代码复制粘贴到文件中。不要忘记在以下代码中替换API令牌值-

#!/usr/bin/env ruby
# encoding: UTF-8

require 'net/https'
require 'json'

class GitLogs
  attr_accessor :raw_log, :commit_data, :commit, :author, :message, :refs

  def initialize
    self.commit = 'Commit: ' + `git log -1 --pretty=format:%h`.force_encoding('utf-8')
    self.author = 'Author: ' + `git log -1 --pretty=format:%aN`.force_encoding('utf-8')
    self.message = 'Message: ' + `git log -1 --pretty=format:%s`.force_encoding('utf-8')
    self.refs = 'Refs: ' + `git log -1 --pretty=format:%d`.force_encoding('utf-8')

    # Example git formatted log output:
    #
    # Commit: 8872e8fe03a10238d7be84d78813874d79ce0c3d
    # Author: John Doe <john.doe@unknown.com>
    # Message: [#90743834] test new v5 hook addition
    # Refs:  (HEAD, feature/hook-test)

    parse!
    self
  end

  def parse!
    self.commit_data = GitLog.new(self.commit, self.author, self.message, self.refs)
  end

  def pivotal_sync!
    Pivotal.new(commit_data).send! if commit_has_story_id?
  end

  def commit_has_story_id?
    # somewhere between square brackets there has to be a hash followed by multiple digits
    !commit_data.message.scan(/\[*+\#(\d+)\D?(.*)\]/).empty?
  end

end

class GitLog
  attr_accessor :hash, :author, :message, :refs

  def initialize hash, author, message, refs
    self.hash     = hash
    self.author   = author
    self.refs     = refs
    self.message  = message

    updated_message
  end

  def updated_message
    return message
  end

  def to_json
    { source_commit:
      { commit_id:  self.hash,
        author:     self.author,
        message:    self.message,
      }
    }.to_json
  end
end

class Pivotal
  attr_accessor :git_log, :tracker_token

  BASE_URI = URI('https://www.pivotaltracker.com/')

  def initialize git_log
    self.git_log        = git_log
    self.tracker_token  = get_token
  end

  def get_token
    'YOUR APT TOKEN GOES HERE. CAN GET IT FROM https://www.pivotaltracker.com/profile'
  end

  def send!
    https = Net::HTTP.start(BASE_URI.host, 443, {
      use_ssl:      true,
      verify_mode:  OpenSSL::SSL::VERIFY_NONE
    })

    request = Net::HTTP::Post.new('/services/v5/source_commits')
    request['X-TrackerToken'] = tracker_token
    request['Content-type']   = 'application/json'
    request.body              = git_log.to_json

    response                  = https.request(request)
  end
end

GitLogs.new().pivotal_sync!