code.gitea.io/gitea@v1.21.7/services/migrations/gitea_downloader_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package migrations
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  	"os"
    10  	"sort"
    11  	"testing"
    12  	"time"
    13  
    14  	base "code.gitea.io/gitea/modules/migration"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestGiteaDownloadRepo(t *testing.T) {
    20  	// Skip tests if Gitea token is not found
    21  	giteaToken := os.Getenv("GITEA_TOKEN")
    22  	if giteaToken == "" {
    23  		t.Skip("skipped test because GITEA_TOKEN was not in the environment")
    24  	}
    25  
    26  	resp, err := http.Get("https://gitea.com/gitea")
    27  	if err != nil || resp.StatusCode != http.StatusOK {
    28  		t.Skipf("Can't reach https://gitea.com, skipping %s", t.Name())
    29  	}
    30  
    31  	downloader, err := NewGiteaDownloader(context.Background(), "https://gitea.com", "gitea/test_repo", "", "", giteaToken)
    32  	if downloader == nil {
    33  		t.Fatal("NewGitlabDownloader is nil")
    34  	}
    35  	if !assert.NoError(t, err) {
    36  		t.Fatal("NewGitlabDownloader error occur")
    37  	}
    38  
    39  	repo, err := downloader.GetRepoInfo()
    40  	assert.NoError(t, err)
    41  	assertRepositoryEqual(t, &base.Repository{
    42  		Name:          "test_repo",
    43  		Owner:         "gitea",
    44  		IsPrivate:     false,
    45  		Description:   "Test repository for testing migration from gitea to gitea",
    46  		CloneURL:      "https://gitea.com/gitea/test_repo.git",
    47  		OriginalURL:   "https://gitea.com/gitea/test_repo",
    48  		DefaultBranch: "master",
    49  	}, repo)
    50  
    51  	topics, err := downloader.GetTopics()
    52  	assert.NoError(t, err)
    53  	sort.Strings(topics)
    54  	assert.EqualValues(t, []string{"ci", "gitea", "migration", "test"}, topics)
    55  
    56  	labels, err := downloader.GetLabels()
    57  	assert.NoError(t, err)
    58  	assertLabelsEqual(t, []*base.Label{
    59  		{
    60  			Name:  "Bug",
    61  			Color: "e11d21",
    62  		},
    63  		{
    64  			Name:  "Enhancement",
    65  			Color: "207de5",
    66  		},
    67  		{
    68  			Name:        "Feature",
    69  			Color:       "0052cc",
    70  			Description: "a feature request",
    71  		},
    72  		{
    73  			Name:  "Invalid",
    74  			Color: "d4c5f9",
    75  		},
    76  		{
    77  			Name:  "Question",
    78  			Color: "fbca04",
    79  		},
    80  		{
    81  			Name:  "Valid",
    82  			Color: "53e917",
    83  		},
    84  	}, labels)
    85  
    86  	milestones, err := downloader.GetMilestones()
    87  	assert.NoError(t, err)
    88  	assertMilestonesEqual(t, []*base.Milestone{
    89  		{
    90  			Title:    "V2 Finalize",
    91  			Created:  time.Unix(0, 0),
    92  			Deadline: timePtr(time.Unix(1599263999, 0)),
    93  			Updated:  timePtr(time.Unix(0, 0)),
    94  			State:    "open",
    95  		},
    96  		{
    97  			Title:       "V1",
    98  			Description: "Generate Content",
    99  			Created:     time.Unix(0, 0),
   100  			Updated:     timePtr(time.Unix(0, 0)),
   101  			Closed:      timePtr(time.Unix(1598985406, 0)),
   102  			State:       "closed",
   103  		},
   104  	}, milestones)
   105  
   106  	releases, err := downloader.GetReleases()
   107  	assert.NoError(t, err)
   108  	assertReleasesEqual(t, []*base.Release{
   109  		{
   110  			Name:            "Second Release",
   111  			TagName:         "v2-rc1",
   112  			TargetCommitish: "master",
   113  			Body:            "this repo has:\r\n* reactions\r\n* wiki\r\n* issues  (open/closed)\r\n* pulls (open/closed/merged) (external/internal)\r\n* pull reviews\r\n* projects\r\n* milestones\r\n* labels\r\n* releases\r\n\r\nto test migration against",
   114  			Draft:           false,
   115  			Prerelease:      true,
   116  			Created:         time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC),
   117  			Published:       time.Date(2020, 9, 1, 18, 2, 43, 0, time.UTC),
   118  			PublisherID:     689,
   119  			PublisherName:   "6543",
   120  			PublisherEmail:  "6543@obermui.de",
   121  		},
   122  		{
   123  			Name:            "First Release",
   124  			TagName:         "V1",
   125  			TargetCommitish: "master",
   126  			Body:            "as title",
   127  			Draft:           false,
   128  			Prerelease:      false,
   129  			Created:         time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC),
   130  			Published:       time.Date(2020, 9, 1, 17, 30, 32, 0, time.UTC),
   131  			PublisherID:     689,
   132  			PublisherName:   "6543",
   133  			PublisherEmail:  "6543@obermui.de",
   134  		},
   135  	}, releases)
   136  
   137  	issues, isEnd, err := downloader.GetIssues(1, 50)
   138  	assert.NoError(t, err)
   139  	assert.True(t, isEnd)
   140  	assert.Len(t, issues, 7)
   141  	assert.EqualValues(t, "open", issues[0].State)
   142  
   143  	issues, isEnd, err = downloader.GetIssues(3, 2)
   144  	assert.NoError(t, err)
   145  	assert.False(t, isEnd)
   146  
   147  	assertIssuesEqual(t, []*base.Issue{
   148  		{
   149  			Number:      4,
   150  			Title:       "what is this repo about?",
   151  			Content:     "",
   152  			Milestone:   "V1",
   153  			PosterID:    -1,
   154  			PosterName:  "Ghost",
   155  			PosterEmail: "",
   156  			State:       "closed",
   157  			IsLocked:    true,
   158  			Created:     time.Unix(1598975321, 0),
   159  			Updated:     time.Unix(1598975400, 0),
   160  			Labels: []*base.Label{{
   161  				Name:        "Question",
   162  				Color:       "fbca04",
   163  				Description: "",
   164  			}},
   165  			Reactions: []*base.Reaction{
   166  				{
   167  					UserID:   689,
   168  					UserName: "6543",
   169  					Content:  "gitea",
   170  				},
   171  				{
   172  					UserID:   689,
   173  					UserName: "6543",
   174  					Content:  "laugh",
   175  				},
   176  			},
   177  			Closed: timePtr(time.Date(2020, 9, 1, 15, 49, 34, 0, time.UTC)),
   178  		},
   179  		{
   180  			Number:      2,
   181  			Title:       "Spam",
   182  			Content:     ":(",
   183  			Milestone:   "",
   184  			PosterID:    689,
   185  			PosterName:  "6543",
   186  			PosterEmail: "6543@obermui.de",
   187  			State:       "closed",
   188  			IsLocked:    false,
   189  			Created:     time.Unix(1598919780, 0),
   190  			Updated:     time.Unix(1598969497, 0),
   191  			Labels: []*base.Label{{
   192  				Name:        "Invalid",
   193  				Color:       "d4c5f9",
   194  				Description: "",
   195  			}},
   196  			Closed: timePtr(time.Unix(1598969497, 0)),
   197  		},
   198  	}, issues)
   199  
   200  	comments, _, err := downloader.GetComments(&base.Issue{Number: 4, ForeignIndex: 4})
   201  	assert.NoError(t, err)
   202  	assertCommentsEqual(t, []*base.Comment{
   203  		{
   204  			IssueIndex:  4,
   205  			PosterID:    689,
   206  			PosterName:  "6543",
   207  			PosterEmail: "6543@obermui.de",
   208  			Created:     time.Unix(1598975370, 0),
   209  			Updated:     time.Unix(1599070865, 0),
   210  			Content:     "a really good question!\n\nIt is the used as TESTSET for gitea2gitea repo migration function",
   211  		},
   212  		{
   213  			IssueIndex:  4,
   214  			PosterID:    -1,
   215  			PosterName:  "Ghost",
   216  			PosterEmail: "",
   217  			Created:     time.Unix(1598975393, 0),
   218  			Updated:     time.Unix(1598975393, 0),
   219  			Content:     "Oh!",
   220  		},
   221  	}, comments)
   222  
   223  	prs, isEnd, err := downloader.GetPullRequests(1, 50)
   224  	assert.NoError(t, err)
   225  	assert.True(t, isEnd)
   226  	assert.Len(t, prs, 6)
   227  	prs, isEnd, err = downloader.GetPullRequests(1, 3)
   228  	assert.NoError(t, err)
   229  	assert.False(t, isEnd)
   230  	assert.Len(t, prs, 3)
   231  	assertPullRequestEqual(t, &base.PullRequest{
   232  		Number:      12,
   233  		PosterID:    689,
   234  		PosterName:  "6543",
   235  		PosterEmail: "6543@obermui.de",
   236  		Title:       "Dont Touch",
   237  		Content:     "\r\nadd dont touch note",
   238  		Milestone:   "V2 Finalize",
   239  		State:       "closed",
   240  		IsLocked:    false,
   241  		Created:     time.Unix(1598982759, 0),
   242  		Updated:     time.Unix(1599023425, 0),
   243  		Closed:      timePtr(time.Unix(1598982934, 0)),
   244  		Assignees:   []string{"techknowlogick"},
   245  		Base: base.PullRequestBranch{
   246  			CloneURL:  "",
   247  			Ref:       "master",
   248  			SHA:       "827aa28a907853e5ddfa40c8f9bc52471a2685fd",
   249  			RepoName:  "test_repo",
   250  			OwnerName: "gitea",
   251  		},
   252  		Head: base.PullRequestBranch{
   253  			CloneURL:  "https://gitea.com/6543-forks/test_repo.git",
   254  			Ref:       "refs/pull/12/head",
   255  			SHA:       "b6ab5d9ae000b579a5fff03f92c486da4ddf48b6",
   256  			RepoName:  "test_repo",
   257  			OwnerName: "6543-forks",
   258  		},
   259  		Merged:         true,
   260  		MergedTime:     timePtr(time.Unix(1598982934, 0)),
   261  		MergeCommitSHA: "827aa28a907853e5ddfa40c8f9bc52471a2685fd",
   262  		PatchURL:       "https://gitea.com/gitea/test_repo/pulls/12.patch",
   263  	}, prs[1])
   264  
   265  	reviews, err := downloader.GetReviews(&base.Issue{Number: 7, ForeignIndex: 7})
   266  	assert.NoError(t, err)
   267  	assertReviewsEqual(t, []*base.Review{
   268  		{
   269  			ID:           1770,
   270  			IssueIndex:   7,
   271  			ReviewerID:   689,
   272  			ReviewerName: "6543",
   273  			CommitID:     "187ece0cb6631e2858a6872e5733433bb3ca3b03",
   274  			CreatedAt:    time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
   275  			State:        "COMMENT", // TODO
   276  			Comments: []*base.ReviewComment{
   277  				{
   278  					ID:        116561,
   279  					InReplyTo: 0,
   280  					Content:   "is one `\\newline` to less?",
   281  					TreePath:  "README.md",
   282  					DiffHunk:  "@@ -2,3 +2,3 @@\n \n-Test repository for testing migration from gitea 2 gitea\n\\ No newline at end of file\n+Test repository for testing migration from gitea 2 gitea",
   283  					Position:  0,
   284  					Line:      4,
   285  					CommitID:  "187ece0cb6631e2858a6872e5733433bb3ca3b03",
   286  					PosterID:  689,
   287  					Reactions: nil,
   288  					CreatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
   289  					UpdatedAt: time.Date(2020, 9, 1, 16, 12, 58, 0, time.UTC),
   290  				},
   291  			},
   292  		},
   293  		{
   294  			ID:           1771,
   295  			IssueIndex:   7,
   296  			ReviewerID:   9,
   297  			ReviewerName: "techknowlogick",
   298  			CommitID:     "187ece0cb6631e2858a6872e5733433bb3ca3b03",
   299  			CreatedAt:    time.Date(2020, 9, 1, 17, 6, 47, 0, time.UTC),
   300  			State:        "REQUEST_CHANGES", // TODO
   301  			Content:      "I think this needs some changes",
   302  		},
   303  		{
   304  			ID:           1772,
   305  			IssueIndex:   7,
   306  			ReviewerID:   9,
   307  			ReviewerName: "techknowlogick",
   308  			CommitID:     "187ece0cb6631e2858a6872e5733433bb3ca3b03",
   309  			CreatedAt:    time.Date(2020, 9, 1, 17, 19, 51, 0, time.UTC),
   310  			State:        base.ReviewStateApproved,
   311  			Official:     true,
   312  			Content:      "looks good",
   313  		},
   314  	}, reviews)
   315  }