<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/jessevdk/go-flags"
)

func checkIfTokensPresent() {
	if len(os.Getenv("TOKEN_PLATFORM")) == 0 {
		log.Fatal("TOKEN_PLATFORM is not set")
	}

	if len(os.Getenv("TOKEN_PROTONET")) == 0 {
		log.Fatal("TOKEN_PROTONET is not set")
	}
}

func updateJSON(repo *buildsRepo, opts taggerOptions, tagTimestamp, isoTimestamp string) error {
	oldBuilds, err := repo.loadChannel(opts.SourceTag)
	if err != nil {
		return err
	}

	newBuilds := []buildsDatum{oldBuilds[0]}
	newBuilds[0].Build = opts.Build
	newBuilds[0].PublishedAt = isoTimestamp
	if opts.URL != "" {
		newBuilds[0].URL = opts.URL
	}

	for k := range newBuilds[0].Images {
		newBuilds[0].Images[k] = tagTimestamp
	}

	log.Printf("Old build version: %d", oldBuilds[0].Build)
	log.Printf("New build version: %d", newBuilds[0].Build)

	err = repo.saveChannel(opts.TargetTag, newBuilds)
	if err != nil {
		return fmt.Errorf("Failed to save channel json: %s", err.Error())
	}

	if opts.Commit == true {
		commitMessage := fmt.Sprintf("release on channel '%s' at %s", opts.TargetTag, isoTimestamp)
		err := repo.addAndCommitChannel(opts.TargetTag, commitMessage)
		if err != nil {
			return err
		}

		err = repo.push()
		if err != nil {
			return err
		}
		log.Println("Push successful")
	} else {
		dump, _ := repo.dumpChannel(opts.TargetTag)
		log.Printf("New JSON:\n%s\n", dump)
	}

	return nil
}

type taggerOptions struct {
	Commit    bool   `short:"c" long:"commit" description:"Commit the changes. Will make a dry run without this flag."`
	Build     int32  `short:"b" long:"build" required:"true" description:"Specify the build number to be placed inside the JSON."`
	SourceTag string `short:"s" long:"source-tag" default:"development" description:"Registry tag to be retagging from."`
	TargetTag string `short:"t" long:"target-tag" default:"soul3" description:"Registry tag to be retagging to."`
	URL       string `short:"u" long:"url" description:"Release notes URL"`
}

func main() {
	var opts taggerOptions

	_, err := flags.Parse(&opts)
	if err != nil {
		os.Exit(1)
	}

	currentTime := time.Now().UTC()
	tagTimestamp := currentTime.Format("2006-01-02-1504")
	isoTimestamp := currentTime.Format("2006-01-02T15:04:05Z")

	repo, err := prepareRepo()
	if err != nil {
		log.Fatalf("Failed to clone the builds repo: %s", err.Error())
	}
	defer repo.Close()

	builds, err := repo.loadChannel(opts.SourceTag)
	if err != nil {
		log.Fatalf("Failed to load build data from channel '%s'", opts.SourceTag)
	}

	fmt.Printf("Tag timestamp: %s\n", tagTimestamp)
	fmt.Printf("ISO timestamp: %s\n", isoTimestamp)

	if opts.Commit == true {
		checkIfTokensPresent()

		err = retagAll(builds[0].Images, opts.SourceTag, tagTimestamp)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		log.Printf("Dry run. Would otherwise retag following images from '%s' to '%s' and update channel '%s':\n", opts.SourceTag, tagTimestamp, opts.TargetTag)
		for k := range builds[0].Images {
			log.Printf(" * %s\n", k)
		}
	}

	err = updateJSON(repo, opts, tagTimestamp, isoTimestamp)
	if err != nil {
		log.Fatal(err)
	}
}
<commit_msg>rename SourceTag and TargetTag to SourceChannel and TargetChannel<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/jessevdk/go-flags"
)

