mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
94 lines
3.3 KiB
Go
94 lines
3.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
// env keys for git hooks need
|
|
const (
|
|
EnvRepoName = "GIT_REPO_NAME"
|
|
EnvRepoUsername = "GIT_REPO_USER_NAME"
|
|
EnvRepoID = "GIT_REPO_ID"
|
|
EnvRepoIsWiki = "GIT_REPO_IS_WIKI"
|
|
EnvPusherName = "GIT_PUSHER_NAME"
|
|
EnvPusherEmail = "GIT_PUSHER_EMAIL"
|
|
EnvPusherID = "GIT_PUSHER_ID"
|
|
EnvKeyID = "GIT_KEY_ID" // public key ID
|
|
EnvDeployKeyID = "GIT_DEPLOY_KEY_ID"
|
|
EnvPRID = "GIT_PR_ID"
|
|
EnvPRIndex = "GIT_PR_INDEX" // not used by the forge at the moment, it is for custom git hooks
|
|
EnvPushTrigger = "GIT_PUSH_TRIGGER"
|
|
EnvIsInternal = "GIT_INTERNAL_PUSH"
|
|
EnvAppURL = "GIT_ROOT_URL"
|
|
EnvActionsTaskID = "GIT_ACTIONS_TASK_ID"
|
|
)
|
|
|
|
type PushTrigger string
|
|
|
|
const (
|
|
PushTriggerPRMergeToBase PushTrigger = "pr-merge-to-base"
|
|
PushTriggerPRUpdateWithBase PushTrigger = "pr-update-with-base"
|
|
)
|
|
|
|
// InternalPushingEnvironment returns an os environment to switch off hooks on push
|
|
// It is recommended to avoid using this unless you are pushing within a transaction
|
|
// or if you absolutely are sure that post-receive and pre-receive will do nothing
|
|
// We provide the full pushing-environment for other hook providers
|
|
func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
|
|
return append(PushingEnvironment(doer, repo),
|
|
EnvIsInternal+"=true",
|
|
)
|
|
}
|
|
|
|
// PushingEnvironment returns an os environment to allow hooks to work on push
|
|
func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
|
|
return FullPushingEnvironment(doer, doer, repo, repo.Name, 0, 0)
|
|
}
|
|
|
|
func DoerPushingEnvironment(doer *user_model.User, repo *repo_model.Repository, isWiki bool) []string {
|
|
env := []string{
|
|
EnvAppURL + "=" + setting.AppURL,
|
|
EnvRepoName + "=" + repo.Name + util.Iif(isWiki, ".wiki", ""),
|
|
EnvRepoUsername + "=" + repo.OwnerName,
|
|
EnvRepoID + "=" + strconv.FormatInt(repo.ID, 10),
|
|
EnvRepoIsWiki + "=" + strconv.FormatBool(isWiki),
|
|
EnvPusherName + "=" + doer.Name,
|
|
EnvPusherID + "=" + strconv.FormatInt(doer.ID, 10),
|
|
}
|
|
if !doer.KeepEmailPrivate {
|
|
env = append(env, EnvPusherEmail+"="+doer.Email)
|
|
}
|
|
if taskID, isActionsUser := user_model.GetActionsUserTaskID(doer); isActionsUser {
|
|
env = append(env, EnvActionsTaskID+"="+strconv.FormatInt(taskID, 10))
|
|
}
|
|
return env
|
|
}
|
|
|
|
// FullPushingEnvironment returns an os environment to allow hooks to work on push
|
|
func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID, prIndex int64) []string {
|
|
isWiki := strings.HasSuffix(repoName, ".wiki")
|
|
authorSig := author.NewGitSig()
|
|
committerSig := committer.NewGitSig()
|
|
environ := append(os.Environ(),
|
|
"GIT_AUTHOR_NAME="+authorSig.Name,
|
|
"GIT_AUTHOR_EMAIL="+authorSig.Email,
|
|
"GIT_COMMITTER_NAME="+committerSig.Name,
|
|
"GIT_COMMITTER_EMAIL="+committerSig.Email,
|
|
EnvPRID+"="+strconv.FormatInt(prID, 10),
|
|
EnvPRIndex+"="+strconv.FormatInt(prIndex, 10),
|
|
"SSH_ORIGINAL_COMMAND=forge-internal",
|
|
)
|
|
environ = append(environ, DoerPushingEnvironment(committer, repo, isWiki)...)
|
|
return environ
|
|
}
|