2021-12-22 13:52:09 -06:00
|
|
|
package model
|
|
|
|
|
2022-01-21 10:10:00 -06:00
|
|
|
import (
|
2022-06-17 10:55:21 -05:00
|
|
|
"context"
|
2022-01-21 10:10:00 -06:00
|
|
|
"fmt"
|
2023-02-03 13:35:49 -06:00
|
|
|
"strings"
|
2022-01-21 10:10:00 -06:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2022-06-10 16:16:42 -05:00
|
|
|
"github.com/nektos/act/pkg/common/git"
|
2022-01-21 10:10:00 -06:00
|
|
|
)
|
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
type GithubContext struct {
|
|
|
|
Event map[string]interface{} `json:"event"`
|
|
|
|
EventPath string `json:"event_path"`
|
|
|
|
Workflow string `json:"workflow"`
|
|
|
|
RunID string `json:"run_id"`
|
|
|
|
RunNumber string `json:"run_number"`
|
|
|
|
Actor string `json:"actor"`
|
|
|
|
Repository string `json:"repository"`
|
|
|
|
EventName string `json:"event_name"`
|
|
|
|
Sha string `json:"sha"`
|
|
|
|
Ref string `json:"ref"`
|
2022-05-08 09:23:19 -05:00
|
|
|
RefName string `json:"ref_name"`
|
|
|
|
RefType string `json:"ref_type"`
|
2021-12-22 13:52:09 -06:00
|
|
|
HeadRef string `json:"head_ref"`
|
|
|
|
BaseRef string `json:"base_ref"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Workspace string `json:"workspace"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
ActionPath string `json:"action_path"`
|
|
|
|
ActionRef string `json:"action_ref"`
|
|
|
|
ActionRepository string `json:"action_repository"`
|
|
|
|
Job string `json:"job"`
|
|
|
|
JobName string `json:"job_name"`
|
|
|
|
RepositoryOwner string `json:"repository_owner"`
|
|
|
|
RetentionDays string `json:"retention_days"`
|
|
|
|
RunnerPerflog string `json:"runner_perflog"`
|
|
|
|
RunnerTrackingID string `json:"runner_tracking_id"`
|
2023-04-13 09:09:29 -05:00
|
|
|
ServerURL string `json:"server_url"`
|
2023-04-13 08:09:28 -05:00
|
|
|
APIURL string `json:"api_url"`
|
|
|
|
GraphQLURL string `json:"graphql_url"`
|
2021-12-22 13:52:09 -06:00
|
|
|
}
|
2022-01-21 10:10:00 -06:00
|
|
|
|
|
|
|
func asString(v interface{}) string {
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
} else if s, ok := v.(string); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if len(ks) == 0 { // degenerate input
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if rval, ok = m[ks[0]]; !ok {
|
|
|
|
return nil
|
|
|
|
} else if len(ks) == 1 { // we've reached the final key
|
|
|
|
return rval
|
|
|
|
} else if m, ok = rval.(map[string]interface{}); !ok {
|
|
|
|
return nil
|
|
|
|
} else { // 1+ more keys
|
|
|
|
return nestedMapLookup(m, ks[1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func withDefaultBranch(ctx context.Context, b string, event map[string]interface{}) map[string]interface{} {
|
2022-01-21 10:10:00 -06:00
|
|
|
repoI, ok := event["repository"]
|
|
|
|
if !ok {
|
|
|
|
repoI = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, ok := repoI.(map[string]interface{})
|
|
|
|
if !ok {
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Warnf("unable to set default branch to %v", b)
|
2022-01-21 10:10:00 -06:00
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the branch is already there return with no changes
|
|
|
|
if _, ok = repo["default_branch"]; ok {
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
repo["default_branch"] = b
|
|
|
|
event["repository"] = repo
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:16:42 -05:00
|
|
|
var findGitRef = git.FindGitRef
|
|
|
|
var findGitRevision = git.FindGitRevision
|
2022-01-21 10:10:00 -06:00
|
|
|
|
2023-02-03 13:35:49 -06:00
|
|
|
func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repoPath string) {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2023-02-03 13:35:49 -06:00
|
|
|
|
2022-01-21 10:10:00 -06:00
|
|
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
|
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
|
|
switch ghc.EventName {
|
|
|
|
case "pull_request_target":
|
2022-09-26 17:49:19 -05:00
|
|
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", ghc.BaseRef)
|
2022-01-21 10:10:00 -06:00
|
|
|
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
2022-07-12 06:36:53 -05:00
|
|
|
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
2022-01-21 10:10:00 -06:00
|
|
|
case "deployment", "deployment_status":
|
|
|
|
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
|
|
|
|
case "release":
|
2023-03-09 14:03:13 -06:00
|
|
|
ghc.Ref = fmt.Sprintf("refs/tags/%s", asString(nestedMapLookup(ghc.Event, "release", "tag_name")))
|
2022-01-21 10:10:00 -06:00
|
|
|
case "push", "create", "workflow_dispatch":
|
|
|
|
ghc.Ref = asString(ghc.Event["ref"])
|
|
|
|
default:
|
2022-09-26 17:49:19 -05:00
|
|
|
defaultBranch := asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
|
|
|
if defaultBranch != "" {
|
|
|
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", defaultBranch)
|
|
|
|
}
|
2022-01-21 10:10:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.Ref == "" {
|
2022-06-17 10:55:21 -05:00
|
|
|
ref, err := findGitRef(ctx, repoPath)
|
2022-01-21 10:10:00 -06:00
|
|
|
if err != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Warningf("unable to get git ref: %v", err)
|
2022-01-21 10:10:00 -06:00
|
|
|
} else {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("using github ref: %s", ref)
|
2022-01-21 10:10:00 -06:00
|
|
|
ghc.Ref = ref
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the branch in the event data
|
|
|
|
if defaultBranch != "" {
|
2022-06-17 10:55:21 -05:00
|
|
|
ghc.Event = withDefaultBranch(ctx, defaultBranch, ghc.Event)
|
2022-01-21 10:10:00 -06:00
|
|
|
} else {
|
2022-06-17 10:55:21 -05:00
|
|
|
ghc.Event = withDefaultBranch(ctx, "master", ghc.Event)
|
2022-01-21 10:10:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.Ref == "" {
|
2022-09-26 17:49:19 -05:00
|
|
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", asString(nestedMapLookup(ghc.Event, "repository", "default_branch")))
|
2022-01-21 10:10:00 -06:00
|
|
|
}
|
|
|
|
}
|
2023-02-03 13:35:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ghc *GithubContext) SetSha(ctx context.Context, repoPath string) {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
|
|
|
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
|
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
|
|
switch ghc.EventName {
|
|
|
|
case "pull_request_target":
|
|
|
|
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
|
|
|
case "deployment", "deployment_status":
|
|
|
|
ghc.Sha = asString(nestedMapLookup(ghc.Event, "deployment", "sha"))
|
|
|
|
case "push", "create", "workflow_dispatch":
|
|
|
|
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
|
|
|
|
ghc.Sha = asString(ghc.Event["after"])
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 10:10:00 -06:00
|
|
|
|
|
|
|
if ghc.Sha == "" {
|
2022-06-17 10:55:21 -05:00
|
|
|
_, sha, err := findGitRevision(ctx, repoPath)
|
2022-01-21 10:10:00 -06:00
|
|
|
if err != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Warningf("unable to get git revision: %v", err)
|
2022-01-21 10:10:00 -06:00
|
|
|
} else {
|
|
|
|
ghc.Sha = sha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 13:35:49 -06:00
|
|
|
|
|
|
|
func (ghc *GithubContext) SetRepositoryAndOwner(ctx context.Context, githubInstance string, remoteName string, repoPath string) {
|
|
|
|
if ghc.Repository == "" {
|
|
|
|
repo, err := git.FindGithubRepo(ctx, repoPath, githubInstance, remoteName)
|
|
|
|
if err != nil {
|
2024-01-28 13:21:21 -06:00
|
|
|
common.Logger(ctx).Warningf("unable to get git repo (githubInstance: %v; remoteName: %v, repoPath: %v): %v", githubInstance, remoteName, repoPath, err)
|
2023-02-03 13:35:49 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ghc.Repository = repo
|
|
|
|
}
|
|
|
|
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ghc *GithubContext) SetRefTypeAndName() {
|
|
|
|
var refType, refName string
|
|
|
|
|
|
|
|
// https://docs.github.com/en/actions/learn-github-actions/environment-variables
|
|
|
|
if strings.HasPrefix(ghc.Ref, "refs/tags/") {
|
|
|
|
refType = "tag"
|
|
|
|
refName = ghc.Ref[len("refs/tags/"):]
|
|
|
|
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
|
|
|
|
refType = "branch"
|
|
|
|
refName = ghc.Ref[len("refs/heads/"):]
|
2023-03-09 14:03:13 -06:00
|
|
|
} else if strings.HasPrefix(ghc.Ref, "refs/pull/") {
|
|
|
|
refType = ""
|
|
|
|
refName = ghc.Ref[len("refs/pull/"):]
|
2023-02-03 13:35:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.RefType == "" {
|
|
|
|
ghc.RefType = refType
|
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.RefName == "" {
|
|
|
|
ghc.RefName = refName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ghc *GithubContext) SetBaseAndHeadRef() {
|
|
|
|
if ghc.EventName == "pull_request" || ghc.EventName == "pull_request_target" {
|
|
|
|
if ghc.BaseRef == "" {
|
|
|
|
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.HeadRef == "" {
|
|
|
|
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|