func checkIfTokensPresent() {
	if len(os.Getenv("TOKEN_PLATFORM")) == 0 {
		log.Fatal("TOKEN_PLATFORM is not set")
	}

	if len(os.Getenv("TOKEN_PROTONET")) == 0 {
		log.Fatal("TOKEN_PROTONET is not set")
	}
}

func updateJSON(repo *buildsRepo, opts taggerOptions, tagTimestamp, isoTimestamp string) error {
	oldBuilds, err := repo.loadChannel(opts.SourceChannel)
	if err != nil {
		return err
	}

	newBuilds := []buildsDatum{oldBuilds[0]}
	newBuilds[0].Build = opts.Build
	newBuilds[0].PublishedAt = isoTimestamp
	if opts.URL != "" {
		newBuilds[0].URL = opts.URL
	}

	for k := range newBuilds[0].Images {
		newBuilds[0].Images[k] = tagTimestamp
	}

	log.Printf("Old build version: %d", oldBuilds[0].Build)
	log.Printf("New build version: %d", newBuilds[0].Build)

	err = repo.saveChannel(opts.TargetChannel, newBuilds)
	if err != nil {
		return fmt.Errorf("Failed to save channel json: %s", err.Error())
	}

	if opts.Commit == true {
		commitMessage := fmt.Sprintf("release on channel '%s' at %s", opts.TargetChannel, isoTimestamp)
		err := repo.addAndCommitChannel(opts.TargetChannel, commitMessage)
		if err != nil {
			return err
		}

		err = repo.push()
		if err != nil {
			return err
		}
		log.Println("Push successful")
	} else {
		dump, _ := repo.dumpChannel(opts.TargetChannel)
		log.Printf("New JSON:\n%s\n", dump)
	}

	return nil
}

type taggerOptions struct {
	Commit        bool   `short:"c" long:"commit" description:"Commit the changes. Will make a dry run without this flag."`
	Build         int32  `short:"b" long:"build" required:"true" description:"Specify the build number to be placed inside the JSON."`
	SourceChannel string `short:"s" long:"source-channel" default:"development" description:"Release channel to be retagging/copying from."`
	TargetChannel string `short:"t" long:"target-channel" default:"soul3" description:"Release channel to be retagging to."`
	URL           string `short:"u" long:"url" description:"Release notes URL"`
}

func main() {
	var opts taggerOptions

	_, err := flags.Parse(&opts)
	if err != nil {
		os.Exit(1)
	}

	currentTime := time.Now().UTC()
	tagTimestamp := currentTime.Format("2006-01-02-1504")
	isoTimestamp := currentTime.Format("2006-01-02T15:04:05Z")

	repo, err := prepareRepo()
	if err != nil {
		log.Fatalf("Failed to clone the builds repo: %s", err.Error())
	}
	defer repo.Close()

	builds, err := repo.loadChannel(opts.SourceChannel)
	if err != nil {
		log.Fatalf("Failed to load build data from channel '%s'", opts.SourceChannel)
	}

	fmt.Printf("Tag timestamp: %s\n", tagTimestamp)
	fmt.Printf("ISO timestamp: %s\n", isoTimestamp)

	if opts.Commit == true {
		checkIfTokensPresent()

		err = retagAll(builds[0].Images, opts.SourceChannel, tagTimestamp)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		log.Printf("Dry run. Would otherwise retag following images from '%s' to '%s' and update channel '%s':\n", opts.SourceChannel, tagTimestamp, opts.TargetChannel)
		for k := range builds[0].Images {
			log.Printf(" * %s\n", k)
		}
	}

	err = updateJSON(repo, opts, tagTimestamp, isoTimestamp)
	if err != nil {
		log.Fatal(err)
	}
}

{'fraction_non_alphanumeric': 0.1495166822575616, 'fraction_numerical': 0.013096351730589336, 'mean_word_length': 4.194331983805668, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '384408', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2251, 'n_words': 573}