class Spreadtheword

Constants

CONNECTOR
NONASCII
VERSION

Public Class Methods

new(projects, options) click to toggle source
# File lib/spreadtheword.rb, line 16
def initialize(projects, options)
  @projects = projects.any? ? projects : [Dir.pwd]
  @title = options.title ? options.title : 'Relase Notes'
  @author = options.author ? options.author : gitUserName
  @since = options.since

  configureGitlab(options) if options.gitlabToken
  configureWrike(options) if options.wrikeToken
  configureGoogleTranslate(options) if options.googleTranslateKey

  @utils = Utils.new(options)
  @logs = []
  @topics = {}
end

Public Instance Methods

configureGitlab(options) click to toggle source
# File lib/spreadtheword.rb, line 31
def configureGitlab(options)
  Gitlab.configure do |config|
    config.endpoint       = options.gitlabEndpoint
    config.private_token  = options.gitlabToken
  end
  @gitlab = URI(options.gitlabEndpoint)
  @gitlabCurrentProject = nil
  @gilabProjects = {}
  @gitlabCache = {}
end
configureGoogleTranslate(options) click to toggle source
# File lib/spreadtheword.rb, line 50
def configureGoogleTranslate(options)
  @translate = Google::Cloud::Translate.new(key: options.googleTranslateKey)
  translateCache = {}
  @getTranslation = lambda { |sentence|
  unless translateCache[sentence]
    @utils.say "Translating\n-> #{sentence}\n"
    translateCache[sentence] = CGI.unescapeHTML(@translate.translate(sentence, to: "en").text)
    @utils.say "<- #{translateCache[sentence]}\n"
  end
  translateCache[sentence]
}
end
configureWrike(options) click to toggle source
# File lib/spreadtheword.rb, line 42
def configureWrike(options)
  Wrike3.configure do |config|
    config.access_token  = options.wrikeToken
  end
  @wrike = Wrike3()
  @wrikeCache = {}
end
fetchAllLogs() click to toggle source
# File lib/spreadtheword.rb, line 110
def fetchAllLogs
  @projects.each do |project|
    @utils.say "Fetching git commit logs from #{project}\n"
    Dir.chdir(project) do
      gitlabSetCurrentProject if @gitlab
      fetchLogs
    end
  end
end
fetchLogs() click to toggle source
# File lib/spreadtheword.rb, line 120
def fetchLogs
  cmd = %Q{git log --pretty=format:"%an#{CONNECTOR}%s#{CONNECTOR}%H"}
  if @since
    cmd = %Q{#{cmd} #{@since}..master}
  end
  logs = `#{cmd}`.to_s.split("\n")
  logs.delete_if do |x|
    x.nil? || '' == x.to_s.strip
  end
  logs.map! do |x|
    contents = x.split(CONNECTOR)
    if contents[1].nil? || '' == contents[1].to_s.strip
      contents[1] = ''
    end
    OpenStruct.new.tap do |y|
      y.author = contents[0]
      y.origMsg = contents[1]
      y.shaHash = contents[2]
      if @translate && y.origMsg =~ NONASCII
        y.msg = @getTranslation.call(contents[1])
      else
        y.msg = y.origMsg
      end
      if @gitlab
        y.gitlabProject = @gitlabCurrentProject
      end
    end
  end
  @logs.concat logs
end
getGitlab(projectId, issueNumber) click to toggle source
# File lib/spreadtheword.rb, line 63
def getGitlab(projectId, issueNumber)
  unless @gitlabCache[projectId] && @gitlabCache[projectId][issueNumber]
    @gitlabCache[projectId] ||= {}
    @gitlabCache[projectId][issueNumber] = Gitlab.issue(projectId, issueNumber)
  end
  return @gitlabCache[projectId][issueNumber]
end
getWrike(wId) click to toggle source
# File lib/spreadtheword.rb, line 71
def getWrike(wId)
  unless @wrikeCache[wId]
    permalink = "https://www.wrike.com/open.htm?id=#{wId}"
    @utils.say "Fetching Wrike task #{permalink}"
    tasks = @wrike.task.list nil, nil, permalink: permalink
    @utils.say "."
    taskId = tasks['data'][0]['id']
    task = @wrike.task.details taskId
    @utils.say "."
    @wrikeCache[wId] = task['data'][0]
    @wrikeCache[wId][:spreadthewordPermalink] = permalink
    @utils.say "\n"
  end
  return @wrikeCache[wId]
end
gitUserName() click to toggle source
# File lib/spreadtheword.rb, line 214
def gitUserName
  `git config --get user.name`.to_s.strip
end
gitlabSetCurrentProject() click to toggle source
# File lib/spreadtheword.rb, line 95
def gitlabSetCurrentProject
  remotes = `git remote -v`
  remotes.to_s.split("\n").each do |line|
    if line.include?(@gitlab.host)
      lines = line.split(@gitlab.host)
      liness = lines[1].split('/')
      @gitlabCurrentProject = {
        namespace: liness[1],
        project: liness[2].split('.git')[0],
      }
      return
    end
  end
end
parseTopics() click to toggle source
# File lib/spreadtheword.rb, line 151
def parseTopics
  @logs.each do |x|
    origin = :plain
    identifier = nil
    payload = nil
    title = 'Others'
    begin
      if x.origMsg =~ /\{W(\d+)\}/
        origin = :wrike
        identifier = "W#{$1}"
        payload = getWrike($1)
        title = payload['title']
        x.msg = x.msg.gsub(/\{W\d+\}/, '')
      elsif x.origMsg =~ /\{#(\d+)\}/
        origin = :gitlab
        targetProjectId = "#{x.gitlabProject[:namespace]}/#{x.gitlabProject[:project]}"
        identifier = "#{targetProjectId}##{$1}"
        payload = getGitlab(targetProjectId, $1)
        title = payload.title
        x.msg = x.msg.gsub(/\{#\d+\}/, '')
      elsif x.origMsg =~ /\{(.+)#(\d+)\}/
        origin = :gitlab
        if $1.include?('/')
          targetProjectId = $1.dup
        else
          targetProjectId = "#{x.gitlabProject[:namespace]}/#{$1}"
        end
        identifier = "#{targetProjectId}##{$2}"
        payload = getGitlab(targetProjectId, $2)
        title = payload.title
        x.msg = x.msg.gsub(/\{.+#\d+\}/, '')
      end
    rescue => e
      STDERR.puts "!!! Exception when parsing topic !!! #{e}"
      origin = :plain
      identifier = nil
      payload = nil
      title = 'Others'
    end
    if @translate && title =~ NONASCII
      title = @getTranslation.call(title)
    end
    @topics[identifier] ||= []
    @topics[identifier] << {
      origin: origin,
      commit: x,
      payload: payload,
      title: title,
    }
  end
end
run!() click to toggle source
# File lib/spreadtheword.rb, line 87
def run!
  fetchAllLogs
  parseTopics
  sortTopics
  writer = Spreadtheword::LaTeX.new(@title, @author, @topics, @getTranslation, @gitlab)
  writer.write!
end
sortTopics() click to toggle source
# File lib/spreadtheword.rb, line 203
def sortTopics
  topicsH = @topics
  @topics = []
  topicsH.each do |k,v|
    @topics << [k,v]
  end
  @topics.sort! do |x,y|
    y[1].size <=> x[1].size
  end
end