|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/models" |
| 14 | + "code.gitea.io/gitea/modules/repofiles" |
| 15 | + repo_module "code.gitea.io/gitea/modules/repository" |
| 16 | + pull_service "code.gitea.io/gitea/services/pull" |
| 17 | + repo_service "code.gitea.io/gitea/services/repository" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | +) |
| 21 | + |
| 22 | +func TestPullUpdate(t *testing.T) { |
| 23 | + onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { |
| 24 | + //Create PR to test |
| 25 | + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) |
| 26 | + org26 := models.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User) |
| 27 | + pr := createOutdatedPR(t, user, org26) |
| 28 | + |
| 29 | + //Test GetDiverging |
| 30 | + diffCount, err := pull_service.GetDiverging(pr) |
| 31 | + assert.NoError(t, err) |
| 32 | + assert.EqualValues(t, 1, diffCount.Behind) |
| 33 | + assert.EqualValues(t, 1, diffCount.Ahead) |
| 34 | + |
| 35 | + message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch) |
| 36 | + err = pull_service.Update(pr, user, message) |
| 37 | + assert.NoError(t, err) |
| 38 | + |
| 39 | + //Test GetDiverging after update |
| 40 | + diffCount, err = pull_service.GetDiverging(pr) |
| 41 | + assert.NoError(t, err) |
| 42 | + assert.EqualValues(t, 0, diffCount.Behind) |
| 43 | + assert.EqualValues(t, 2, diffCount.Ahead) |
| 44 | + |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullRequest { |
| 49 | + baseRepo, err := repo_service.CreateRepository(actor, actor, models.CreateRepoOptions{ |
| 50 | + Name: "repo-pr-update", |
| 51 | + Description: "repo-tmp-pr-update description", |
| 52 | + AutoInit: true, |
| 53 | + Gitignores: "C,C++", |
| 54 | + License: "MIT", |
| 55 | + Readme: "Default", |
| 56 | + IsPrivate: false, |
| 57 | + }) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.NotEmpty(t, baseRepo) |
| 60 | + |
| 61 | + headRepo, err := repo_module.ForkRepository(actor, forkOrg, baseRepo, "repo-pr-update", "desc") |
| 62 | + assert.NoError(t, err) |
| 63 | + assert.NotEmpty(t, headRepo) |
| 64 | + |
| 65 | + //create a commit on base Repo |
| 66 | + _, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{ |
| 67 | + TreePath: "File_A", |
| 68 | + Message: "Add File A", |
| 69 | + Content: "File A", |
| 70 | + IsNewFile: true, |
| 71 | + OldBranch: "master", |
| 72 | + NewBranch: "master", |
| 73 | + Author: &repofiles.IdentityOptions{ |
| 74 | + Name: actor.Name, |
| 75 | + Email: actor.Email, |
| 76 | + }, |
| 77 | + Committer: &repofiles.IdentityOptions{ |
| 78 | + Name: actor.Name, |
| 79 | + Email: actor.Email, |
| 80 | + }, |
| 81 | + Dates: &repofiles.CommitDateOptions{ |
| 82 | + Author: time.Now(), |
| 83 | + Committer: time.Now(), |
| 84 | + }, |
| 85 | + }) |
| 86 | + assert.NoError(t, err) |
| 87 | + |
| 88 | + //create a commit on head Repo |
| 89 | + _, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{ |
| 90 | + TreePath: "File_B", |
| 91 | + Message: "Add File on PR branch", |
| 92 | + Content: "File B", |
| 93 | + IsNewFile: true, |
| 94 | + OldBranch: "master", |
| 95 | + NewBranch: "newBranch", |
| 96 | + Author: &repofiles.IdentityOptions{ |
| 97 | + Name: actor.Name, |
| 98 | + Email: actor.Email, |
| 99 | + }, |
| 100 | + Committer: &repofiles.IdentityOptions{ |
| 101 | + Name: actor.Name, |
| 102 | + Email: actor.Email, |
| 103 | + }, |
| 104 | + Dates: &repofiles.CommitDateOptions{ |
| 105 | + Author: time.Now(), |
| 106 | + Committer: time.Now(), |
| 107 | + }, |
| 108 | + }) |
| 109 | + assert.NoError(t, err) |
| 110 | + |
| 111 | + //create Pull |
| 112 | + pullIssue := &models.Issue{ |
| 113 | + RepoID: baseRepo.ID, |
| 114 | + Title: "Test Pull -to-update-", |
| 115 | + PosterID: actor.ID, |
| 116 | + Poster: actor, |
| 117 | + IsPull: true, |
| 118 | + } |
| 119 | + pullRequest := &models.PullRequest{ |
| 120 | + HeadRepoID: headRepo.ID, |
| 121 | + BaseRepoID: baseRepo.ID, |
| 122 | + HeadBranch: "newBranch", |
| 123 | + BaseBranch: "master", |
| 124 | + HeadRepo: headRepo, |
| 125 | + BaseRepo: baseRepo, |
| 126 | + Type: models.PullRequestGitea, |
| 127 | + } |
| 128 | + err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil) |
| 129 | + assert.NoError(t, err) |
| 130 | + |
| 131 | + issue := models.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue) |
| 132 | + pr, err := models.GetPullRequestByIssueID(issue.ID) |
| 133 | + assert.NoError(t, err) |
| 134 | + |
| 135 | + return pr |
| 136 | +} |
0 commit comments