Last active
September 9, 2024 18:07
-
-
Save gwennlbh/42e5bf808be9f1d79668ff1e30c725b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "pathname" | |
require "json" | |
require "tty-prompt" | |
require "pastel" | |
conventional = true | |
LABEL_TO_CONVENTIONAL = { | |
bug: :fix, | |
perf: :perf, | |
performance: :perf, | |
important: :fix, | |
enhancement: :feat, | |
feature: :feat, | |
new_feature: :feat, | |
ui: :style, | |
styling: :style, | |
deployment: :chore, | |
testing: :test, | |
test: :test, | |
security: :fix, | |
documentation: :docs, | |
docs: :docs, | |
} | |
LABEL_TO_GITMOJI = { | |
bug: :bug, | |
perf: :zap, | |
performance: :zap, | |
important: :ambulance, | |
enhancement: :sparkles, | |
feature: :sparkles, | |
new_feature: :sparkles, | |
ui: :lipstick, | |
styling: :lipstick, | |
deployment: :rocket, | |
testing: :white_check_mark, | |
test: :white_check_mark, | |
security: :lock, | |
documentation: :memo, | |
docs: :memo, | |
analytics: :chart_with_upwards_trend, | |
legal: :page_facing_up, | |
breaking_changes: :boom, | |
breaking: :boom, | |
assets: :bento, | |
brand: :bento, | |
branding: :bento, | |
brand_resources: :bento, | |
accessibility: :wheelchair, | |
database: :card_file_box, | |
ux: :children_crossing, | |
architecture: :building_construction, | |
meta: :building_construction, | |
responsive: :iphone, | |
mobile: :iphone, | |
easter_egg: :egg, | |
seo: :mag, | |
typing: :label, | |
types: :label, | |
cli: :triangular_flag_on_post, | |
flags: :triangular_flag_on_post, | |
flag: :triangular_flag_on_post, | |
errors: :goal_net, | |
error_handling: :goal_net, | |
auth: :passport_control, | |
authentication: :passport_control, | |
dev: :technologist, | |
devx: :technologist, | |
} | |
prompt = TTY::Prompt.new active_color: :cyan | |
pastel = Pastel.new | |
gitmojis = JSON.load((Pathname.new(Dir.home) / ".gitmoji" / "gitmojis.json").read) | |
by_name = gitmojis.to_h{|e| [e['code'].gsub(':', '').to_sym, e]} | |
is_github = `git remote get-url origin`.include? 'github.com' | |
issues = [] | |
if is_github | |
issues = JSON.load `gh #{ARGV.first ? "-R " + ARGV.first : ""} issue list --json number,title,labels` | |
else | |
`glab #{ARGV.first ? "-R " + ARGV.first : ""} issue list -P 100`.each_line do |row| | |
if row.start_with? '#' | |
data = row.chomp.split "\t" | |
issues << { | |
number: data[0].sub('#', ''), | |
title: data[2], | |
labels: data[3].sub('(', '').sub(')', '').split(', ').map do |labelname| { name: labelname } end | |
} | |
end | |
end | |
end | |
by_number = issues.to_h{|i| [i[:number].to_i, i]} | |
# pp by_number | |
begin | |
pp issues.map | |
choices = issues.map do |i| | |
{ | |
name: pastel.cyan.bold("##{i[:number]}") + " #{i[:title]} " + "[#{i[:labels].map{|l| l[:name]}.join " "}]", | |
value: i[:number].to_i | |
} | |
end | |
issue = by_number[prompt.select("Close issue…", choices, cycle: true, per_page: 15, filter: true, symbols: { marker: pastel.bold("—")})] | |
found = nil | |
issue[:labels].each do |label| | |
name = label[:name].gsub('-', '_').to_sym | |
if conventional | |
found = LABEL_TO_CONVENTIONAL[name] if LABEL_TO_CONVENTIONAL.has_key? name | |
else | |
found = LABEL_TO_GITMOJI[name] if LABEL_TO_GITMOJI.has_key? name | |
end | |
end | |
if conventional | |
conventional = found || prompt.select("Select a type", LABEL_TO_CONVENTIONAL.values.uniq, cycle: true, per_page: 15, filter: true, symbols: { marker: pastel.bold("—")} ) | |
gitmoji = { name: conventional == "fix" ? "bug" : conventional, emoji: "#{conventional}:" } | |
else | |
gitmoji = by_name[found || prompt.select("Select a gitmoji", by_name.keys)] | |
end | |
rescue TTY::Reader::InputInterrupt | |
exit | |
end | |
keyword = gitmoji[:name] == "bug" ? "fixes" : "closes" | |
emoji = gitmoji[:emoji] | |
if conventional | |
scope = prompt.ask "Scope" do |q| | |
q.default "" | |
q.modify :strip, :collapse, :down | |
end | |
if scope != "" | |
emoji = "#{emoji.sub ':', ''}(#{scope}):" | |
end | |
end | |
title = prompt.ask "Message" do |q| | |
default_title = issue[:title].gsub('"', '\\"') | |
if conventional | |
default_title = default_title.downcase | |
end | |
q.default default_title | |
q.modify :strip, :collapse | |
if conventional | |
q.modify :down | |
end | |
end | |
message = "#{emoji} #{title} (#{keyword} ##{issue[:number]})" | |
if prompt.yes? "Commit with message \"#{message}\"?" | |
`git commit -m "#{message}"` | |
else | |
puts "Cancelled." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment