package ci

import (
	"fmt"

	log "github.com/sirupsen/logrus"
)

// Travis struct
type WoodpeckerCI struct{}

// Detect if on travis
func (t WoodpeckerCI) detect(envs map[string]string) (*ProviderConfig, error) {

	if envs["CI"] != "woodpecker" {
		return nil, fmt.Errorf("not running on woodpecker")
	}

	isPR := false

	value := envs["CI_COMMIT_PULL_REQUEST"]
	pr := ""

	if value == "" {
		log.Debugf("CI_COMMIT_PULL_REQUEST=%s, not running on pr", value)
	} else {
		isPR = true
		pr = value
	}

	return &ProviderConfig{
		Service:  "woodpecker",
		Name:     "Woodpecker CI",
		Commit:   envs["CI_COMMIT_SHA"],
		Tag:      envs["CI_COMMIT_TAG"],
		BuildURL: envs["CI_PIPELINE_URL"],
		Branch:   envs["CI_COMMIT_BRANCH"],
		IsPR:     isPR,
		PR:       pr,
		PRBranch: envs["CI_COMMIT_SOURCE_BRANCH"],
	}, nil
}
