module GitRelease

Constants

VERSION

Public Class Methods

add_doc(repo, tag, text) click to toggle source
# File lib/git-release.rb, line 314
def self.add_doc(repo, tag, text)
    client = get_api_client
    releases = client.releases(repo)
    release = get_tag(releases, tag)

    if text.kind_of?(Array)
        text = text.join("\n")
    end
    if release.body.nil? or release.body.empty?
        body = text
    else
        body = "#{release.body}\n#{text}"
    end
    client.update_release(release.url, :body => body)
end
ask_note(note) click to toggle source
# File lib/git-release.rb, line 153
def self.ask_note(note)
    note = $cli.ask("OAuth token note: ", String) { |q| q.default = note }
    if note.empty?
        exit 1
    end
    note
end
ask_otp() click to toggle source
# File lib/git-release.rb, line 145
def self.ask_otp
    otp = $cli.ask("GitHub 2FA: ", String)
    if otp.empty?
        exit 1
    end
    otp
end
ask_password() click to toggle source
# File lib/git-release.rb, line 137
def self.ask_password
    pass = $cli.ask("GitHub Password: ", String) { |q| q.echo = "*" }
    if pass.empty?
        exit 1
    end
    pass
end
ask_username() click to toggle source
# File lib/git-release.rb, line 129
def self.ask_username
    user = $cli.ask("GitHub User: ", String)
    if user.empty?
        exit 1
    end
    user
end
choose_delete(client, note) click to toggle source
# File lib/git-release.rb, line 191
def self.choose_delete(client, note)
    $cli.choose do |menu|
        menu.prompt = "Token '#{note}' already exists: "
        menu.choice("regenerate token (will delete old one)") do
            delete_token(client, note)
            note
        end
        menu.choice("generate token with new name") do
            ask_note(note)
        end
    end
end
clear_doc(repo, tag) click to toggle source
# File lib/git-release.rb, line 307
def self.clear_doc(repo, tag)
    client = get_api_client
    releases = client.releases(repo)
    release = get_tag(releases, tag)
    client.update_release(release.url, :body => "")
end
delete_token(client, note) click to toggle source
# File lib/git-release.rb, line 161
def self.delete_token(client, note)
    otp = ""
    id = ""
    done = false
    while !done do
        begin
            if id.empty?
                if otp.empty?
                    auths = client.authorizations
                else
                    auths = client.authorizations(
                        :headers => { "X-GitHub-OTP" => otp})
                end
                id = auths.find { |a| a.note.include?(note) }.id
            else
                if otp.empty?
                    client.delete_authorization(id)
                else
                    client.delete_authorization(
                        id,
                        :headers => { "X-GitHub-OTP" => otp})
                end
                done = true
            end
        rescue Octokit::OneTimePasswordRequired => e
            otp = ask_otp
        end
    end
end
do_login() click to toggle source
# File lib/git-release.rb, line 97
def self.do_login
    if File.file?(File.expand_path($token_file))
        begin
            test = get_api_client
            test.repos
        rescue
            client = get_user_client
            token = get_token(client)
            store_token(token)
        end
    else
        client = get_user_client
        token = get_token(client)
        store_token(token)
    end
end
get_api_client() click to toggle source
# File lib/git-release.rb, line 87
def self.get_api_client
    Octokit::Client.new(:access_token => load_token)
end
get_repo() click to toggle source
# File lib/git-release.rb, line 75
def self.get_repo
    repo = `git remote get-url origin`.
        sub("\n", "").
        gsub(/.*github.com./, "")
    if $?.exitstatus == 0
        repo
    else
        puts "not a git repository"
        exit 1
    end
end
get_tag(releases, tag) click to toggle source
# File lib/git-release.rb, line 276
def self.get_tag(releases, tag)
    release = releases.find { |r| r.tag_name == tag }
    if release.empty?
        puts "unknown tag #{tag}"
        exit 1
    end
    release
end
get_token(client) click to toggle source
# File lib/git-release.rb, line 204
def self.get_token(client)
    token = ""
    note = 'Git Release CLI',
    otp = ""
    while token.empty? do
        begin
            if otp.empty?
                token = client.create_authorization(
                    :scopes => ['repo'],
                    :note => note)
            else
                token = client.create_authorization(
                    :scopes => ['repo'],
                    :note => note,
                    :headers => { "X-GitHub-OTP" => otp})
            end
        rescue Octokit::OneTimePasswordRequired => e
            otp = ask_otp
        rescue Octokit::UnprocessableEntity => e
            if e.message.include?("already_exists")
                note = choose_delete(client, note)
            else
                puts e
                exit 1
            end
        rescue => e
            puts e
            exit 1
        end
    end
    token.token
end
get_user_client() click to toggle source
# File lib/git-release.rb, line 91
def self.get_user_client
    user = ask_username
    pass = ask_password
    client = Octokit::Client.new(:login => user, :password => pass)
end
load_token() click to toggle source
# File lib/git-release.rb, line 114
def self.load_token
    begin
        File.open(File.expand_path($token_file), &:readline)
    rescue
        puts "please login first"
        exit 1
    end
end
print_releases(repo) click to toggle source
run(args) click to toggle source
# File lib/git-release.rb, line 17
def self.run(args)
    args << 'help' if args.empty?
    command = args.shift

    case command
    when 'help'
        usage
    when 'version'
        version
    when 'login'
        login = do_login
        puts 'successfully logged in'
    when 'list'
        repo = get_repo
        print_releases(repo)
    when 'set'
        repo = get_repo
        tag = args.shift
        status = $status[args.shift] || unknown_status
        set_release(repo, tag, status)
    when 'doc'
        repo = get_repo
        task = args.shift
        tag = args.shift
        case task
        when 'clear'
            clear_doc(repo, tag)
        when 'add'
            add_doc(repo, tag, args)
        end
    end
end
set_release(repo, tag, status) click to toggle source
# File lib/git-release.rb, line 285
def self.set_release(repo, tag, status)
    client = get_api_client
    releases = client.releases(repo)
    release = get_tag(releases, tag)
    case status
    when "release"
        prerelease = false
        draft = false
    when "prerelease"
        prerelease = true
        draft = false
    when "draft"
        prerelease = true
        draft = true
    end

    client.update_release(
        release.url,
        :prerelease => prerelease,
        :draft => draft)
end
store_token(token) click to toggle source
# File lib/git-release.rb, line 123
def self.store_token(token)
    f = File.new(File.expand_path($token_file), 'w', 0600)
    f.write(token)
    f.close
end
unknown_status() click to toggle source
# File lib/git-release.rb, line 69
def self.unknown_status
    puts "unknown status"
    puts "use one of: r[elease], p[rerelease] or d[raft]"
    exit 1
end
usage() click to toggle source
# File lib/git-release.rb, line 50
def self.usage
    puts "usage: git release <command> [<args>]"
    puts ""
    puts "These are the available commands:"
    puts " help                 print this message"
    puts " version              print the current version"
    puts " login                create or verify api token"
    puts " list                 list all releases"
    puts " set <tag> <state>    change the status of a release"
    puts "     tag              tag of the release to change"
    puts "     state            new state. either r[elease], p[rerelease] or d[raft]"
    puts " doc clear <tag>      clear the release notes"
    puts " doc add <tag> <text> add one or more lines to the release notes"
end
version() click to toggle source
# File lib/git-release.rb, line 65
def self.version
    puts "git-release version #{GitRelease::VERSION}"
end