1
1
mirror of https://github.com/cooperspencer/gickup synced 2024-10-18 07:38:08 +02:00

added mirror to bitbucket

This commit is contained in:
Andreas Wachter 2024-04-19 13:33:15 +02:00
parent 0e76cf9481
commit 75da4f5090
4 changed files with 135 additions and 6 deletions

@ -74,7 +74,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
if include[r.Name] {
repos = append(repos, types.Repo{
Name: r.Name,
URL: r.Links["clone"].([]interface{})[0].(map[string]interface{})["href"].(string),
URL: r.Links["html"].(map[string]interface{})["href"].(string),
SSHURL: r.Links["clone"].([]interface{})[1].(map[string]interface{})["href"].(string),
Token: "",
Defaultbranch: r.Mainbranch.Name,
@ -95,7 +95,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
if len(include) == 0 {
repos = append(repos, types.Repo{
Name: r.Name,
URL: r.Links["clone"].([]interface{})[0].(map[string]interface{})["href"].(string),
URL: r.Links["html"].(map[string]interface{})["href"].(string),
SSHURL: r.Links["clone"].([]interface{})[1].(map[string]interface{})["href"].(string),
Token: "",
Defaultbranch: r.Mainbranch.Name,
@ -111,3 +111,36 @@ 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) {
client := bitbucket.NewBasicAuth(destination.Username, destination.Password)
if destination.User == "" {
destination.User = destination.Username
}
if destination.URL == "" {
destination.URL = bitbucket.DEFAULT_BITBUCKET_API_BASE_URL
} else {
if destination.URL != "https://bitbucket.org/" {
bitbucketURL, err := url.Parse(destination.URL)
if err != nil {
return "", err
}
client.SetApiBaseURL(*bitbucketURL)
}
}
sub = logger.CreateSubLogger("stage", "bitbucket", "url", destination.URL)
r, err := client.Repositories.Repository.Get(&bitbucket.RepositoryOptions{Owner: destination.User, RepoSlug: repo.Name})
if err != nil {
r, err = client.Repositories.Repository.Create(&bitbucket.RepositoryOptions{Owner: destination.User, RepoSlug: repo.Name, Description: repo.Description, Scm: "git"})
if err != nil {
return "", err
}
}
return r.Links["html"].(map[string]interface{})["href"].(string), nil
}

@ -486,6 +486,13 @@ func TempClone(repo types.Repo, tempdir string) (*git.Repository, error) {
Username: "xyz",
Password: repo.Token,
}
} else {
if repo.Origin.Username != "" && repo.Origin.Password != "" {
auth = &http.BasicAuth{
Username: repo.Origin.Username,
Password: repo.Origin.Password,
}
}
}
if repo.Origin.LFS {
g, err := gitcmd.New()
@ -612,9 +619,18 @@ func CreateRemotePush(repo *git.Repository, destination types.GenRepo, url strin
return err
}
} else {
auth = &http.BasicAuth{
Username: "xyz",
Password: token,
if token != "" {
auth = &http.BasicAuth{
Username: "xyz",
Password: token,
}
} else {
if destination.Username != "" && destination.Password != "" {
auth = &http.BasicAuth{
Username: destination.Username,
Password: destination.Password,
}
}
}
}
if lfs {

78
main.go

@ -678,6 +678,84 @@ func backup(repos []types.Repo, conf *types.Conf) {
}
}
for _, d := range conf.Destination.BitBucket {
if !strings.HasSuffix(r.Name, ".wiki") {
repotime := time.Now()
status := 0
if d.URL == "" {
d.URL = "https://bitbucket.org/"
}
log.Info().
Str("stage", "bitbucket").
Str("url", d.URL).
Msgf("mirroring %s to %s", types.Blue(r.Name), d.URL)
if !cli.Dry {
tempdir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("bitbucket-%x", repotime))
if err != nil {
log.Error().
Str("stage", "tempclone").
Str("url", r.URL).
Msg(err.Error())
continue
}
defer os.RemoveAll(tempdir)
temprepo, err := local.TempClone(r, tempdir)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Info().
Str("stage", "bitbucket").
Str("url", r.URL).
Msg(err.Error())
} else {
log.Error().
Str("stage", "tempclone").
Str("url", r.URL).
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}
}
cloneurl, err := bitbucket.GetOrCreate(d, r)
if err != nil {
log.Error().
Str("stage", "bitbucket").
Str("url", r.URL).
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}
err = local.CreateRemotePush(temprepo, d, cloneurl, r.Origin.LFS)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Info().
Str("stage", "bitbucket").
Str("url", r.URL).
Msg(err.Error())
} else {
log.Error().
Str("stage", "bitbucket").
Str("url", r.URL).
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}
}
prometheus.RepoTime.WithLabelValues(r.Hoster, r.Name, r.Owner, "bitbucket", d.URL).Set(time.Since(repotime).Seconds())
status = 1
prometheus.RepoSuccess.WithLabelValues(r.Hoster, r.Name, r.Owner, "bitbucket", d.URL).Set(float64(status))
prometheus.DestinationBackupsComplete.WithLabelValues("bitbucket").Inc()
os.RemoveAll(tempdir)
}
}
}
prometheus.SourceBackupsComplete.WithLabelValues(r.Name).Inc()
}
}

@ -29,6 +29,7 @@ type Destination struct {
Gogs []GenRepo `yaml:"gogs"`
OneDev []GenRepo `yaml:"onedev"`
Sourcehut []GenRepo `yaml:"sourcehut"`
BitBucket []GenRepo `yaml:"bitbucket"`
}
// Count TODO.
@ -39,7 +40,8 @@ func (dest Destination) Count() int {
len(dest.Github) +
len(dest.Gitlab) +
len(dest.OneDev) +
len(dest.Sourcehut)
len(dest.Sourcehut) +
len(dest.BitBucket)
}
// Local TODO.