<commit_before>package main

import (
	"net/http"
)

func main() {

	http.HandleFunc("/events", handler)
	http.ListenAndServe(":5050", nil)
}

func handler(writer http.ResponseWriter, request *http.Request) {

	writer.WriteHeader(http.StatusOK)
}<commit_msg>watch branches; parse image tag; update deployments;<commit_after>package main

import (
	"net/http"
	"fmt"
	"strings"
	"errors"
	"os"

	log "github.com/Sirupsen/logrus"
)

var (
	kube KubeClient
	watchBranches Set
)

func main() {

	kube = NewKubeClient()
	watchBranches = getWatchRepositories()

	http.HandleFunc("/events", handler)
	http.ListenAndServe(":5050", nil)
}

func handler(writer http.ResponseWriter, request *http.Request) {

	if repositories, err := GetPushEventRepositories(request.Body); err != nil {
		log.Error(err)
		writer.WriteHeader(http.StatusBadRequest)
	} else {
		for _, currRepository := range repositories {
			branch, namespace, deployment, version, err := parseTag(currRepository.Tag)
			if err != nil {
				log.Error(err)
				continue
			}

			fmt.Println("branch: ", branch, " ur:", watchBranches)
			if watchBranches.Contains(branch) {
				image := fmt.Sprintf("%s:%s", currRepository.Name, version)
				if err := kube.UpdateDeployment(deployment, namespace, image); err != nil {
					log.Errorf("Failed to update deployment: %s, branch: %s, namespace: %s, image: %s",
						deployment, branch, namespace, image, err)
				}
			}
		}
		writer.WriteHeader(http.StatusOK)
	}
}

// example tag: master-default-ceribrouideplyment-7
func parseTag(tag string) (branch, namespace, deployment, version string, err error) {

	ret := strings.Split(tag, "-")
	if len(ret) != 4 || ret[0] == "" || ret[1] == "" || ret[2] == "" || ret[3] == "" {
		return "", "", "", "", errors.New(fmt.Sprintf(
			"Failed to parse docker image tag: %s. Format should be: <branch>-<kubernetes namespace>-<kubernetes deployment name>-<version>", tag))
	}

	return ret[0], ret[1], ret[2], ret[3], nil
}

func getWatchRepositories() Set {

	ret := NewSet()
	names := os.Getenv("WATCH_BRANCES")
	if len(names) > 0 {
		for _, currBranch := range strings.Split(names, ",") {
			ret.Add(currBranch)
		}
	} else {
		ret.Add("master")
	}
	log.Info("Watch branches:", watchBranches.ToArray())

	return ret
}
{'fraction_non_alphanumeric': 0.16260522817899867, 'fraction_numerical': 0.008418254319893664, 'mean_word_length': 4.300469483568075, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 8, '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': '4947268', 'n_tokens_mistral': 843, 'n_tokens_neox': 768, 'n_words': 177}