From 0e6489317ebb8c2a01229f785e12ed440141eaa5 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Apr 2025 23:10:54 -0600 Subject: [PATCH] Get changed files based on merge base when checking `pull_request` actions trigger (#34106) (#34120) Backport #34106 Fix #33941 --- modules/actions/workflows.go | 4 +- tests/integration/actions_trigger_test.go | 110 ++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 0d2b0dd919..a538b6e290 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -463,7 +463,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa matchTimes++ } case "paths": - filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) + filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.MergeBase) if err != nil { log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", headCommit.ID.String(), err) } else { @@ -476,7 +476,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa } } case "paths-ignore": - filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) + filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.MergeBase) if err != nil { log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", headCommit.ID.String(), err) } else { diff --git a/tests/integration/actions_trigger_test.go b/tests/integration/actions_trigger_test.go index 4718aa7e73..a026e1f239 100644 --- a/tests/integration/actions_trigger_test.go +++ b/tests/integration/actions_trigger_test.go @@ -4,7 +4,9 @@ package integration import ( + "encoding/base64" "fmt" + "net/http" "net/url" "strings" "testing" @@ -24,7 +26,9 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" + "code.gitea.io/gitea/modules/util" pull_service "code.gitea.io/gitea/services/pull" release_service "code.gitea.io/gitea/services/release" repo_service "code.gitea.io/gitea/services/repository" @@ -451,3 +455,109 @@ func TestCreateDeleteRefEvent(t *testing.T) { assert.NotNil(t, run) }) } + +func TestClosePullRequestWithPath(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + // user2 is the owner of the base repo + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + user2Token := getTokenForLoggedInUser(t, loginUser(t, user2.Name), auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) + // user4 is the owner of the fork repo + user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) + user4Token := getTokenForLoggedInUser(t, loginUser(t, user4.Name), auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) + + // create the base repo + req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{ + Name: "close-pull-request-with-path", + Private: false, + Readme: "Default", + AutoInit: true, + DefaultBranch: "main", + }).AddTokenAuth(user2Token) + resp := MakeRequest(t, req, http.StatusCreated) + var apiBaseRepo api.Repository + DecodeJSON(t, resp, &apiBaseRepo) + baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiBaseRepo.ID}) + user2APICtx := NewAPITestContext(t, baseRepo.OwnerName, baseRepo.Name, auth_model.AccessTokenScopeWriteRepository) + + // init the workflow + wfTreePath := ".gitea/workflows/pull.yml" + wfFileContent := `name: Pull Request +on: + pull_request: + types: + - closed + paths: + - 'app/**' +jobs: + echo: + runs-on: ubuntu-latest + steps: + - run: echo 'Hello World' +` + + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, wfTreePath), &api.CreateFileOptions{ + FileOptions: api.FileOptions{ + BranchName: baseRepo.DefaultBranch, + Message: "create " + wfTreePath, + Author: api.Identity{ + Name: user2.Name, + Email: user2.Email, + }, + Committer: api.Identity{ + Name: user2.Name, + Email: user2.Email, + }, + Dates: api.CommitDateOptions{ + Author: time.Now(), + Committer: time.Now(), + }, + }, + ContentBase64: base64.StdEncoding.EncodeToString([]byte(wfFileContent)), + }).AddTokenAuth(user2Token) + MakeRequest(t, req, http.StatusCreated) + + // user4 forks the repo + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks", baseRepo.OwnerName, baseRepo.Name), + &api.CreateForkOption{ + Name: util.ToPointer("close-pull-request-with-path-fork"), + }).AddTokenAuth(user4Token) + resp = MakeRequest(t, req, http.StatusAccepted) + var apiForkRepo api.Repository + DecodeJSON(t, resp, &apiForkRepo) + forkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiForkRepo.ID}) + user4APICtx := NewAPITestContext(t, user4.Name, forkRepo.Name, auth_model.AccessTokenScopeWriteRepository) + + // user4 creates a pull request to add file "app/main.go" + doAPICreateFile(user4APICtx, "app/main.go", &api.CreateFileOptions{ + FileOptions: api.FileOptions{ + NewBranchName: "user4/add-main", + Message: "create main.go", + Author: api.Identity{ + Name: user4.Name, + Email: user4.Email, + }, + Committer: api.Identity{ + Name: user4.Name, + Email: user4.Email, + }, + Dates: api.CommitDateOptions{ + Author: time.Now(), + Committer: time.Now(), + }, + }, + ContentBase64: base64.StdEncoding.EncodeToString([]byte("// main.go")), + })(t) + apiPull, err := doAPICreatePullRequest(user4APICtx, baseRepo.OwnerName, baseRepo.Name, baseRepo.DefaultBranch, user4.Name+":user4/add-main")(t) + assert.NoError(t, err) + + doAPIMergePullRequest(user2APICtx, baseRepo.OwnerName, baseRepo.Name, apiPull.Index)(t) + + pullRequest := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: apiPull.ID}) + + // load and compare ActionRun + assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID})) + actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID}) + assert.Equal(t, actions_module.GithubEventPullRequest, actionRun.TriggerEvent) + assert.Equal(t, pullRequest.MergedCommitID, actionRun.CommitSHA) + }) +}