Skip to content

Commit

Permalink
backup issues from github
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperspencer committed Nov 28, 2023
1 parent a7f72fc commit a9dcf5d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions conf.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ source:
username: your-user # user is used to clone the repo with
password: your-password
ssh: true # can be true or false
issues: true # back up issues
sshkey: /path/to/key # if empty, it uses your home directories' .ssh/id_rsa
exclude: # this excludes the repos "foo" and "bar"
- foo
Expand Down
30 changes: 30 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -256,6 +257,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
Hoster: "github.com",
Description: r.GetDescription(),
Private: r.GetPrivate(),
Issues: GetIssues(r, client, repo),
})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
Expand Down Expand Up @@ -287,11 +289,13 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
Hoster: "github.com",
Description: r.GetDescription(),
Private: r.GetPrivate(),
Issues: GetIssues(r, client, repo),
})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
repos = append(repos, wiki)
}

}
} else {
repos = append(repos, types.Repo{
Expand All @@ -305,6 +309,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
Hoster: "github.com",
Description: r.GetDescription(),
Private: r.GetPrivate(),
Issues: GetIssues(r, client, repo),
})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
Expand All @@ -318,6 +323,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
return repos, ran
}

// GetOrCreate Get or create a repository
func GetOrCreate(destination types.GenRepo, repo types.Repo) (string, error) {
sub = logger.CreateSubLogger("stage", "github", "url", "https://github.com")
token := destination.GetToken()
Expand Down Expand Up @@ -369,3 +375,27 @@ func GetOrCreate(destination types.GenRepo, repo types.Repo) (string, error) {

return *r.CloneURL, nil
}

// GetIssues get issues
func GetIssues(repo *github.Repository, client *github.Client, conf types.GenRepo) map[string]interface{} {
issues := map[string]interface{}{}
if conf.Issues {
listOptions := &github.IssueListByRepoOptions{State: "all", ListOptions: github.ListOptions{Page: 0, PerPage: 100}}
for {
i, _, err := client.Issues.ListByRepo(context.Background(), *repo.Owner.Login, *repo.Name, listOptions)
if err != nil {
sub.Error().Err(err).Str("repo", *repo.Name).Msg("can't fetch issues")
} else {
if len(i) > 0 {
for _, issue := range i {
issues[strconv.Itoa(*issue.Number)] = issue
}
} else {
break
}
listOptions.Page++
}
}
}
return issues
}
44 changes: 39 additions & 5 deletions local/local.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package local

import (
"encoding/json"
"fmt"
"math/rand"
"net"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -182,18 +184,50 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
}
}

if len(repo.Issues) > 0 {
if os.IsNotExist(err) && !dry {
if err := os.MkdirAll(fmt.Sprintf("%s.issues", repo.Name), 0o777); err != nil {
sub.Error().
Msg(err.Error())
}
}
sub.Info().Str("repo", repo.Name).Msg("backing up issues")
if !dry {
for k, v := range repo.Issues {
jsonData, err := json.Marshal(v)
if err != nil {
sub.Error().
Msg(err.Error())
} else {
err = os.WriteFile(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name), fmt.Sprintf("%s.json", k)), jsonData, 0644)
if err != nil {
sub.Error().
Msg(err.Error())
}
}
}
}
}

if l.Zip {
tozip := []string{repo.Name}

if len(repo.Issues) > 0 {
tozip = append(tozip, fmt.Sprintf("%s.issues", repo.Name))
}
sub.Info().
Msgf("zipping %s", types.Green(repo.Name))
err := archiver.Archive([]string{repo.Name}, fmt.Sprintf("%s.zip", repo.Name))
err := archiver.Archive(tozip, fmt.Sprintf("%s.zip", repo.Name))
if err != nil {
sub.Warn().
Str("repo", repo.Name).Msg(err.Error())
}
err = os.RemoveAll(repo.Name)
if err != nil {
sub.Warn().
Str("repo", repo.Name).Msg(err.Error())
for _, dir := range tozip {
err = os.RemoveAll(dir)
if err != nil {
sub.Warn().
Str("repo", repo.Name).Msg(err.Error())
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ type GenRepo struct {
ExcludeOrgs []string `yaml:"excludeorgs"`
Include []string `yaml:"include"`
IncludeOrgs []string `yaml:"includeorgs"`
Issues bool `yaml:"issues"`
Wiki bool `yaml:"wiki"`
Starred bool `yaml:"starred"`
CreateOrg bool `yaml:"createorg"`
Expand Down Expand Up @@ -384,6 +385,7 @@ type Repo struct {
Owner string
Hoster string
Description string
Issues map[string]interface{}
Private bool
}

Expand Down

0 comments on commit a9dcf5d

Please sign in to comment.