Example #1
0
func main() {
	flag.Parse()
	splitTarget := strings.Split(*remoteRepository, "/")
	if len(splitTarget) != 2 {
		usage("Target repository is required, in the format `user/repo'")
	}
	userName := splitTarget[0]
	repoName := splitTarget[1]

	localDirInfo, err := os.Stat(*localRepositoryDir)
	if err != nil {
		log.Fatal(err)
	}
	if !localDirInfo.IsDir() {
		usage("Local repository must be a directory")
	}

	local, err := repository.NewGitRepo(*localRepositoryDir)
	if err != nil {
		log.Fatal("Couldn't open local repository: ", err.Error(), "\n",
			"Make sure you clone the remote repository locally first!")
	}

	tokenAuth := *token != ""
	if !tokenAuth {
		fmt.Fprintln(os.Stderr, "Not using authentication. Note that this will be EXTREMELY SLOW;")
		fmt.Fprintln(os.Stderr, "you get 60 requests to the github API per hour.")
		fmt.Fprint(os.Stderr, auth.TokenHelp)
	}

	var client *github.Client
	if tokenAuth {
		client = auth.TokenClient(*token)
	} else {
		client = auth.UnauthenticatedClient()
	}

	_, _, err = client.Repositories.Get(userName, repoName)
	if err != nil {
		log.Fatal("Error fetching repository info: ", err.Error())
	}

	errOutput := make(chan error, 1000)
	nErrors := 0
	go func() {
		for err := range errOutput {
			if !*quiet {
				log.Println(err)
			}
			nErrors++
		}
	}()
	statuses, err := mirror.GetAllStatuses(userName, repoName, client, errOutput)
	if err != nil {
		log.Fatal("Error reading statuses: ", err.Error())
	}
	reviews, err := mirror.GetAllPullRequests(local, userName, repoName, client, errOutput)
	if err != nil {
		log.Fatal("Error reading pull requests: ", err.Error())
	}
	close(errOutput)

	nStatuses := len(statuses)
	nReviews := len(reviews)
	var l *log.Logger
	if *quiet {
		l = log.New(ioutil.Discard, "", 0)
	} else {
		l = log.New(os.Stdout, "", 0)
	}
	logChan := make(chan string, 1000)
	go func() {
		for msg := range logChan {
			l.Println(msg)
		}
	}()

	l.Printf("Done reading! Read %d statuses, %d PRs", nStatuses, nReviews)
	l.Printf("Committing...\n")
	if err := mirror.WriteNewReports(statuses, local, logChan); err != nil {
		log.Fatal(err)
	}
	if err := mirror.WriteNewReviews(reviews, local, logChan); err != nil {
		log.Fatal(err)
	}
	close(logChan)

	l.Printf("Done! Hit %d errors", nErrors)
	if nErrors > 0 {
		os.Exit(1)
	}
}
// initialize performs initial reading and commiting for the repository
func initialize(userName, repoName string) {
	c, done := context.WithCancel(appengine.BackgroundContext())
	defer done()

	errorf := makeErrorf(c, userName, repoName)
	repoData, err := getRepoData(c, userName, repoName)
	if err != nil {
		errorf("Can't load repo to initialize: %s", err.Error())
		return
	}

	repo, err := clone(c, userName, repoName, userName, repoData.Token)
	if err != nil {
		errorf("Can't clone repo: %s", err.Error())
		return
	}

	client := github.NewClient(oauth2.NewClient(c, oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: repoData.Token},
	)))

	errChan := make(chan error, 1000)
	nErrors := 0
	go func() {
		for err := range errChan {
			errorf(err.Error())
			nErrors++
		}
	}()

	reviews, err := mirror.GetAllPullRequests(repo, userName, repoName, client, errChan)
	if err != nil {
		errorf("Can't get PRs: %s", err.Error())
		return
	}

	statuses, err := mirror.GetAllStatuses(userName, repoName, client, errChan)
	if err != nil {
		errorf("Can't get statuses: %s", err.Error())
		return
	}
	close(errChan)

	nStatuses := len(statuses)
	nReviews := len(reviews)
	logChan := make(chan string, 1000)
	go func() {
		for msg := range logChan {
			log.Infof(c, msg)
		}
	}()
	log.Infof(c, "Done reading! Read %d statuses, %d PRs", nStatuses, nReviews)
	log.Infof(c, "Committing...\n")
	if err := mirror.WriteNewReports(statuses, repo, logChan); err != nil {
		errorf(err.Error())
		return
	}
	if err := mirror.WriteNewReviews(reviews, repo, logChan); err != nil {
		errorf(err.Error())
		return
	}
	close(logChan)
	err = syncNotes(repo)
	if err != nil {
		errorf("Error pushing initialization changes for %s/%s: %s",
			userName,
			repoName,
			err.Error())
		return
	}
	log.Infof(c, "Success initializing %s/%s", userName, repoName)

	err = modifyRepoData(c, userName, repoName, func(item *repoStorageData) {
		item.Status = statusReady
	})

	if err != nil {
		errorf("Can't change repo status for %s/%s: %s",
			userName,
			repoName,
			err.Error(),
		)
	}
}