code.gitea.io/gitea@v1.21.7/tests/integration/actions_trigger_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/url"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	actions_model "code.gitea.io/gitea/models/actions"
    13  	"code.gitea.io/gitea/models/db"
    14  	issues_model "code.gitea.io/gitea/models/issues"
    15  	repo_model "code.gitea.io/gitea/models/repo"
    16  	unit_model "code.gitea.io/gitea/models/unit"
    17  	"code.gitea.io/gitea/models/unittest"
    18  	user_model "code.gitea.io/gitea/models/user"
    19  	actions_module "code.gitea.io/gitea/modules/actions"
    20  	"code.gitea.io/gitea/modules/git"
    21  	pull_service "code.gitea.io/gitea/services/pull"
    22  	repo_service "code.gitea.io/gitea/services/repository"
    23  	files_service "code.gitea.io/gitea/services/repository/files"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestPullRequestTargetEvent(t *testing.T) {
    29  	onGiteaRun(t, func(t *testing.T, u *url.URL) {
    30  		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the base repo
    31  		org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})  // owner of the forked repo
    32  
    33  		// create the base repo
    34  		baseRepo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
    35  			Name:          "repo-pull-request-target",
    36  			Description:   "test pull-request-target event",
    37  			AutoInit:      true,
    38  			Gitignores:    "Go",
    39  			License:       "MIT",
    40  			Readme:        "Default",
    41  			DefaultBranch: "main",
    42  			IsPrivate:     false,
    43  		})
    44  		assert.NoError(t, err)
    45  		assert.NotEmpty(t, baseRepo)
    46  
    47  		// enable actions
    48  		err = repo_service.UpdateRepositoryUnits(db.DefaultContext, baseRepo, []repo_model.RepoUnit{{
    49  			RepoID: baseRepo.ID,
    50  			Type:   unit_model.TypeActions,
    51  		}}, nil)
    52  		assert.NoError(t, err)
    53  
    54  		// create the forked repo
    55  		forkedRepo, err := repo_service.ForkRepository(git.DefaultContext, user2, org3, repo_service.ForkRepoOptions{
    56  			BaseRepo:    baseRepo,
    57  			Name:        "forked-repo-pull-request-target",
    58  			Description: "test pull-request-target event",
    59  		})
    60  		assert.NoError(t, err)
    61  		assert.NotEmpty(t, forkedRepo)
    62  
    63  		// add workflow file to the base repo
    64  		addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, baseRepo, user2, &files_service.ChangeRepoFilesOptions{
    65  			Files: []*files_service.ChangeRepoFile{
    66  				{
    67  					Operation:     "create",
    68  					TreePath:      ".gitea/workflows/pr.yml",
    69  					ContentReader: strings.NewReader("name: test\non:\n  pull_request_target:\n    paths:\n      - 'file_*.txt'\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo helloworld\n"),
    70  				},
    71  			},
    72  			Message:   "add workflow",
    73  			OldBranch: "main",
    74  			NewBranch: "main",
    75  			Author: &files_service.IdentityOptions{
    76  				Name:  user2.Name,
    77  				Email: user2.Email,
    78  			},
    79  			Committer: &files_service.IdentityOptions{
    80  				Name:  user2.Name,
    81  				Email: user2.Email,
    82  			},
    83  			Dates: &files_service.CommitDateOptions{
    84  				Author:    time.Now(),
    85  				Committer: time.Now(),
    86  			},
    87  		})
    88  		assert.NoError(t, err)
    89  		assert.NotEmpty(t, addWorkflowToBaseResp)
    90  
    91  		// add a new file to the forked repo
    92  		addFileToForkedResp, err := files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, org3, &files_service.ChangeRepoFilesOptions{
    93  			Files: []*files_service.ChangeRepoFile{
    94  				{
    95  					Operation:     "create",
    96  					TreePath:      "file_1.txt",
    97  					ContentReader: strings.NewReader("file1"),
    98  				},
    99  			},
   100  			Message:   "add file1",
   101  			OldBranch: "main",
   102  			NewBranch: "fork-branch-1",
   103  			Author: &files_service.IdentityOptions{
   104  				Name:  org3.Name,
   105  				Email: org3.Email,
   106  			},
   107  			Committer: &files_service.IdentityOptions{
   108  				Name:  org3.Name,
   109  				Email: org3.Email,
   110  			},
   111  			Dates: &files_service.CommitDateOptions{
   112  				Author:    time.Now(),
   113  				Committer: time.Now(),
   114  			},
   115  		})
   116  		assert.NoError(t, err)
   117  		assert.NotEmpty(t, addFileToForkedResp)
   118  
   119  		// create Pull
   120  		pullIssue := &issues_model.Issue{
   121  			RepoID:   baseRepo.ID,
   122  			Title:    "Test pull-request-target-event",
   123  			PosterID: org3.ID,
   124  			Poster:   org3,
   125  			IsPull:   true,
   126  		}
   127  		pullRequest := &issues_model.PullRequest{
   128  			HeadRepoID: forkedRepo.ID,
   129  			BaseRepoID: baseRepo.ID,
   130  			HeadBranch: "fork-branch-1",
   131  			BaseBranch: "main",
   132  			HeadRepo:   forkedRepo,
   133  			BaseRepo:   baseRepo,
   134  			Type:       issues_model.PullRequestGitea,
   135  		}
   136  		err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
   137  		assert.NoError(t, err)
   138  
   139  		// load and compare ActionRun
   140  		assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
   141  		actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID})
   142  		assert.Equal(t, addFileToForkedResp.Commit.SHA, actionRun.CommitSHA)
   143  		assert.Equal(t, actions_module.GithubEventPullRequestTarget, actionRun.TriggerEvent)
   144  
   145  		// add another file whose name cannot match the specified path
   146  		addFileToForkedResp, err = files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, org3, &files_service.ChangeRepoFilesOptions{
   147  			Files: []*files_service.ChangeRepoFile{
   148  				{
   149  					Operation:     "create",
   150  					TreePath:      "foo.txt",
   151  					ContentReader: strings.NewReader("foo"),
   152  				},
   153  			},
   154  			Message:   "add foo.txt",
   155  			OldBranch: "main",
   156  			NewBranch: "fork-branch-2",
   157  			Author: &files_service.IdentityOptions{
   158  				Name:  org3.Name,
   159  				Email: org3.Email,
   160  			},
   161  			Committer: &files_service.IdentityOptions{
   162  				Name:  org3.Name,
   163  				Email: org3.Email,
   164  			},
   165  			Dates: &files_service.CommitDateOptions{
   166  				Author:    time.Now(),
   167  				Committer: time.Now(),
   168  			},
   169  		})
   170  		assert.NoError(t, err)
   171  		assert.NotEmpty(t, addFileToForkedResp)
   172  
   173  		// create Pull
   174  		pullIssue = &issues_model.Issue{
   175  			RepoID:   baseRepo.ID,
   176  			Title:    "A mismatched path cannot trigger pull-request-target-event",
   177  			PosterID: org3.ID,
   178  			Poster:   org3,
   179  			IsPull:   true,
   180  		}
   181  		pullRequest = &issues_model.PullRequest{
   182  			HeadRepoID: forkedRepo.ID,
   183  			BaseRepoID: baseRepo.ID,
   184  			HeadBranch: "fork-branch-2",
   185  			BaseBranch: "main",
   186  			HeadRepo:   forkedRepo,
   187  			BaseRepo:   baseRepo,
   188  			Type:       issues_model.PullRequestGitea,
   189  		}
   190  		err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
   191  		assert.NoError(t, err)
   192  
   193  		// the new pull request cannot trigger actions, so there is still only 1 record
   194  		assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
   195  	})
   196  }