<commit_before>// Package cienv provides utility for environment variable in CI services.
package cienv

import (
	"errors"
	"os"
	"regexp"
	"strconv"
	"strings"
)

// PullRequestInfo represents required information about GitHub PullRequest or
// GitLab MergeRequest.
type PullRequestInfo struct {
	Owner       string
	Repo        string
	PullRequest int // MergeRequest for GitLab.
	SHA         string

	// Optional.
	Branch string
}

// GetPullRequestInfo returns PullRequestInfo from environment variables.
//
// Supporrted CI services' documents:
// - Travis CI: https://docs.travis-ci.com/user/environment-variables/
// - Circle CI: https://circleci.com/docs/environment-variables/
// - Drone.io: http://docs.drone.io/environment-reference/
// - GitLab CI: https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables
//   - GitLab CI doesn't export ID of Merge Request. https://gitlab.com/gitlab-org/gitlab-ce/issues/15280
func GetPullRequestInfo() (prInfo *PullRequestInfo, isPR bool, err error) {
	owner, repo := getOwnerAndRepoFromSlug([]string{
		"TRAVIS_REPO_SLUG",
		"DRONE_REPO", // drone<=0.4
	})
	if owner == "" {
		owner = getOneEnvValue([]string{
			"CI_REPO_OWNER", // common
			"CIRCLE_PROJECT_USERNAME",
			"DRONE_REPO_OWNER",
			"CI_PROJECT_NAMESPACE", // GitLab CI
		})
	}
	if owner == "" {
		return nil, false, errors.New("cannot get repo owner from environment variable. Set CI_REPO_OWNER?")
	}

	if repo == "" {
		repo = getOneEnvValue([]string{
			"CI_REPO_NAME", // common
			"CIRCLE_PROJECT_REPONAME",
			"DRONE_REPO_NAME",
			"CI_PROJECT_NAME", // GitLab CI
		})
	}

	if owner == "" {
		return nil, false, errors.New("cannot get repo name from environment variable. Set CI_REPO_NAME?")
	}

	sha := getOneEnvValue([]string{
		"CI_COMMIT", // common
		"TRAVIS_PULL_REQUEST_SHA",
		"CIRCLE_SHA1",
		"DRONE_COMMIT",
		"CI_COMMIT_SHA", // GitLab CI
	})
	if sha == "" {
		return nil, false, errors.New("cannot get commit SHA from environment variable. Set CI_COMMIT?")
	}

	branch := getOneEnvValue([]string{
		"CI_BRANCH", // common
		"TRAVIS_PULL_REQUEST_BRANCH",
		"CIRCLE_BRANCH",
		"DRONE_COMMIT_BRANCH",<commit_msg>look up TRAVIS_COMMIT for non-pr build<commit_after>// Package cienv provides utility for environment variable in CI services.
package cienv

import (
	"errors"
	"os"
	"regexp"
	"strconv"
	"strings"
)

// PullRequestInfo represents required information about GitHub PullRequest or
// GitLab MergeRequest.
type PullRequestInfo struct {
	Owner       string
	Repo        string
	PullRequest int // MergeRequest for GitLab.
	SHA         string

	// Optional.
	Branch string
}

// GetPullRequestInfo returns PullRequestInfo from environment variables.
//
// Supporrted CI services' documents:
// - Travis CI: https://docs.travis-ci.com/user/environment-variables/
// - Circle CI: https://circleci.com/docs/environment-variables/
// - Drone.io: http://docs.drone.io/environment-reference/
// - GitLab CI: https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables
//   - GitLab CI doesn't export ID of Merge Request. https://gitlab.com/gitlab-org/gitlab-ce/issues/15280
func GetPullRequestInfo() (prInfo *PullRequestInfo, isPR bool, err error) {
	owner, repo := getOwnerAndRepoFromSlug([]string{
		"TRAVIS_REPO_SLUG",
		"DRONE_REPO", // drone<=0.4
	})
	if owner == "" {
		owner = getOneEnvValue([]string{
			"CI_REPO_OWNER", // common
			"CIRCLE_PROJECT_USERNAME",
			"DRONE_REPO_OWNER",
			"CI_PROJECT_NAMESPACE", // GitLab CI
		})
	}
	if owner == "" {
		return nil, false, errors.New("cannot get repo owner from environment variable. Set CI_REPO_OWNER?")
	}

	if repo == "" {
		repo = getOneEnvValue([]string{
			"CI_REPO_NAME", // common
			"CIRCLE_PROJECT_REPONAME",
			"DRONE_REPO_NAME",
			"CI_PROJECT_NAME", // GitLab CI
		})
	}

	if owner == "" {
		return nil, false, errors.New("cannot get repo name from environment variable. Set CI_REPO_NAME?")
	}

	sha := getOneEnvValue([]string{
		"CI_COMMIT", // common
		"TRAVIS_PULL_REQUEST_SHA",
		"TRAVIS_COMMIT",
		"CIRCLE_SHA1",
		"DRONE_COMMIT",
		"CI_COMMIT_SHA", // GitLab CI
	})
	if sha == "" {
		return nil, false, errors.New("cannot get commit SHA from environment variable. Set CI_COMMIT?")
	}

	branch := getOneEnvValue([]string{
		"CI_BRANCH", // common
		"TRAVIS_PULL_REQUEST_BRANCH",
		"CIRCLE_BRANCH",
		"DRONE_COMMIT_BRANCH",
{'fraction_non_alphanumeric': 0.13293741434444953, 'fraction_numerical': 0.003654636820465966, 'mean_word_length': 3.812087912087912, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '1013680', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1541, 'n_words': 347}