Class: CitizenCodeScripts::Begin

Inherits:
Base
  • Object
show all
Defined in:
lib/citizen_code_scripts/begin.rb

Constant Summary

Constants included from Colorize

Colorize::COLOR_CODES

Instance Attribute Summary

Attributes inherited from Base

#argv

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#app_names, #app_root, inherited, #initialize, load_scripts_deferred, name, run, script_classes, script_names, scripts, #staging_app_name, #step, #system!

Methods included from Colorize

#colorize, included

Constructor Details

This class inherits a constructor from CitizenCodeScripts::Base

Class Method Details

.descriptionObject



29
30
31
# File 'lib/citizen_code_scripts/begin.rb', line 29

def self.description
  "Starts your Pivotal Tracker story and opens a new PR on Github"
end

.helpObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/citizen_code_scripts/begin.rb', line 33

def self.help
  <<-EOF
citizen begin #{colorize(:light_blue, "[story id] [branch name]")}

Example: $ #{colorize(:light_blue, 'citizen begin "#133717234"')}

This command will start your Pivotal Tracker story for you, open a pull
request on Github, and copy over the Pivotal Tracker story description to
the Github pull request description. As well, any tasks in your
Pivotal Tracker story will automatically become [x] checkbox tasks on the
Github PR.

* All branch names will be auto-converted to
  kebab-case, lowercase

* Passing story id/branch name as arguments are optional - if
  they are missing, you'll be prompted
EOF
end

Instance Method Details

#runObject



53
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/citizen_code_scripts/begin.rb', line 53

def run
  story_id, branch_name = argv

  if !command?("hub")
    abort <<-EOF
You need to install `hub` before you can use this program.

brew install hub
EOF
  end

  pivotal = Pivotal.new(git_config("user.pivotalApiToken"))
  config_abort_if_blank!("user.pivotalApiToken", pivotal.token)

  project_id = git_config("user.pivotalProjectId")
  config_abort_if_blank!("user.pivotalProjectId", project_id)

  story_id ||= prompt("Please paste the Pivotal story ID here")
  story_id = story_id.gsub(/[^\d]/, '')

  story_url = "https://www.pivotaltracker.com/services/v5"\
    "/projects/#{project_id}/stories/#{story_id}"

  story = pivotal.request(story_url)

  default_branch_name = normalized_branch_name(story['name'])

  branch_name ||= prompt("Please enter a branch name (#{default_branch_name})")

  if branch_name.strip == ""
    branch_name = default_branch_name
  else
    branch_name = normalized_branch_name(branch_name)
  end

  # Start the story
  pivotal.request(story_url, method: "PUT", body: '{ "current_state":"started" }')

  silent "git checkout master",
    "git fetch origin",
    "git reset --hard origin/master"

  puts "==> Checking out #{branch_name}"

  silent "git checkout -b #{branch_name}",
    'git commit --allow-empty -m "Initial commit for story #' + story_id + '"',
    "git push origin #{branch_name}",
    "git branch --set-upstream #{branch_name} origin/#{branch_name}"

  tasks = pivotal.request(
  "https://www.pivotaltracker.com/services/v5"\
    "/projects/#{project_id}/stories/#{story_id}/tasks"
  )

  story_description = story['description']

  if story_description.nil?
    story_description = "_No story description given in Pivotal_"
  end

  description = <<-EOF
#{story['name']}

#{story['url']}

# Description

#{story_description}
EOF

  description.strip!

  unless tasks.empty?
    description += "\n\n## TODO\n\n"

    tasks.each do |task|
      description += "- [ ] #{task['description']}\n"
    end

    description.strip!
  end

  puts "==> Opening pull request on GitHub"

  tempfile = Tempfile.new('begin_pull_request')

  begin
    tempfile.write(description)
    tempfile.close

    labels = ['WIP', story['story_type']].join(',')

    url = `hub pull-request -F #{tempfile.path} -l "#{labels}" -a "" -o`

    # Copy it to your clipboard
    system("echo #{url} | pbcopy")
    puts url
  ensure
    tempfile.unlink
  end
end