Skip to content

Instantly share code, notes, and snippets.

@skvl
Forked from harlantwood/push_to_github.rb
Created June 27, 2019 06:27
Show Gist options
  • Select an option

  • Save skvl/717898c02c4581e53536d35a9fec5ef0 to your computer and use it in GitHub Desktop.

Select an option

Save skvl/717898c02c4581e53536d35a9fec5ef0 to your computer and use it in GitHub Desktop.

Revisions

  1. @harlantwood harlantwood revised this gist Jun 17, 2012. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,15 @@
    # Committing changes to a repo via the Github API is not entirely trivial.
    #
    # The five-step process is outlined here:
    # http://developer.github.com/v3/git/
    #
    # And Matt Swanson wrote a blog post translating this into actual API calls:
    # Matt Swanson wrote a blog post translating the above steps into actual API calls:
    # http://swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
    #
    # I was not able to find sample code for actually doing this in Ruby,
    # either via the HTTP API or any of the gems that wrap the API.
    #
    # So in the hopes it will help others, here is a simple function to
    # commit a single file to github via the API. The code only handles
    # the simple case of committing a single file to the master branch,
    # the case of committing a single file to the master branch,
    # but should be easy to modify for your own needs.
    #
    # Note also that we use HTTP basic auth, not OAuth, in this example.
  2. @harlantwood harlantwood revised this gist Jun 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ def push_to_github(params)
    branch = github(:get, repo, "refs/heads/master")
    last_commit_sha = branch['object']['sha']

    # create the last commit
    # get the last commit
    # see http://developer.github.com/v3/git/commits/
    last_commit = github :get, repo, "commits/#{last_commit_sha}"
    last_tree_sha = last_commit['tree']['sha']
  3. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -16,13 +16,14 @@
    #
    # Note also that we use HTTP basic auth, not OAuth, in this example.
    #
    # Usage:
    # Prerequisites:
    #
    # $ gem install rest-client
    #
    # $ export GH_USERNAME=xxxx
    # $ export GH_PASSWORD=yyyy
    #
    # Usage:
    #
    # push_to_github :path => "path/to/file.txt", :content => "hello commit", :repo => 'test'
    #
    # In the above example, the repo 'test' must be owned by GH_USERNAME, and have at least one commit.
  4. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ def push_to_github(params)

    # get the head of the master branch
    # see http://developer.github.com/v3/git/refs/
    branch = github :get, repo, "refs/heads/master"
    branch = github(:get, repo, "refs/heads/master")
    last_commit_sha = branch['object']['sha']

    # create the last commit
    @@ -62,21 +62,23 @@ def push_to_github(params)
    # create commit
    # see http://developer.github.com/v3/git/commits/
    new_commit = github :post, repo, :commits,
    :parents => [last_commit],
    :parents => [last_commit_sha],
    :tree => new_content_tree_sha,
    :message => 'commit via api'
    new_commit_sha = new_commit['sha']

    # update branch to point to new commit
    # see http://developer.github.com/v3/git/refs/
    github :patch, repo, "/refs/heads/master",
    github :patch, repo, "refs/heads/master",
    :sha => new_commit_sha
    end

    def github(method, repo, resource, params={})
    JSON.parse RestClient.send(method,
    "https://#{ENV['GITHUB_USER']}:#{ENV['GITHUB_PASS']}@api.github.com" +
    "/repos/#{ENV['GITHUB_USER']}/#{repo}/git/#{resource}",
    params.to_json, :content_type => :json, :accept => :json
    )
    end
    resource_url = "https://#{ENV['GITHUB_USER']}:#{ENV['GITHUB_PASS']}@api.github.com" +
    "/repos/#{ENV['GITHUB_USER']}/#{repo}/git/#{resource}"
    if params.empty?
    JSON.parse RestClient.send(method, resource_url)
    else
    JSON.parse RestClient.send(method, resource_url, params.to_json, :content_type => :json, :accept => :json)
    end
    end
  5. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Committing changes to a repo via the Github API is not so simple.
    # Committing changes to a repo via the Github API is not entirely trivial.
    #
    # The five-step process is outlined here:
    # http://developer.github.com/v3/git/
    @@ -13,6 +13,8 @@
    # commit a single file to github via the API. The code only handles
    # the simple case of committing a single file to the master branch,
    # but should be easy to modify for your own needs.
    #
    # Note also that we use HTTP basic auth, not OAuth, in this example.
    #
    # Usage:
    #
    @@ -39,9 +41,14 @@

    def push_to_github(params)
    repo = params[:repo]

    # get the head of the master branch
    # see http://developer.github.com/v3/git/refs/
    branch = github :get, repo, "refs/heads/master"
    last_commit_sha = branch['object']['sha']

    # create the last commit
    # see http://developer.github.com/v3/git/commits/
    last_commit = github :get, repo, "commits/#{last_commit_sha}"
    last_tree_sha = last_commit['tree']['sha']

  6. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 20 additions and 8 deletions.
    28 changes: 20 additions & 8 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,32 @@
    # gem install rest-client
    # Committing changes to a repo via the Github API is not so simple.
    #
    # export GH_USERNAME=xxxx
    # export GH_PASSWORD=yyyy
    # The five-step process is outlined here:
    # http://developer.github.com/v3/git/
    #
    # And Matt Swanson wrote a blog post translating this into actual API calls:
    # http://swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
    #
    # I was not able to find sample code for actually doing this in Ruby,
    # either via the HTTP API or any of the gems that wrap the API.
    #
    # So in the hopes it will help others, here is a simple function to
    # commit a single file to github via the API. The code only handles
    # the simple case of committing a single file to the master branch,
    # but should be easy to modify for your own needs.
    #
    # Usage:
    #
    # $ gem install rest-client
    #
    # $ export GH_USERNAME=xxxx
    # $ export GH_PASSWORD=yyyy
    #
    # push_to_github :path => "path/to/file.txt", :content => "hello commit", :repo => 'test'
    #
    # In the above example, the repo 'test' must be owned by GH_USERNAME, and have at least one commit.
    #
    # No news is good news. If you don't raise exceptions, you should see the commit in your repo.
    #
    # Inspirations:
    # - http://swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
    # - http://developer.github.com/v3/git/
    # - http://news.ycombinator.com/item?id=2746877
    #
    # CC0 Public Domain Dedication
    # To the extent possible under law, Harlan T Wood
    # has waived all copyright and related or neighboring
  7. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,11 @@
    #
    # No news is good news. If you don't raise exceptions, you should see the commit in your repo.
    #
    # Inspirations:
    # - http://swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
    # - http://developer.github.com/v3/git/
    # - http://news.ycombinator.com/item?id=2746877
    #
    # CC0 Public Domain Dedication
    # To the extent possible under law, Harlan T Wood
    # has waived all copyright and related or neighboring
  8. @harlantwood harlantwood revised this gist Jun 15, 2012. 2 changed files with 7 additions and 12 deletions.
    11 changes: 0 additions & 11 deletions LICENSE_CC0.html
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    <p xmlns:dct="http://purl.org/dc/terms/">
    <a rel="license"
    href="http://creativecommons.org/publicdomain/zero/1.0/">
    <img src="http://i.creativecommons.org/p/zero/1.0/88x31.png" style="border-style: none;" alt="CC0" />
    </a>
    <br />
    To the extent possible under law,
    <span rel="dct:publisher" resource="[_:publisher]">the person who associated CC0</span>
    with this work has waived all copyright and related or neighboring
    rights to this work.
    </p>
    8 changes: 7 additions & 1 deletion push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,12 @@
    #
    # No news is good news. If you don't raise exceptions, you should see the commit in your repo.
    #
    # CC0 Public Domain Dedication
    # To the extent possible under law, Harlan T Wood
    # has waived all copyright and related or neighboring
    # rights to this work.
    # http://creativecommons.org/publicdomain/zero/1.0/
    #

    require 'rest_client'
    require 'json'
    @@ -49,4 +55,4 @@ def github(method, repo, resource, params={})
    "/repos/#{ENV['GITHUB_USER']}/#{repo}/git/#{resource}",
    params.to_json, :content_type => :json, :accept => :json
    )
    end
    end
  9. @harlantwood harlantwood revised this gist Jun 15, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions LICENSE_CC0.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <p xmlns:dct="http://purl.org/dc/terms/">
    <a rel="license"
    href="http://creativecommons.org/publicdomain/zero/1.0/">
    <img src="http://i.creativecommons.org/p/zero/1.0/88x31.png" style="border-style: none;" alt="CC0" />
    </a>
    <br />
    To the extent possible under law,
    <span rel="dct:publisher" resource="[_:publisher]">the person who associated CC0</span>
    with this work has waived all copyright and related or neighboring
    rights to this work.
    </p>
  10. @harlantwood harlantwood created this gist Jun 15, 2012.
    52 changes: 52 additions & 0 deletions push_to_github.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    # gem install rest-client
    #
    # export GH_USERNAME=xxxx
    # export GH_PASSWORD=yyyy
    #
    # Usage:
    # push_to_github :path => "path/to/file.txt", :content => "hello commit", :repo => 'test'
    #
    # In the above example, the repo 'test' must be owned by GH_USERNAME, and have at least one commit.
    #
    # No news is good news. If you don't raise exceptions, you should see the commit in your repo.
    #

    require 'rest_client'
    require 'json'

    def push_to_github(params)
    repo = params[:repo]
    branch = github :get, repo, "refs/heads/master"
    last_commit_sha = branch['object']['sha']

    last_commit = github :get, repo, "commits/#{last_commit_sha}"
    last_tree_sha = last_commit['tree']['sha']

    # create tree object (also implicitly creates a blob based on content)
    # see http://developer.github.com/v3/git/trees/
    new_content_tree = github :post, repo, :trees,
    :base_tree => last_tree_sha,
    :tree => [{:path => params[:path], :content => params[:content], :mode => '100644'}]
    new_content_tree_sha = new_content_tree['sha']

    # create commit
    # see http://developer.github.com/v3/git/commits/
    new_commit = github :post, repo, :commits,
    :parents => [last_commit],
    :tree => new_content_tree_sha,
    :message => 'commit via api'
    new_commit_sha = new_commit['sha']

    # update branch to point to new commit
    # see http://developer.github.com/v3/git/refs/
    github :patch, repo, "/refs/heads/master",
    :sha => new_commit_sha
    end

    def github(method, repo, resource, params={})
    JSON.parse RestClient.send(method,
    "https://#{ENV['GITHUB_USER']}:#{ENV['GITHUB_PASS']}@api.github.com" +
    "/repos/#{ENV['GITHUB_USER']}/#{repo}/git/#{resource}",
    params.to_json, :content_type => :json, :accept => :json
    )
    end