Skip to content

Commit

Permalink
Chores (#2)
Browse files Browse the repository at this point in the history
* Chore: add readme and license

* Refactor: move CurrentUser into repo package

* Refactor: utils into repo
  • Loading branch information
till authored Jun 28, 2020
1 parent f283560 commit 10dde97
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 38 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) 2020, Planetary Quantum GmbH

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# github-org-sync

A tool to sync files from a skeleton/template repository to other repositories in your organization.

Inspiration from:
- https://github.com/cloudalchemy/auto-maintenance
- https://github.com/prometheus/prometheus/blob/master/scripts/sync_repo_files.sh

# Status

Currently, this repository contains a little tool written in Golang to do the syncing. The goal is to offer a GitHub Action as well.

# Configuration

See [.envrc-dist](.envrc-dist) for necessary configuration.

# License

BSD-2-Clause ("Simplified BSD License")
11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import (
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"

"github.com/hostwithquantum/github-org-sync-action/utils"

"github.com/hostwithquantum/github-org-sync-action/repo"
"github.com/hostwithquantum/github-org-sync-action/user"
)

func init() {
Expand All @@ -30,7 +27,7 @@ func main() {
log.Info(fmt.Sprintf("From: %s", skeletonRepository))

// init CurrentUser (for auth)
currentUser := user.CurrentUser{
currentUser := repo.CurrentUser{
Email: os.Getenv("GITHUB_EMAIL"),
Name: os.Getenv("GITHUB_USER"),
Token: os.Getenv("GITHUB_ACCESS_TOKEN"),
Expand All @@ -47,7 +44,7 @@ func main() {
// clone skeleton repository
skeleton := repo.NewRepo(org, skeletonRepository, currentUser, tmpDirectory)

skeletonGithub := utils.GithubLink(org, skeletonRepository)
skeletonGithub := repo.GithubLink(org, skeletonRepository)

log.Info("Cloned skeleton")

Expand All @@ -56,7 +53,7 @@ func main() {
for _, repository := range repositories {

target := repo.NewRepo(org, repository, currentUser, tmpDirectory)
githubLink := utils.GithubLink(org, repository)
githubLink := repo.GithubLink(org, repository)
log.Infof("Cloned: '%s'", githubLink)

defaultBranch := target.GetDefaultBranch()
Expand Down Expand Up @@ -96,5 +93,5 @@ func main() {
}

err := os.RemoveAll(tmpDirectory)
utils.CheckIfError(err)
repo.CheckIfError(err)
}
8 changes: 3 additions & 5 deletions repo/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import (
"fmt"

"github.com/google/go-github/github"
"github.com/hostwithquantum/github-org-sync-action/user"
"github.com/hostwithquantum/github-org-sync-action/utils"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)

// Github ...
type Github struct {
user user.CurrentUser
user CurrentUser
org string
context context.Context
client *github.Client
}

// NewGithub ...
func NewGithub(user user.CurrentUser, org string) Github {
func NewGithub(user CurrentUser, org string) Github {
// init context
ctx := context.Background()

Expand All @@ -47,7 +45,7 @@ func (g Github) CreatePullRequest(repository string, pr *github.NewPullRequest)
}

// handle other errors
utils.CheckIfError(err)
CheckIfError(err)

log.Info(fmt.Sprintf("PR created: %s", pullRequest.GetHTMLURL()))
}
Expand Down
4 changes: 1 addition & 3 deletions repo/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"path/filepath"

log "github.com/sirupsen/logrus"

"github.com/hostwithquantum/github-org-sync-action/utils"
)

// Handler ...
Expand All @@ -31,7 +29,7 @@ func (h Handler) Sync(target string) {
"%s/.github/workflows/%s", target, filepath.Base(file))

err := copy(file, repoFile)
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Synced to '%s' to '%s'", file, repoFile)
}
Expand Down
38 changes: 17 additions & 21 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

log "github.com/sirupsen/logrus"

"github.com/hostwithquantum/github-org-sync-action/user"
"github.com/hostwithquantum/github-org-sync-action/utils"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
Expand All @@ -21,13 +18,13 @@ import (
type Repo struct {
org string
name string
user user.CurrentUser
user CurrentUser
target string
repo *git.Repository
}

// NewRepo ...
func NewRepo(org string, name string, user user.CurrentUser, tmpDirectory string) Repo {
func NewRepo(org string, name string, user CurrentUser, tmpDirectory string) Repo {

cloneDirectory := fmt.Sprintf("%s/%s", tmpDirectory, name)

Expand All @@ -44,10 +41,10 @@ func (r Repo) CommitAndPush(message string, branch string) {
branchRef := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch))

worktree, err := r.repo.Worktree()
utils.CheckIfError(err)
CheckIfError(err)

status, err := worktree.Status()
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Current status is: %v", status.IsClean())
if status.IsClean() {
Expand All @@ -57,7 +54,7 @@ func (r Repo) CommitAndPush(message string, branch string) {

for file := range status {
hash, err := worktree.Add(file)
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Staged file: %s", file)

Expand All @@ -72,7 +69,7 @@ func (r Repo) CommitAndPush(message string, branch string) {
When: time.Now(),
},
})
utils.CheckIfError(err)
CheckIfError(err)
log.Debugf("Created commit: %s", commit.String())

err = r.repo.Push(&git.PushOptions{
Expand All @@ -83,48 +80,47 @@ func (r Repo) CommitAndPush(message string, branch string) {
config.RefSpec(branchRef + ":" + branchRef),
},
})
utils.CheckIfError(err)
CheckIfError(err)
log.Debugf("Pushed branch: %s!", branchRef)
}

// CreateBranch ...
func (r Repo) CreateBranch(branch string) {
worktree, err := r.repo.Worktree()
utils.CheckIfError(err)
CheckIfError(err)

err = worktree.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: true,
})
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Created new branch and switched workspace")
}

// GetDefaultBranch ...
func (r Repo) GetDefaultBranch() string {
ref, err := r.repo.Head()
utils.CheckIfError(err)
CheckIfError(err)

return ref.Name().Short()
}

// GetLastCommit ...
func (r Repo) GetLastCommit() plumbing.Hash {
headRef, err := r.repo.Head()
utils.CheckIfError(err)
log.Debugf("Last commit in '%s' is: %s",
utils.GithubLink(r.org, r.name), headRef.Hash())
CheckIfError(err)
log.Debugf("Last commit in '%s' is: %s", GithubLink(r.org, r.name), headRef.Hash())
return headRef.Hash()
}

// NeedsCommit ...
func (r Repo) NeedsCommit() bool {
worktree, err := r.repo.Worktree()
utils.CheckIfError(err)
CheckIfError(err)

status, err := worktree.Status()
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Current status is: %v", status.IsClean())

Expand All @@ -147,12 +143,12 @@ func clone(org string, name string, target string, token string) *git.Repository
URL: createGithubURL(org, name),
Progress: os.Stdout,
})
utils.CheckIfError(err)
CheckIfError(err)

log.Debugf("Cloned repository '%s' to '%s'", utils.GithubLink(org, name), target)
log.Debugf("Cloned repository '%s' to '%s'", GithubLink(org, name), target)
return repo
}

func createGithubURL(org string, name string) string {
return fmt.Sprintf("https://github.com/%s", utils.GithubLink(org, name))
return fmt.Sprintf("https://github.com/%s", GithubLink(org, name))
}
2 changes: 1 addition & 1 deletion user/user.go → repo/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package user
package repo

// CurrentUser ...
type CurrentUser struct {
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go → repo/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package repo

import (
"fmt"
Expand Down

0 comments on commit 10dde97

Please sign in to comment.