Class: Fgi::GitService

Inherits:
Object
  • Object
show all
Extended by:
HttpRequests
Defined in:
lib/fgi/git_service.rb

Class Method Summary collapse

Methods included from HttpRequests

get, post

Class Method Details

.current_branchObject



107
108
109
# File 'lib/fgi/git_service.rb', line 107

def current_branch
  `git branch | grep '*'`.gsub('* ', '').chomp
end

.fix_issue(options) ⇒ Object

All the process initiated by the issue fix

=> Commiting with a 'Fix #<id>' message
=> Pushing the current branch to the remote repo
=> Return to the default project branch

Parameters:

  • options (Hash)

    the options given by the user in the command line



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fgi/git_service.rb', line 54

def fix_issue(options)
  if ISSUES[current_branch].nil?
    puts "We could not find any issues to fix on your current branch (#{current_branch})."
    exit!
  end
  set_spent_time(options: options, issue_id: ISSUES[current_branch][:id])
  git_remote = ask_for_remote
  `git add .`
  puts "Are you sure to want to close the issue #{ISSUES[current_branch][:title]} ?"
  begin
    input = STDIN.gets.chomp
    if %w[y yes].include?(input)
      commit_message =  "Fix ##{ISSUES[current_branch][:id]}"
      commit_message += " - #{options[:fix_message]}" unless options[:fix_message].nil?
      `git commit -a --allow-empty -m '#{commit_message}'`
      `git push #{git_remote} HEAD`
      `git branch -u #{git_remote}/#{ISSUES[current_branch]}` # Define the upstream branch
      `git checkout #{CONFIG[:default_branch]}` # Be sure to be on the default branch.
      remove_issue(current_branch)
      puts "Congrat's ! You're now back to work on the default branch (#{CONFIG[:default_branch]})"
    end
  rescue Interrupt
    puts %q"Why did you killed me ? :'("
    exit!
  end
end

.new_issue(title:, options: {}) ⇒ Object

All the process initiated by the issue creation

=> Create issue
=> Create a new branch from the default one
=> Set the issue estimation time

Parameters:

  • title (String)

    the issue title

  • options (Hash) (defaults to: {})

    the options given by the user in the command line



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fgi/git_service.rb', line 23

def new_issue(title:, options: {})
  git_service = CONFIG[:git_service_class].new
  title = get_issue_title if title.nil?
  response = create_issue(title: title, git_service: git_service)

  if CONFIG[:default_branch].nil?
    puts "\n/!\\ FGI IS NOT UP-TO-DATE /!\\"
    puts 'We are not able to create and switch you to the new branch.'
    puts 'Delete .config.fgi.yml and reconfigure fgi by running `fgi config`'
  elsif !response['iid'].nil?
    branch_name = snakify(title)
    branch_name = "#{options[:prefix]}/#{branch_name}" unless options[:prefix].nil?
    save_issue(branch: branch_name, id: response['iid'], title: response['title'].tr("'", ' ').tr('_', ' '))
    create_branch(name: branch_name, from_current: options[:from_current]) unless options[:later]
    unless options[:duration].nil?
      res = set_issue_time_trackers(
              issue_id:    response['iid'],
              duration:    options[:duration],
              git_service: git_service,
              tracker:     :estimate
            )
      post_estimation_display(res['human_time_estimate'], options[:duration])
    end
  end
end

.servicesArray<String>

Returns an array containing all the git services available.

Returns:

  • (Array<String>)

    an array containing all the git services available.



9
10
11
12
13
14
15
# File 'lib/fgi/git_service.rb', line 9

def services
  services = []
  Dir.entries("#{File.dirname(__FILE__)}/git_services").each do |service|
    services << service.gsub(/.rb/, '').to_sym unless %w[. ..].include?(service)
  end
  services
end

.set_issue_time_trackers(issue_id:, duration:, git_service:, tracker:) ⇒ Object

TODO, Make sure it works for all git services The method to set the estimation time to resolve the issue

Parameters:

  • issue_id (Integer)

    the issue id to set its estimation time

  • estimation (String)

    the estimation time given by the user

  • git_service (Class)

    the git service class to use for this project

  • tracker (Symbol)

    the tracker to set. Can be :estimate or :spent



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fgi/git_service.rb', line 87

def set_issue_time_trackers(issue_id:, duration:, git_service:, tracker:)
  return if duration.nil? || !%i[estimate spent].include?(tracker)
  # Since GitLab version isn't up to date, we should be able
  #   to add estimations and spent time in issues comments (/estimate, /spent)
  api = if tracker == :spent
          'add_spent_time'
        else
          'time_estimate'
        end
  url_with_querystring = "#{git_service.routes[:issues]}/#{issue_id}/#{api}?duration=#{duration}"
  headers = { git_service.token_header => TOKEN, 'Content-Type' => 'application/json' }
  response = post(url: url_with_querystring, headers: headers)
  # GitLab sucks sometimes... This API is an example
  begin
    JSON.parse(response[:body])
  rescue Exception
    response[:body]
  end
end