// Adds a single commit to a repo with the current data, e.g. 2022-04-01.
//
// Used in a kubernetes cron job to populate a repo that has one commit per day.
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"os"
	"path/filepath"

	"go.skia.org/infra/go/auth"
	"go.skia.org/infra/go/git"
	"go.skia.org/infra/go/gitauth"
	"go.skia.org/infra/go/now"
	"go.skia.org/infra/go/sklog"
	"go.skia.org/infra/go/util"
	"golang.org/x/oauth2/google"
)

const filename = "DATE"

var (
	authorEmail = flag.String("author_email", "perf-comp-ui@skia-public.iam.gserviceaccount.com", "Email address of the git author.")
	local       = flag.Bool("local", false, "Running locally if true. As opposed to in production.")
	repoURL     = flag.String("gitrepo", "https://skia.googlesource.com/perf-compui", "The repo that has commits associated with runs of the cron job.")
)

func main() {
	ctx := context.Background()
	ts, err := google.DefaultTokenSource(ctx, auth.ScopeGerrit)
	if err != nil {
		sklog.Fatal(err)
	}

	// Do all the work in a temp directory.
	workDir, err := os.MkdirTemp("", "comp-ui-cron-job")
	if err != nil {
		sklog.Fatal(err)
	}
	defer func() {
		if err := os.RemoveAll(workDir); err != nil {
			sklog.Error(err)
		}
	}()

	// We presume that if running locally that you've already authenticated to
	// Gerrit, otherwise write out a git cookie that enables R/W access to the
	// git repo.
	if !*local {
		if _, err := gitauth.New(ctx, ts, "/tmp/git-cookie", true, *authorEmail); err != nil {
			sklog.Fatal(err)
		}
	}

	// Checkout the repo.
	checkout, err := git.NewCheckout(ctx, *repoURL, workDir)
	if err != nil {
		sklog.Fatalf("Unable to create the checkout of %q at %q: %s", *repoURL, workDir, err)
	}
	if err := checkout.UpdateBranch(ctx, git.MainBranch); err != nil {
		sklog.Fatalf("Unable to update the checkout of %q at %q: %s", *repoURL, workDir, err)
	}

	// We write today as the data into a file called "DATE".
	timestamp := now.Now(ctx)
	dateAsString := timestamp.Format("2006.01.02")
	err = util.WithWriteFile(filepath.Join(checkout.Dir(), filename), func(w io.Writer) error {
		_, err := w.Write([]byte(dateAsString))
		return err
	})
	if err != nil {
		sklog.Fatal(err)
	}

	// Commit and push the update to the git repo.
	if msg, err := checkout.Git(ctx, "add", filename); err != nil {
		sklog.Fatal(err, msg)
	}
	if msg, err := checkout.Git(ctx, "commit", "-m", dateAsString, fmt.Sprintf("--date=%d", timestamp.Unix())); err != nil {
		sklog.Fatal(err, msg)
	}
	if msg, err := checkout.Git(ctx, "push", git.DefaultRemote, git.MainBranch); err != nil {
		sklog.Fatal(err, msg)
	}
	sklog.Infof("Success: %s", dateAsString)
}
