code.gitea.io/gitea@v1.22.3/modules/repository/env.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  
    11  	repo_model "code.gitea.io/gitea/models/repo"
    12  	user_model "code.gitea.io/gitea/models/user"
    13  	"code.gitea.io/gitea/modules/setting"
    14  )
    15  
    16  // env keys for git hooks need
    17  const (
    18  	EnvRepoName     = "GITEA_REPO_NAME"
    19  	EnvRepoUsername = "GITEA_REPO_USER_NAME"
    20  	EnvRepoID       = "GITEA_REPO_ID"
    21  	EnvRepoIsWiki   = "GITEA_REPO_IS_WIKI"
    22  	EnvPusherName   = "GITEA_PUSHER_NAME"
    23  	EnvPusherEmail  = "GITEA_PUSHER_EMAIL"
    24  	EnvPusherID     = "GITEA_PUSHER_ID"
    25  	EnvKeyID        = "GITEA_KEY_ID" // public key ID
    26  	EnvDeployKeyID  = "GITEA_DEPLOY_KEY_ID"
    27  	EnvPRID         = "GITEA_PR_ID"
    28  	EnvPushTrigger  = "GITEA_PUSH_TRIGGER"
    29  	EnvIsInternal   = "GITEA_INTERNAL_PUSH"
    30  	EnvAppURL       = "GITEA_ROOT_URL"
    31  	EnvActionPerm   = "GITEA_ACTION_PERM"
    32  )
    33  
    34  type PushTrigger string
    35  
    36  const (
    37  	PushTriggerPRMergeToBase    PushTrigger = "pr-merge-to-base"
    38  	PushTriggerPRUpdateWithBase PushTrigger = "pr-update-with-base"
    39  )
    40  
    41  // InternalPushingEnvironment returns an os environment to switch off hooks on push
    42  // It is recommended to avoid using this unless you are pushing within a transaction
    43  // or if you absolutely are sure that post-receive and pre-receive will do nothing
    44  // We provide the full pushing-environment for other hook providers
    45  func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
    46  	return append(PushingEnvironment(doer, repo),
    47  		EnvIsInternal+"=true",
    48  	)
    49  }
    50  
    51  // PushingEnvironment returns an os environment to allow hooks to work on push
    52  func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
    53  	return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
    54  }
    55  
    56  // FullPushingEnvironment returns an os environment to allow hooks to work on push
    57  func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID int64) []string {
    58  	isWiki := "false"
    59  	if strings.HasSuffix(repoName, ".wiki") {
    60  		isWiki = "true"
    61  	}
    62  
    63  	authorSig := author.NewGitSig()
    64  	committerSig := committer.NewGitSig()
    65  
    66  	environ := append(os.Environ(),
    67  		"GIT_AUTHOR_NAME="+authorSig.Name,
    68  		"GIT_AUTHOR_EMAIL="+authorSig.Email,
    69  		"GIT_COMMITTER_NAME="+committerSig.Name,
    70  		"GIT_COMMITTER_EMAIL="+committerSig.Email,
    71  		EnvRepoName+"="+repoName,
    72  		EnvRepoUsername+"="+repo.OwnerName,
    73  		EnvRepoIsWiki+"="+isWiki,
    74  		EnvPusherName+"="+committer.Name,
    75  		EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
    76  		EnvRepoID+"="+fmt.Sprintf("%d", repo.ID),
    77  		EnvPRID+"="+fmt.Sprintf("%d", prID),
    78  		EnvAppURL+"="+setting.AppURL,
    79  		"SSH_ORIGINAL_COMMAND=gitea-internal",
    80  	)
    81  
    82  	if !committer.KeepEmailPrivate {
    83  		environ = append(environ, EnvPusherEmail+"="+committer.Email)
    84  	}
    85  
    86  	return environ
    87  }