提交 3d3d8fbf 编写于 作者: T TommyLike

Update code to fetch all projects when checking sig yaml file

上级 e4786590
.idea/*
\ No newline at end of file
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"github.com/antihax/optional" "github.com/antihax/optional"
"os" "os"
"os/signal" "os/signal"
"strings" "strconv"
"sync" "sync"
"syscall" "syscall"
...@@ -91,6 +91,7 @@ func (gh *GiteeHandler) ValidateUser(wg *sync.WaitGroup, stopChannel <-chan stru ...@@ -91,6 +91,7 @@ func (gh *GiteeHandler) ValidateUser(wg *sync.WaitGroup, stopChannel <-chan stru
fmt.Printf("User channel finished, quiting..\n") fmt.Printf("User channel finished, quiting..\n")
return return
} else { } else {
fmt.Printf("Starting to validate user %s\n", u)
if !gh.checkUserExists(u) { if !gh.checkUserExists(u) {
*invalid = append(*invalid, u) *invalid = append(*invalid, u)
} }
...@@ -102,26 +103,6 @@ func (gh *GiteeHandler) ValidateUser(wg *sync.WaitGroup, stopChannel <-chan stru ...@@ -102,26 +103,6 @@ func (gh *GiteeHandler) ValidateUser(wg *sync.WaitGroup, stopChannel <-chan stru
} }
} }
func (gh *GiteeHandler) ValidateRepo(wg *sync.WaitGroup, stopChannel <-chan struct{}, rsChannel chan<- string, repos <-chan string, orgName string) {
defer wg.Done()
for {
select {
case u, ok := <- repos:
if !ok {
fmt.Printf("Repo channel finished, quiting..\n ")
return
} else {
if !gh.checkRepoExists(u, orgName) {
rsChannel <- u
}
}
case <-stopChannel:
fmt.Println("quit signal captured, quiting.")
return
}
}
}
func (gh *GiteeHandler) checkUserExists(name string) bool { func (gh *GiteeHandler) checkUserExists(name string) bool {
option := gitee.GetV5UsersUsernameOpts{ option := gitee.GetV5UsersUsernameOpts{
AccessToken: optional.NewString(gh.Token), AccessToken: optional.NewString(gh.Token),
...@@ -138,30 +119,47 @@ func (gh *GiteeHandler) checkUserExists(name string) bool { ...@@ -138,30 +119,47 @@ func (gh *GiteeHandler) checkUserExists(name string) bool {
return true return true
} }
func (gh *GiteeHandler) checkRepoExists(name, orgName string) bool { func (gh *GiteeHandler) CollectRepoPageCount(pageSize int, enterpriseName string) int {
option := gitee.GetV5SearchRepositoriesOpts{ option := gitee.GetV5EnterprisesEnterpriseReposOpts{
AccessToken: optional.NewString(gh.Token), AccessToken: optional.NewString(gh.Token),
Owner: optional.NewString(orgName), PerPage: optional.NewInt32(int32(pageSize)),
} }
projects, result, err := gh.GiteeClient.SearchApi.GetV5SearchRepositories(gh.Context, name, &option) _, result, err := gh.GiteeClient.RepositoriesApi.GetV5EnterprisesEnterpriseRepos(gh.Context, enterpriseName, &option)
if err != nil || result.StatusCode != 200 { if err != nil || result.StatusCode != 200 {
fmt.Printf("[Warning] Repo %s does not exists, or failed %v \n", name, err)
return false fmt.Printf("[Error] Can't collect projects in enterprise %s, %v \n", enterpriseName, err)
return -1
} }
//check total count size, ok := result.Header["Total_page"]
if len(projects) == 0 { if !ok {
fmt.Printf("[Warning] can't found project %s from gitee website\n", name) fmt.Printf("[Error] Can't collect 'Total_page' from Header %v", result.Header)
return false return -1
} }
sizeInt, err := strconv.ParseInt(size[0], 10, 0)
if err != nil {
fmt.Printf("[Error] Can't convert 'Total_page' to integer %v", size)
return -1
}
return int(sizeInt)
}
var projectNames []string func (gh *GiteeHandler) CollectRepos(wg *sync.WaitGroup, pageSize, totalPage, workerIndex, gap int, enterpriseName string, rsChannel chan<- string) {
for _, p := range projects { defer wg.Done()
if p.FullName == name { for i := workerIndex; i <= totalPage; i+=gap {
return true fmt.Printf("Starting to fetch project lists %d/%d from enterpise %s\n", i, totalPage, enterpriseName)
option := gitee.GetV5EnterprisesEnterpriseReposOpts{
AccessToken: optional.NewString(gh.Token),
PerPage: optional.NewInt32(int32(pageSize)),
Page: optional.NewInt32(int32(i)),
}
projects, result, err := gh.GiteeClient.RepositoriesApi.GetV5EnterprisesEnterpriseRepos(gh.Context, enterpriseName, &option)
if err != nil || result.StatusCode != 200 {
fmt.Printf("[Warning] Failed to get projects %d/%d from enterprise %s\n", i, totalPage, enterpriseName)
continue
}
for _,p := range projects {
rsChannel <- p.FullName
} }
projectNames = append(projectNames, p.FullName)
} }
fmt.Printf("[Warning] Unable to get repo %s information, actual search result %s \n", name, strings.Join(projectNames, ","))
return false
} }
...@@ -81,7 +81,7 @@ func CheckOwner() error { ...@@ -81,7 +81,7 @@ func CheckOwner() error {
return err return err
} }
if len(failedUser) != 0 { if len(failedUser) != 0 {
return fmt.Errorf("[Import] Failed to recognize gitee user: %s\n", strings.Join(failedUser,",")) return fmt.Errorf("[Import] Failed to recognize gitee user:\n %s\n", strings.Join(failedUser,"\n"))
} }
fmt.Printf("Owners successfully verified.") fmt.Printf("Owners successfully verified.")
return nil return nil
......
...@@ -59,6 +59,8 @@ func buildSigCommand() *cobra.Command { ...@@ -59,6 +59,8 @@ func buildSigCommand() *cobra.Command {
func CheckSigRepo() error { func CheckSigRepo() error {
var wg sync.WaitGroup var wg sync.WaitGroup
var totalProjects []string
var scanProjects []string
var invalidProjects []string var invalidProjects []string
fmt.Printf("Starting to validating all of the repos in sig file %s\n", sigRepoCheck.FileName) fmt.Printf("Starting to validating all of the repos in sig file %s\n", sigRepoCheck.FileName)
if _, err := os.Stat(sigRepoCheck.FileName); os.IsNotExist(err) { if _, err := os.Stat(sigRepoCheck.FileName); os.IsNotExist(err) {
...@@ -68,17 +70,28 @@ func CheckSigRepo() error { ...@@ -68,17 +70,28 @@ func CheckSigRepo() error {
// Setting up gitee handler // Setting up gitee handler
giteeHandler := NewGiteeHandler(sigRepoCheck.GiteeToken) giteeHandler := NewGiteeHandler(sigRepoCheck.GiteeToken)
sigChannel := make(chan string, 50) sigChannel := make(chan string, 50)
stopCh := SetupSignalHandler()
resultChannel := make(chan string, 50) resultChannel := make(chan string, 50)
// Running 5 workers to check the repo status
go func() { go func() {
for rs := range resultChannel { for rs := range resultChannel {
invalidProjects = append(invalidProjects, rs) totalProjects = append(totalProjects, rs)
} }
}() }()
for i := 0; i < 5; i++ {
go func() {
for rs := range sigChannel {
scanProjects = append(scanProjects, rs)
}
}()
// Running 5 workers to collect the projects status
size := giteeHandler.CollectRepoPageCount(100, "open_euler")
if size <= 0 {
return fmt.Errorf("can't get any projects in enterprise 'open_euler'")
}
for i := 1; i <= 5; i++ {
wg.Add(1) wg.Add(1)
go giteeHandler.ValidateRepo(&wg, stopCh, resultChannel, sigChannel, "open_euler") go giteeHandler.CollectRepos(&wg,100, size, i, 5 , "open_euler", resultChannel, )
} }
scanner := NewDirScanner("") scanner := NewDirScanner("")
...@@ -88,10 +101,25 @@ func CheckSigRepo() error { ...@@ -88,10 +101,25 @@ func CheckSigRepo() error {
if err != nil { if err != nil {
return err return err
} }
for _, scan := range scanProjects {
if !Find(totalProjects, scan) {
invalidProjects = append(invalidProjects, scan)
}
}
if len(invalidProjects) != 0 { if len(invalidProjects) != 0 {
return fmt.Errorf("[Import] Failed to recognize gitee projects: %s\n", strings.Join(invalidProjects,",")) return fmt.Errorf("[Import] Failed to recognize gitee %d projects:\n %s\n", len(invalidProjects), strings.Join(invalidProjects,"\n"))
} }
fmt.Printf("Projects successfully verified.") fmt.Printf("Projects successfully verified.")
return nil return nil
} }
func Find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
文件已添加
...@@ -760,13 +760,13 @@ type GetV5EnterprisesEnterpriseReposOpts struct { ...@@ -760,13 +760,13 @@ type GetV5EnterprisesEnterpriseReposOpts struct {
PerPage optional.Int32 PerPage optional.Int32
} }
func (a *RepositoriesApiService) GetV5EnterprisesEnterpriseRepos(ctx context.Context, enterprise string, localVarOptionals *GetV5EnterprisesEnterpriseReposOpts) (Project, *http.Response, error) { func (a *RepositoriesApiService) GetV5EnterprisesEnterpriseRepos(ctx context.Context, enterprise string, localVarOptionals *GetV5EnterprisesEnterpriseReposOpts) ([]Project, *http.Response, error) {
var ( var (
localVarHttpMethod = strings.ToUpper("Get") localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{} localVarPostBody interface{}
localVarFileName string localVarFileName string
localVarFileBytes []byte localVarFileBytes []byte
localVarReturnValue Project localVarReturnValue []Project
) )
// create path and map variables // create path and map variables
...@@ -840,7 +840,7 @@ func (a *RepositoriesApiService) GetV5EnterprisesEnterpriseRepos(ctx context.Con ...@@ -840,7 +840,7 @@ func (a *RepositoriesApiService) GetV5EnterprisesEnterpriseRepos(ctx context.Con
} }
if localVarHttpResponse.StatusCode == 200 { if localVarHttpResponse.StatusCode == 200 {
var v Project var v []Project
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")) err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil { if err != nil {
newErr.error = err.Error() newErr.error = err.Error()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册