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

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	auth_model "code.gitea.io/gitea/models/auth"
    14  	"code.gitea.io/gitea/models/db"
    15  	issues_model "code.gitea.io/gitea/models/issues"
    16  	"code.gitea.io/gitea/models/unittest"
    17  	user_model "code.gitea.io/gitea/models/user"
    18  	"code.gitea.io/gitea/modules/git"
    19  	pull_service "code.gitea.io/gitea/services/pull"
    20  	repo_service "code.gitea.io/gitea/services/repository"
    21  	files_service "code.gitea.io/gitea/services/repository/files"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestAPIPullUpdate(t *testing.T) {
    27  	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
    28  		// Create PR to test
    29  		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    30  		org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
    31  		pr := createOutdatedPR(t, user, org26)
    32  
    33  		// Test GetDiverging
    34  		diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
    35  		assert.NoError(t, err)
    36  		assert.EqualValues(t, 1, diffCount.Behind)
    37  		assert.EqualValues(t, 1, diffCount.Ahead)
    38  		assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
    39  		assert.NoError(t, pr.LoadIssue(db.DefaultContext))
    40  
    41  		session := loginUser(t, "user2")
    42  		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
    43  		req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
    44  		session.MakeRequest(t, req, http.StatusOK)
    45  
    46  		// Test GetDiverging after update
    47  		diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
    48  		assert.NoError(t, err)
    49  		assert.EqualValues(t, 0, diffCount.Behind)
    50  		assert.EqualValues(t, 2, diffCount.Ahead)
    51  	})
    52  }
    53  
    54  func TestAPIPullUpdateByRebase(t *testing.T) {
    55  	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
    56  		// Create PR to test
    57  		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    58  		org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26})
    59  		pr := createOutdatedPR(t, user, org26)
    60  
    61  		// Test GetDiverging
    62  		diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
    63  		assert.NoError(t, err)
    64  		assert.EqualValues(t, 1, diffCount.Behind)
    65  		assert.EqualValues(t, 1, diffCount.Ahead)
    66  		assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
    67  		assert.NoError(t, pr.LoadIssue(db.DefaultContext))
    68  
    69  		session := loginUser(t, "user2")
    70  		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
    71  		req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?style=rebase&token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
    72  		session.MakeRequest(t, req, http.StatusOK)
    73  
    74  		// Test GetDiverging after update
    75  		diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
    76  		assert.NoError(t, err)
    77  		assert.EqualValues(t, 0, diffCount.Behind)
    78  		assert.EqualValues(t, 1, diffCount.Ahead)
    79  	})
    80  }
    81  
    82  func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *issues_model.PullRequest {
    83  	baseRepo, err := repo_service.CreateRepository(db.DefaultContext, actor, actor, repo_service.CreateRepoOptions{
    84  		Name:        "repo-pr-update",
    85  		Description: "repo-tmp-pr-update description",
    86  		AutoInit:    true,
    87  		Gitignores:  "C,C++",
    88  		License:     "MIT",
    89  		Readme:      "Default",
    90  		IsPrivate:   false,
    91  	})
    92  	assert.NoError(t, err)
    93  	assert.NotEmpty(t, baseRepo)
    94  
    95  	headRepo, err := repo_service.ForkRepository(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{
    96  		BaseRepo:    baseRepo,
    97  		Name:        "repo-pr-update",
    98  		Description: "desc",
    99  	})
   100  	assert.NoError(t, err)
   101  	assert.NotEmpty(t, headRepo)
   102  
   103  	// create a commit on base Repo
   104  	_, err = files_service.ChangeRepoFiles(git.DefaultContext, baseRepo, actor, &files_service.ChangeRepoFilesOptions{
   105  		Files: []*files_service.ChangeRepoFile{
   106  			{
   107  				Operation:     "create",
   108  				TreePath:      "File_A",
   109  				ContentReader: strings.NewReader("File A"),
   110  			},
   111  		},
   112  		Message:   "Add File A",
   113  		OldBranch: "master",
   114  		NewBranch: "master",
   115  		Author: &files_service.IdentityOptions{
   116  			Name:  actor.Name,
   117  			Email: actor.Email,
   118  		},
   119  		Committer: &files_service.IdentityOptions{
   120  			Name:  actor.Name,
   121  			Email: actor.Email,
   122  		},
   123  		Dates: &files_service.CommitDateOptions{
   124  			Author:    time.Now(),
   125  			Committer: time.Now(),
   126  		},
   127  	})
   128  	assert.NoError(t, err)
   129  
   130  	// create a commit on head Repo
   131  	_, err = files_service.ChangeRepoFiles(git.DefaultContext, headRepo, actor, &files_service.ChangeRepoFilesOptions{
   132  		Files: []*files_service.ChangeRepoFile{
   133  			{
   134  				Operation:     "create",
   135  				TreePath:      "File_B",
   136  				ContentReader: strings.NewReader("File B"),
   137  			},
   138  		},
   139  		Message:   "Add File on PR branch",
   140  		OldBranch: "master",
   141  		NewBranch: "newBranch",
   142  		Author: &files_service.IdentityOptions{
   143  			Name:  actor.Name,
   144  			Email: actor.Email,
   145  		},
   146  		Committer: &files_service.IdentityOptions{
   147  			Name:  actor.Name,
   148  			Email: actor.Email,
   149  		},
   150  		Dates: &files_service.CommitDateOptions{
   151  			Author:    time.Now(),
   152  			Committer: time.Now(),
   153  		},
   154  	})
   155  	assert.NoError(t, err)
   156  
   157  	// create Pull
   158  	pullIssue := &issues_model.Issue{
   159  		RepoID:   baseRepo.ID,
   160  		Title:    "Test Pull -to-update-",
   161  		PosterID: actor.ID,
   162  		Poster:   actor,
   163  		IsPull:   true,
   164  	}
   165  	pullRequest := &issues_model.PullRequest{
   166  		HeadRepoID: headRepo.ID,
   167  		BaseRepoID: baseRepo.ID,
   168  		HeadBranch: "newBranch",
   169  		BaseBranch: "master",
   170  		HeadRepo:   headRepo,
   171  		BaseRepo:   baseRepo,
   172  		Type:       issues_model.PullRequestGitea,
   173  	}
   174  	err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
   175  	assert.NoError(t, err)
   176  
   177  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: "Test Pull -to-update-"})
   178  	pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, issue.ID)
   179  	assert.NoError(t, err)
   180  
   181  	return pr
   182  }