// Copyright (c) 2023 Open Anolis Community Distro SIG, all rights reserved. // // Author: Jacob Wang // Xiao Lun // Zhao Hang package modes import ( "bytes" "fmt" "net/http" "strings" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/storage/memory" "pkgcrew/pkg/data" ) const ( GiteePrefix = "https://gitee.com" ) func CreateOrgRepo(pd *data.ProcessData) error { client := &http.Client{} url := fmt.Sprintf("https://gitee.com/api/v5/orgs/%s/repos",pd.GroupRpms) reqBody := []byte(fmt.Sprintf( ` { "access_token":"%s", "name":"%s", "has_issues":"true", "has_wiki":"true", "can_comment":"true", "public":1 } `,pd.HttpToken,pd.Package)) req, _ := http.NewRequest("POST", url, bytes.NewBuffer(reqBody)) req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { return fmt.Errorf("create repo error: %v", err) } if resp.StatusCode == 201 { pd.Log.Printf("create repo success: %v", resp.Status) } else { return fmt.Errorf("create repo error: %v", resp.Status) } return nil } func CheckRepoExist(pd *data.ProcessData) error { repo, _ := git.Init(memory.NewStorage(),memfs.New()) remoteUrl := fmt.Sprintf("%s/%s/%s.git", pd.DownstreamPrefix, pd.GroupRpms, pd.Package) refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*") _, err := repo.CreateRemote(&config.RemoteConfig{ Name: "origin", URLs: []string{remoteUrl}, Fetch: []config.RefSpec{refspec}, }) err = repo.Fetch(&git.FetchOptions{ RemoteName: "origin", RefSpecs: []config.RefSpec{refspec}, Auth: pd.Authenticator, }) if fmt.Sprintf("%s",err) == "repository not found" { pd.Log.Printf("could not get dist Worktree: %v", err) // Only create gitee code repo, gitlab can create repo automatically if strings.HasPrefix(pd.DownstreamPrefix, GiteePrefix) { CreateOrgRepo(pd) } else { pd.Log.Printf("DownstreamPrefix is not gitee url, skip Create-Organization-Repository step.") } } return nil }