<commit_before>require 'jira4r'
require 'activesupport'
require 'grit'

module Socialcast
  GIT_BRANCH_FIELD = 'customfield_10010'
  IN_STAGING_FIELD = 'customfield_10020'
  JIRA_CREDENTIALS_FILE = File.expand_path('~/.jira_key')

  def current_branch
    repo = Grit::Repo.new(Dir.pwd)
    Grit::Head.current(repo).name
  end
  def jira_credentials
    @credentials ||= YAML.load_file(JIRA_CREDENTIALS_FILE).symbolize_keys!
    @credentials
  end
  def jira_server
    #make sure soap4r is installed
    require 'jira4r'
    require "highline/import.rb"

    return @jira if @jira
    if !File.exists?(JIRA_CREDENTIALS_FILE)
      input = {}
      input[:username] = HighLine.ask("JIRA username: ")
      input[:password] = HighLine.ask("JIRA password: ") { |q| q.echo = "*" }

      File.open(JIRA_CREDENTIALS_FILE, "w") do |f|
        f.write input.to_yaml
      end
    end
    File.chmod 0600, JIRA_CREDENTIALS_FILE
    credentials = jira_credentials

    begin
      @jira = Jira4R::JiraTool.new 2, "https://issues.socialcast.com"
      @jira.login credentials[:username], credentials[:password]
      return @jira
    rescue => e
      puts "Error: #{e.message}"
      File.delete config_file
      raise e
    end
  end

  def update_ticket(ticket, options = {})
    fields = []
    fields << Jira4R::V2::RemoteFieldValue.new(GIT_BRANCH_FIELD, [options[:branch]]) if options[:branch]
    fields << Jira4R::V2::RemoteFieldValue.new(IN_STAGING_FIELD, ['true']) if options[:in_staging]
    jira_server.updateIssue ticket, fields
  end
  def start_ticket(ticket)
    transition_ticket_if_has_status ticket, 1, 11
  end
  def resolve_ticket(ticket)
    transition_ticket_if_has_status ticket, 3, 21
  end
  def release_ticket(ticket)
    transition_ticket_if_has_status ticket, 5, 101
  end
  def transition_ticket_if_has_status(ticket, status, action)
    issue = jira_server.getIssue ticket
    if issue.status == status.to_s
      jira_server.progressWorkflowAction ticket, action.to_s, []
    end
  end

  def run_cmd(cmd)
    puts "\nRunning: #{cmd}"
    raise "#{cmd} failed" unless system cmd
  end

  def reset_branch(branch)
    run_cmd "git branch -D #{branch}"
    run_cmd "git push origin :#{branch}"
    run_cmd "git checkout master"
    run_cmd "git checkout -b #{branch}"
    run_cmd "grb publish #{branch}"
  end

  def integrate(branch, destination_branch = 'staging')
    puts "integrating #{branch} into #{destination_branch}"
    run_cmd "git remote prune origin"
    unless destination_branch == 'master'
      run_cmd "git branch -D #{destination_branch}" rescue nil
      run_cmd "grb track #{destination_branch}"
    end
    run_cmd "git checkout #{destination_branch}"
    run_cmd "git pull . #{branch}"
    run_cmd "git push origin HEAD"

    run_cmd "git checkout #{branch}"
  end<commit_msg>fix resetting branch if not tracked<commit_after>require 'jira4r'
require 'activesupport'
require 'grit'

module Socialcast
  GIT_BRANCH_FIELD = 'customfield_10010'
  IN_STAGING_FIELD = 'customfield_10020'
  JIRA_CREDENTIALS_FILE = File.expand_path('~/.jira_key')

  def current_branch
    repo = Grit::Repo.new(Dir.pwd)
    Grit::Head.current(repo).name
  end
  def jira_credentials
    @credentials ||= YAML.load_file(JIRA_CREDENTIALS_FILE).symbolize_keys!
    @credentials
  end
  def jira_server
    #make sure soap4r is installed
    require 'jira4r'
    require "highline/import.rb"

    return @jira if @jira
    if !File.exists?(JIRA_CREDENTIALS_FILE)
      input = {}
      input[:username] = HighLine.ask("JIRA username: ")
      input[:password] = HighLine.ask("JIRA password: ") { |q| q.echo = "*" }

      File.open(JIRA_CREDENTIALS_FILE, "w") do |f|
        f.write input.to_yaml
      end
    end
    File.chmod 0600, JIRA_CREDENTIALS_FILE
    credentials = jira_credentials

    begin
      @jira = Jira4R::JiraTool.new 2, "https://issues.socialcast.com"
      @jira.login credentials[:username], credentials[:password]
      return @jira
    rescue => e
      puts "Error: #{e.message}"
      File.delete config_file
      raise e
    end
  end

  def update_ticket(ticket, options = {})
    fields = []
    fields << Jira4R::V2::RemoteFieldValue.new(GIT_BRANCH_FIELD, [options[:branch]]) if options[:branch]
    fields << Jira4R::V2::RemoteFieldValue.new(IN_STAGING_FIELD, ['true']) if options[:in_staging]
    jira_server.updateIssue ticket, fields
  end
  def start_ticket(ticket)
    transition_ticket_if_has_status ticket, 1, 11
  end
  def resolve_ticket(ticket)
    transition_ticket_if_has_status ticket, 3, 21
  end
  def release_ticket(ticket)
    transition_ticket_if_has_status ticket, 5, 101
  end
  def transition_ticket_if_has_status(ticket, status, action)
    issue = jira_server.getIssue ticket
    if issue.status == status.to_s
      jira_server.progressWorkflowAction ticket, action.to_s, []
    end
  end

  def run_cmd(cmd)
    puts "\nRunning: #{cmd}"
    raise "#{cmd} failed" unless system cmd
  end

  def reset_branch(branch)
    run_cmd "git branch -D #{branch}" rescue nil
    run_cmd "git push origin :#{branch}"
    run_cmd "git checkout master"
    run_cmd "git checkout -b #{branch}"
    run_cmd "grb publish #{branch}"
  end

  def integrate(branch, destination_branch = 'staging')
    puts "integrating #{branch} into #{destination_branch}"
    run_cmd "git remote prune origin"
    unless destination_branch == 'master'
      run_cmd "git branch -D #{destination_branch}" rescue nil
      run_cmd "grb track #{destination_branch}"
    end
    run_cmd "git checkout #{destination_branch}"
    run_cmd "git pull . #{branch}"
    run_cmd "git push origin HEAD"

    run_cmd "git checkout #{branch}"
  end
{'fraction_non_alphanumeric': 0.10674652104985027, 'fraction_numerical': 0.011625858728201514, 'mean_word_length': 3.6540983606557376, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188709', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1932, 'n_words': 456}