Last active
March 29, 2018 22:39
-
-
Save aerostitch/3e721080dc13ee166f44d329f8a93582 to your computer and use it in GitHub Desktop.
Usage example of creating a repo and pushing several files inside a single commit (for just 1 file, the contents api is easier to use)
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
package main | |
import ( | |
"context" | |
"errors" | |
"time" | |
"github.com/google/go-github/github" | |
"golang.org/x/oauth2" | |
) | |
// getGitHubClient generates a new github client using the given context | |
func getGitHubClient(ctx context.Context) (client *github.Client, err error) { | |
token, err := os.Getenv("GITHUB_AUTH_TOKEN") | |
if err != nil { | |
return | |
} | |
if token == "" { | |
return nil, errors.New("Empty github token. Please verify your AWS_PROFILE variable") | |
} | |
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) | |
tc := oauth2.NewClient(ctx, ts) | |
client = github.NewClient(tc) | |
return | |
} | |
// createGithubRepo creates a new github repository | |
func createGithubRepo(repoOwner, repoName, description string, tags []string, private bool) error { | |
ctx := context.Background() | |
client, err := getGitHubClient(ctx) | |
if err != nil { | |
return err | |
} | |
disable := false | |
enable := true | |
r := &github.Repository{Name: &repoName, Private: &private, Description: &description, HasWiki: &disable, HasProjects: &disable, HasPages: &disable, AutoInit: &enable} | |
_, _, err = client.Repositories.Create(ctx, repoOwner, r) | |
if err != nil { | |
return err | |
} | |
_, _, err = client.Repositories.ReplaceAllTopics(ctx, repoOwner, repoName, tags) | |
return err | |
} | |
// pushGithubRepo generates a new commit with the content of the templates | |
// prepared for the repository | |
func pushGithubRepo(repoOwner, repoName, commitMsg string, data map[string][]byte) error { | |
ctx := context.Background() | |
client, err := getGitHubClient(ctx) | |
if err != nil { | |
return err | |
} | |
// Get the ref object for the master branch | |
ref, _, err := client.Git.GetRef(ctx, repoOwner, repoName, "refs/heads/master") | |
if err != nil { | |
return err | |
} | |
// Create a tree with what to commit | |
authName := "my-login" | |
authEmail := "[email protected]" | |
date := time.Now() | |
author := &github.CommitAuthor{Date: &date, Name: &authName, Email: &authEmail} | |
entries := []github.TreeEntry{} | |
for file, content := range data { | |
entries = append(entries, github.TreeEntry{Path: github.String(file), Type: github.String("blob"), Content: github.String(string(content)), Mode: github.String("100644")}) | |
} | |
tree, _, err := client.Git.CreateTree(ctx, repoOwner, repoName, *ref.Object.SHA, entries) | |
if err != nil { | |
return err | |
} | |
// Get the parent commit to attach the commit to | |
parent, _, err := client.Repositories.GetCommit(ctx, repoOwner, repoName, *ref.Object.SHA) | |
if err != nil { | |
return err | |
} | |
parent.Commit.SHA = parent.SHA | |
// Create the commit in the tree | |
commit := &github.Commit{Author: author, Message: github.String(commitMsg), Tree: tree, Parents: []github.Commit{*parent.Commit}} | |
newCommit, _, err := client.Git.CreateCommit(ctx, repoOwner, repoName, commit) | |
if err != nil { | |
return err | |
} | |
// Attach the commit to the master branch | |
ref.Object.SHA = newCommit.SHA | |
_, _, err = client.Git.UpdateRef(ctx, repoOwner, repoName, ref, false) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment