mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-14 11:12:52 +00:00
8161479fde
For a `pull_request_target` (PRT) run, Gitea loads the top-level workflow from the trusted base branch, but any local reusable workflow it calls (`uses: ./...`) was read from the PR **head** commit, which the fork author controls. ## Fix **Record the source commit where the content is read.** `DetectedWorkflow` now carries a `SourceCommitSHA` filled in next to `Content`, so the PRT detection pass at the base commit records the base SHA automatically. **Defense in depth.** `loadReusableWorkflowSource` pins the PR base commit for a PRT run's local `uses: ./...` rather than trusting the stored SHA. This also covers runs recorded before this change, whose rows still hold the head SHA and would otherwise resolve from the fork on rerun. Existing run rows are not migrated. --------- Co-authored-by: Zettat <zettat123@gmail.com>
121 lines
3.7 KiB
Go
121 lines
3.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
actions_model "gitea.dev/models/actions"
|
|
actions_module "gitea.dev/modules/actions"
|
|
"gitea.dev/modules/json"
|
|
api "gitea.dev/modules/structs"
|
|
webhook_module "gitea.dev/modules/webhook"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEvaluateJobIfDefersMatrixExpression(t *testing.T) {
|
|
// A placeholder's `if:` reading `matrix.*` can only be decided per combination once the matrix is expanded.
|
|
|
|
emptyRun := &actions_model.ActionRun{} // if the gate is removed, the emptyRun will cause an error
|
|
deferredJob := func(ifExpr string) *actions_model.ActionRunJob {
|
|
return &actions_model.ActionRunJob{
|
|
ID: 1, JobID: "build", Needs: []string{"setup"}, IsMatrixDeferred: true,
|
|
WorkflowPayload: fmt.Appendf(nil, `name: test
|
|
on: push
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
if: %s
|
|
strategy:
|
|
matrix:
|
|
value: ${{ fromJson(needs.setup.outputs.m) }}
|
|
steps:
|
|
- run: echo
|
|
`, ifExpr),
|
|
}
|
|
}
|
|
|
|
for _, tt := range []struct {
|
|
ifExpr string
|
|
// wantNeedsFailed is what the `if:` decides when the needs did not all succeed.
|
|
wantNeedsFailed bool
|
|
}{
|
|
// These hold only for a real combination, so none can be decided before the matrix expands,
|
|
// and the fallback is the needs gate: a job whose needs did not succeed is still skipped.
|
|
{ifExpr: "${{ matrix.value == 1 }}"},
|
|
{ifExpr: "matrix.value == 1"}, // an `if:` may omit the `${{ }}`
|
|
// always() asks to run whatever the needs did, so it must not fall back to their gate.
|
|
{ifExpr: "${{ always() && matrix.value == 1 }}", wantNeedsFailed: true},
|
|
} {
|
|
t.Run(tt.ifExpr, func(t *testing.T) {
|
|
got, err := evaluateJobIf(t.Context(), emptyRun, nil, deferredJob(tt.ifExpr), nil, true)
|
|
require.NoError(t, err)
|
|
assert.True(t, got, "must reach the expansion that gates each combination on its own values")
|
|
|
|
got, err = evaluateJobIf(t.Context(), emptyRun, nil, deferredJob(tt.ifExpr), nil, false)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.wantNeedsFailed, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPullRequestTargetBaseSHA(t *testing.T) {
|
|
prPayload := func(baseSHA string) string {
|
|
payload, err := json.Marshal(api.PullRequestPayload{
|
|
PullRequest: &api.PullRequest{
|
|
Base: &api.PRBranchInfo{Sha: baseSHA},
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
return string(payload)
|
|
}
|
|
|
|
t.Run("pull_request_target with base SHA", func(t *testing.T) {
|
|
run := &actions_model.ActionRun{
|
|
Event: webhook_module.HookEventPullRequest,
|
|
TriggerEvent: actions_module.GithubEventPullRequestTarget,
|
|
EventPayload: prPayload("base-sha"),
|
|
}
|
|
got, ok := pullRequestTargetBaseSHA(run)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "base-sha", got)
|
|
})
|
|
|
|
t.Run("non pull_request_target trigger", func(t *testing.T) {
|
|
run := &actions_model.ActionRun{
|
|
Event: webhook_module.HookEventPullRequest,
|
|
TriggerEvent: actions_module.GithubEventPullRequest,
|
|
EventPayload: prPayload("base-sha"),
|
|
}
|
|
got, ok := pullRequestTargetBaseSHA(run)
|
|
assert.False(t, ok)
|
|
assert.Empty(t, got)
|
|
})
|
|
|
|
t.Run("missing base SHA", func(t *testing.T) {
|
|
run := &actions_model.ActionRun{
|
|
Event: webhook_module.HookEventPullRequest,
|
|
TriggerEvent: actions_module.GithubEventPullRequestTarget,
|
|
EventPayload: prPayload(""),
|
|
}
|
|
got, ok := pullRequestTargetBaseSHA(run)
|
|
assert.False(t, ok)
|
|
assert.Empty(t, got)
|
|
})
|
|
|
|
t.Run("invalid payload", func(t *testing.T) {
|
|
run := &actions_model.ActionRun{
|
|
Event: webhook_module.HookEventPullRequest,
|
|
TriggerEvent: actions_module.GithubEventPullRequestTarget,
|
|
EventPayload: "{",
|
|
}
|
|
got, ok := pullRequestTargetBaseSHA(run)
|
|
assert.False(t, ok)
|
|
assert.Empty(t, got)
|
|
})
|
|
}
|