Class: GitStoryid

Inherits:
Object
  • Object
show all
Defined in:
lib/git_storyid.rb

Defined Under Namespace

Classes: Configuration, Error, JiraConfiguration, PivotalConfiguration, RefetchStories, SerializedIssue

Constant Summary collapse

TRACKER_CONFIG =
{
  pivotal: {
    class: PivotalConfiguration,
    options: {
      :api_token => "Api token (https://www.pivotaltracker.com/profile)",
      :me => "Your pivotal initials (e.g. BG)",
      :project_id => "Project ID",
    },
  },
  jira: {
    class: JiraConfiguration,
    options: {
      :site         => 'Site URL',
      project_id: "Project ID",
      :username     => 'Username',
      :password     => 'Password',
    }
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ GitStoryid

Returns a new instance of GitStoryid.



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

def initialize(*arguments)
  @tracker = Configuration.build
  @git_options = []
  parser = OptionParser.new do |opts|
    opts.banner = "Do git commit with information from pivotal story"
    opts.on("-m", "--message [MESSAGE]", "Add addional MESSAGE to comit") do |custom_message|
      @custom_message = custom_message
    end
    opts.on("-f", "--finish", "Specify that this commit finishes a story or fixes a bug") do
      @finish_stories = true
    end
    opts.on("-d", "--deliver", "Specify that this commit delivers a story or a bug") do
      @deliver_stories = true
    end
  end
  parser.parse!(arguments)

  unless arguments.empty?
    @stories = arguments.map do |argument|
      @tracker.find_story_by_id(argument)
    end
  end
end

Class Method Details

.output(message) ⇒ Object



13
14
15
# File 'lib/git_storyid.rb', line 13

def self.output(message)
  puts message
end

.run(*args) ⇒ Object



9
10
11
# File 'lib/git_storyid.rb', line 9

def self.run(*args)
  new(*args).run
end

Instance Method Details

#build_commit_messageObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/git_storyid.rb', line 118

def build_commit_message
  message = @stories.map do |story|
    "#{finish_story_prefix(story)}##{story.id}"
  end.join(", ")
  message = "[#{message}] "
  if @custom_message && !@custom_message.empty?
    message += @custom_message.to_s + "\n\n"
  end
  message += @stories.map do |story|
    "#{story.type.capitalize}: " + story.name.strip
  end.join("\n\n")
  message
end

#commit_changesObject



114
115
116
# File 'lib/git_storyid.rb', line 114

def commit_changes
  output execute("git", "commit", "-m", build_commit_message)
end

#ensure_changes_stashed!Object



142
143
144
145
146
# File 'lib/git_storyid.rb', line 142

def ensure_changes_stashed!
  if execute("git", "diff", "--staged").empty?
    quit "No changes staged to commit."
  end
end

#execute(*args) ⇒ Object



138
139
140
# File 'lib/git_storyid.rb', line 138

def execute(*args)
  Open3.popen3(*args) {|i, o| return o.read }
end

#fetch_story(index) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/git_storyid.rb', line 60

def fetch_story(index)
  if (1..100).include?(index.to_i)
    @tracker.all_stories[index.to_i - 1]
  else
    # Consider it a direct story id
    @tracker.find_story_by_id(index)
  end
end

#finish_story_prefix(story) ⇒ Object



132
133
134
135
136
# File 'lib/git_storyid.rb', line 132

def finish_story_prefix(story)
  return "Delivers " if @deliver_stories
  return "" unless @finish_stories
  story.type == "bug" ? "Fixes " : "Finishes "
end

#output(message) ⇒ Object



17
18
19
# File 'lib/git_storyid.rb', line 17

def output(message)
  self.class.output(message)
end

#quit(message) ⇒ Object



103
104
105
106
# File 'lib/git_storyid.rb', line 103

def quit(message)
  output message
  exit 1
end

#quit_if_no_storiesObject



69
70
71
72
73
# File 'lib/git_storyid.rb', line 69

def quit_if_no_stories
  if @tracker.all_stories.empty?
    quit "No stories started and owned by you."
  end
end

#readlineObject



99
100
101
# File 'lib/git_storyid.rb', line 99

def readline
  Readline.readline("Indexes: ", true)
end

#readline_stories_if_not_presentObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git_storyid.rb', line 46

def readline_stories_if_not_present
  if !@stories || @stories.empty?
    quit_if_no_stories
    output stories_menu
    @stories = readline_story_ids.map do |index|
      fetch_story(index) || quit("Story #{index} was not found.")
    end
  end
rescue RefetchStories
  @tracker.reset
  @stories = nil
  readline_stories_if_not_present
end

#readline_story_idsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/git_storyid.rb', line 84

def readline_story_ids
  input = readline
  if input =~ /\A\s*re?f?r?e?s?h?\s*\z/
    raise RefetchStories
  end
  ids = input.split(/\s*,\s*/).reject do |string|
    string.empty?
  end
  if ids.empty?
    quit("Cancelling.")
  else
    ids
  end
end

#runObject



108
109
110
111
112
# File 'lib/git_storyid.rb', line 108

def run
  ensure_changes_stashed!
  readline_stories_if_not_present
  commit_changes
end

#stories_menuObject



75
76
77
78
79
80
81
82
# File 'lib/git_storyid.rb', line 75

def stories_menu
  result = ""
  @tracker.all_stories.each_with_index do |story, index|
    result << "[#{index + 1}] #{story.name}\n"
  end
  result << "\n"
  result
end