Class: Copywriter

Inherits:
Object
  • Object
show all
Defined in:
lib/github-copywriter/regex.rb,
lib/github-copywriter.rb,
lib/github-copywriter/version.rb,
lib/github-copywriter/octikit_wrapper.rb

Overview

github-copywriter/octikit_wrapper.rb

Octokit wrapper for commiting multiple files to a GitHub repo.

Author: Ryan Jacobs <[email protected]>

Defined Under Namespace

Modules: Regex

Constant Summary collapse

COMMIT_MSG =
"Update copyright. ♥ github-copywriter\n\nFor more info, visit http://ryanmjacobs.github.io/github-copywriter"
VERSION =

Current version

Returns:

  • (String)
"1.2.4".freeze

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Copywriter

Returns a new instance of Copywriter.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/github-copywriter.rb', line 20

def initialize(username, password)
    # Auth to GitHub
    Octokit.auto_paginate = true
    @client = Octokit::Client.new(:login => username, :password => password)

    # Check if the basic_auth worked.
    # TODO: Find a better way to do this
    # https://github.com/ryanmjacobs/github-copywriter/issues/1
    begin
        @client.authorizations
    rescue Octokit::Unauthorized
        puts "error: Bad credentials.".red
        exit
    rescue Faraday::ConnectionFailed
        puts "error: Connection failed.".red
        exit
    end
end

Instance Method Details

#run!(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/github-copywriter.rb', line 39

def run!(options={})
    # Default options
    options = {
        all_repos: false,
        skip_forks: false,
        dry_run: false,
        year: nil,
        branches: nil
    }.merge(options)

    if options[:all_repos] then
        repos = @client.repositories()
    else
        repos = Array.new
        options[:repos].each do |r|
            name = @client.+"/"+File.basename(r)
            if @client.repository?(name) then
                repos << @client.repository(name)
            else
                puts "error: repo \"#{name}\" does not exist!".red
                exit
            end
        end
    end

    # Get copyright year
    cur_year = options[:year] || Time.now.year

    # Loop through each repo
    repos.each do |repo|

        # Skip if repo is a fork and --skip-forks is on
        next if options[:skip_forks] and repo[:fork]

        # Repo name
        repo_name = repo[:full_name]
        puts "\n#{repo_name}:".cyan

        # Repo refs (branches) to loop through
        if options[:branches] == nil then
            # Get every single ref (branch) by default
            refs = @client.refs(repo_name, "heads").map { |r| r[:ref].sub("refs/", "") }
        else
            # User-supplied branches
            refs = options[:branches].map { |branch| "heads/#{branch}" }
        end

        # Loop through each ref
        refs.each do |ref|
            puts "  #{ref}:".yellow

            # Grab the *whole* tree
            begin
                latest_commit = @client.ref(repo_name, ref).object
                root_tree     = @client.commit(repo_name, latest_commit.sha).commit.tree
                whole_tree    = @client.tree(repo_name, root_tree.sha, :recursive => true)
            rescue Octokit::NotFound
                puts "error: ref #{ref} not found.".red
                exit
            end

            # Warn the user about truncation
            if whole_tree[:truncated] then
                puts "    warning: tree truncated because number of items exceeded limit.".yellow
                puts "             If you feel like fixing this, see issue #xx".yellow
                puts "             http://github.com/ryanmjacobs/github-copywriter/xx".yellow
            end

            # Stores updated files until we commit
            # @modified_files is a hash {:path, :content}
            @modified_files = Array.new

            # Loop through all of whole_tree's blobs
            whole_tree[:tree].each do |file|
                next if file[:type] != "blob"

                if Copywriter::Regex.accepted_name?(file[:path]) then
                    # Grab file from repo
                    file = @client.contents(repo_name, :path => file[:path], :ref => ref)
                    if file[:type] != "file" then raise "error: #{file_path} on #{repo} is not a file!" end

                    # Update the copyright
                    new_content = Copywriter::Regex.update_copyright(cur_year, Base64.decode64(file[:content]))

                    # Skip to the next file if we didn't even find a copyright in the text
                    next if new_content[:copyrights_found] == 0

                    # Add to list of files to commit, only if the file has changed
                    if new_content[:copyrights_updated] > 0 then
                        @modified_files << {:path => file[:path], :content => new_content[:content]}
                        puts "    #{file[:path]} is now up-to-date.".green
                    else
                        puts "    #{file[:path]} is already up-to-date."
                    end
                end
            end

            # Commit stored up files
            if @modified_files.size > 0 then
                if @modified_files.size == 1 then
                    print "    Committing 1 file..."
                else
                    print "    Committing #{@modified_files.size} files..."
                end

                commit_files(repo_name, ref, "100644", @modified_files, COMMIT_MSG)

                puts " done! #{options[:dry_run] ? '(dry run)' : ''}"
            else
                puts "    No files needed to be commited."
            end
        end
    end
end