func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repository, err error) { endpoint := os.Getenv("REGISTRY_URL") username, password, ok := ra.Ctx.Request.BasicAuth() if ok { return newRepositoryClient(endpoint, api.GetIsInsecure(), username, password, repoName, "repository", repoName, "pull", "push", "*") } username, err = ra.getUsername() if err != nil { return nil, err } return cache.NewRepositoryClient(endpoint, api.GetIsInsecure(), username, repoName, "repository", repoName, "pull", "push", "*") }
func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string, error) { var needsAdd []string var needsDel []string sort.Strings(reposInRegistry) sort.Strings(reposInDB) i, j := 0, 0 repoInR, repoInD := "", "" for i < len(reposInRegistry) && j < len(reposInDB) { repoInR = reposInRegistry[i] repoInD = reposInDB[j] d := strings.Compare(repoInR, repoInD) if d < 0 { i++ exist, err := projectExists(repoInR) if err != nil { log.Errorf("failed to check the existence of project %s: %v", repoInR, err) continue } if !exist { continue } // TODO remove the workaround when the bug of registry is fixed // TODO read it from config endpoint := os.Getenv("REGISTRY_URL") client, err := cache.NewRepositoryClient(endpoint, true, "admin", repoInR, "repository", repoInR) if err != nil { return needsAdd, needsDel, err } exist, err = repositoryExist(repoInR, client) if err != nil { return needsAdd, needsDel, err } if !exist { continue } needsAdd = append(needsAdd, repoInR) } else if d > 0 { needsDel = append(needsDel, repoInD) j++ } else { // TODO remove the workaround when the bug of registry is fixed // TODO read it from config endpoint := os.Getenv("REGISTRY_URL") client, err := cache.NewRepositoryClient(endpoint, true, "admin", repoInR, "repository", repoInR) if err != nil { return needsAdd, needsDel, err } exist, err := repositoryExist(repoInR, client) if err != nil { return needsAdd, needsDel, err } if !exist { needsDel = append(needsDel, repoInD) } i++ j++ } } for i < len(reposInRegistry) { repoInR = reposInRegistry[i] i++ exist, err := projectExists(repoInR) if err != nil { log.Errorf("failed to check whether project of %s exists: %v", repoInR, err) continue } if !exist { continue } needsAdd = append(needsAdd, repoInR) } for j < len(reposInDB) { needsDel = append(needsDel, reposInDB[j]) j++ } return needsAdd, needsDel, nil }