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

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // Copyright 2018 Jonas Franz. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package migrations
     6  
     7  import (
     8  	"context"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	base "code.gitea.io/gitea/modules/migration"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestGitHubDownloadRepo(t *testing.T) {
    19  	GithubLimitRateRemaining = 3 // Wait at 3 remaining since we could have 3 CI in //
    20  	token := os.Getenv("GITHUB_READ_TOKEN")
    21  	if token == "" {
    22  		t.Skip("Skipping GitHub migration test because GITHUB_READ_TOKEN is empty")
    23  	}
    24  	downloader := NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", token, "go-gitea", "test_repo")
    25  	err := downloader.RefreshRate()
    26  	assert.NoError(t, err)
    27  
    28  	repo, err := downloader.GetRepoInfo()
    29  	assert.NoError(t, err)
    30  	assertRepositoryEqual(t, &base.Repository{
    31  		Name:          "test_repo",
    32  		Owner:         "go-gitea",
    33  		Description:   "Test repository for testing migration from github to gitea",
    34  		CloneURL:      "https://github.com/go-gitea/test_repo.git",
    35  		OriginalURL:   "https://github.com/go-gitea/test_repo",
    36  		DefaultBranch: "master",
    37  	}, repo)
    38  
    39  	topics, err := downloader.GetTopics()
    40  	assert.NoError(t, err)
    41  	assert.Contains(t, topics, "gitea")
    42  
    43  	milestones, err := downloader.GetMilestones()
    44  	assert.NoError(t, err)
    45  	assertMilestonesEqual(t, []*base.Milestone{
    46  		{
    47  			Title:       "1.0.0",
    48  			Description: "Milestone 1.0.0",
    49  			Deadline:    timePtr(time.Date(2019, 11, 11, 8, 0, 0, 0, time.UTC)),
    50  			Created:     time.Date(2019, 11, 12, 19, 37, 8, 0, time.UTC),
    51  			Updated:     timePtr(time.Date(2019, 11, 12, 21, 56, 17, 0, time.UTC)),
    52  			Closed:      timePtr(time.Date(2019, 11, 12, 19, 45, 49, 0, time.UTC)),
    53  			State:       "closed",
    54  		},
    55  		{
    56  			Title:       "1.1.0",
    57  			Description: "Milestone 1.1.0",
    58  			Deadline:    timePtr(time.Date(2019, 11, 12, 8, 0, 0, 0, time.UTC)),
    59  			Created:     time.Date(2019, 11, 12, 19, 37, 25, 0, time.UTC),
    60  			Updated:     timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
    61  			Closed:      timePtr(time.Date(2019, 11, 12, 19, 45, 46, 0, time.UTC)),
    62  			State:       "closed",
    63  		},
    64  	}, milestones)
    65  
    66  	labels, err := downloader.GetLabels()
    67  	assert.NoError(t, err)
    68  	assertLabelsEqual(t, []*base.Label{
    69  		{
    70  			Name:        "bug",
    71  			Color:       "d73a4a",
    72  			Description: "Something isn't working",
    73  		},
    74  		{
    75  			Name:        "documentation",
    76  			Color:       "0075ca",
    77  			Description: "Improvements or additions to documentation",
    78  		},
    79  		{
    80  			Name:        "duplicate",
    81  			Color:       "cfd3d7",
    82  			Description: "This issue or pull request already exists",
    83  		},
    84  		{
    85  			Name:        "enhancement",
    86  			Color:       "a2eeef",
    87  			Description: "New feature or request",
    88  		},
    89  		{
    90  			Name:        "good first issue",
    91  			Color:       "7057ff",
    92  			Description: "Good for newcomers",
    93  		},
    94  		{
    95  			Name:        "help wanted",
    96  			Color:       "008672",
    97  			Description: "Extra attention is needed",
    98  		},
    99  		{
   100  			Name:        "invalid",
   101  			Color:       "e4e669",
   102  			Description: "This doesn't seem right",
   103  		},
   104  		{
   105  			Name:        "question",
   106  			Color:       "d876e3",
   107  			Description: "Further information is requested",
   108  		},
   109  		{
   110  			Name:        "wontfix",
   111  			Color:       "ffffff",
   112  			Description: "This will not be worked on",
   113  		},
   114  	}, labels)
   115  
   116  	releases, err := downloader.GetReleases()
   117  	assert.NoError(t, err)
   118  	assertReleasesEqual(t, []*base.Release{
   119  		{
   120  			TagName:         "v0.9.99",
   121  			TargetCommitish: "master",
   122  			Name:            "First Release",
   123  			Body:            "A test release",
   124  			Created:         time.Date(2019, 11, 9, 16, 49, 21, 0, time.UTC),
   125  			Published:       time.Date(2019, 11, 12, 20, 12, 10, 0, time.UTC),
   126  			PublisherID:     1669571,
   127  			PublisherName:   "mrsdizzie",
   128  		},
   129  	}, releases)
   130  
   131  	// downloader.GetIssues()
   132  	issues, isEnd, err := downloader.GetIssues(1, 2)
   133  	assert.NoError(t, err)
   134  	assert.False(t, isEnd)
   135  	assertIssuesEqual(t, []*base.Issue{
   136  		{
   137  			Number:     1,
   138  			Title:      "Please add an animated gif icon to the merge button",
   139  			Content:    "I just want the merge button to hurt my eyes a little. \xF0\x9F\x98\x9D ",
   140  			Milestone:  "1.0.0",
   141  			PosterID:   18600385,
   142  			PosterName: "guillep2k",
   143  			State:      "closed",
   144  			Created:    time.Date(2019, 11, 9, 17, 0, 29, 0, time.UTC),
   145  			Updated:    time.Date(2019, 11, 12, 20, 29, 53, 0, time.UTC),
   146  			Labels: []*base.Label{
   147  				{
   148  					Name:        "bug",
   149  					Color:       "d73a4a",
   150  					Description: "Something isn't working",
   151  				},
   152  				{
   153  					Name:        "good first issue",
   154  					Color:       "7057ff",
   155  					Description: "Good for newcomers",
   156  				},
   157  			},
   158  			Reactions: []*base.Reaction{
   159  				{
   160  					UserID:   1669571,
   161  					UserName: "mrsdizzie",
   162  					Content:  "+1",
   163  				},
   164  			},
   165  			Closed: timePtr(time.Date(2019, 11, 12, 20, 22, 22, 0, time.UTC)),
   166  		},
   167  		{
   168  			Number:     2,
   169  			Title:      "Test issue",
   170  			Content:    "This is test issue 2, do not touch!",
   171  			Milestone:  "1.1.0",
   172  			PosterID:   1669571,
   173  			PosterName: "mrsdizzie",
   174  			State:      "closed",
   175  			Created:    time.Date(2019, 11, 12, 21, 0, 6, 0, time.UTC),
   176  			Updated:    time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
   177  			Labels: []*base.Label{
   178  				{
   179  					Name:        "duplicate",
   180  					Color:       "cfd3d7",
   181  					Description: "This issue or pull request already exists",
   182  				},
   183  			},
   184  			Reactions: []*base.Reaction{
   185  				{
   186  					UserID:   1669571,
   187  					UserName: "mrsdizzie",
   188  					Content:  "heart",
   189  				},
   190  				{
   191  					UserID:   1669571,
   192  					UserName: "mrsdizzie",
   193  					Content:  "laugh",
   194  				},
   195  				{
   196  					UserID:   1669571,
   197  					UserName: "mrsdizzie",
   198  					Content:  "-1",
   199  				},
   200  				{
   201  					UserID:   1669571,
   202  					UserName: "mrsdizzie",
   203  					Content:  "confused",
   204  				},
   205  				{
   206  					UserID:   1669571,
   207  					UserName: "mrsdizzie",
   208  					Content:  "hooray",
   209  				},
   210  				{
   211  					UserID:   1669571,
   212  					UserName: "mrsdizzie",
   213  					Content:  "+1",
   214  				},
   215  			},
   216  			Closed: timePtr(time.Date(2019, 11, 12, 21, 1, 31, 0, time.UTC)),
   217  		},
   218  	}, issues)
   219  
   220  	// downloader.GetComments()
   221  	comments, _, err := downloader.GetComments(&base.Issue{Number: 2, ForeignIndex: 2})
   222  	assert.NoError(t, err)
   223  	assertCommentsEqual(t, []*base.Comment{
   224  		{
   225  			IssueIndex: 2,
   226  			PosterID:   1669571,
   227  			PosterName: "mrsdizzie",
   228  			Created:    time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
   229  			Updated:    time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
   230  			Content:    "This is a comment",
   231  			Reactions: []*base.Reaction{
   232  				{
   233  					UserID:   1669571,
   234  					UserName: "mrsdizzie",
   235  					Content:  "+1",
   236  				},
   237  			},
   238  		},
   239  		{
   240  			IssueIndex: 2,
   241  			PosterID:   1669571,
   242  			PosterName: "mrsdizzie",
   243  			Created:    time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
   244  			Updated:    time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
   245  			Content:    "A second comment",
   246  			Reactions:  nil,
   247  		},
   248  	}, comments)
   249  
   250  	// downloader.GetPullRequests()
   251  	prs, _, err := downloader.GetPullRequests(1, 2)
   252  	assert.NoError(t, err)
   253  	assertPullRequestsEqual(t, []*base.PullRequest{
   254  		{
   255  			Number:     3,
   256  			Title:      "Update README.md",
   257  			Content:    "add warning to readme",
   258  			Milestone:  "1.1.0",
   259  			PosterID:   1669571,
   260  			PosterName: "mrsdizzie",
   261  			State:      "closed",
   262  			Created:    time.Date(2019, 11, 12, 21, 21, 43, 0, time.UTC),
   263  			Updated:    time.Date(2019, 11, 12, 21, 39, 28, 0, time.UTC),
   264  			Labels: []*base.Label{
   265  				{
   266  					Name:        "documentation",
   267  					Color:       "0075ca",
   268  					Description: "Improvements or additions to documentation",
   269  				},
   270  			},
   271  			PatchURL: "https://github.com/go-gitea/test_repo/pull/3.patch",
   272  			Head: base.PullRequestBranch{
   273  				Ref:      "master",
   274  				CloneURL: "https://github.com/mrsdizzie/test_repo.git",
   275  				SHA:      "076160cf0b039f13e5eff19619932d181269414b",
   276  				RepoName: "test_repo",
   277  
   278  				OwnerName: "mrsdizzie",
   279  			},
   280  			Base: base.PullRequestBranch{
   281  				Ref:       "master",
   282  				SHA:       "72866af952e98d02a73003501836074b286a78f6",
   283  				OwnerName: "go-gitea",
   284  				RepoName:  "test_repo",
   285  			},
   286  			Closed:         timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
   287  			Merged:         true,
   288  			MergedTime:     timePtr(time.Date(2019, 11, 12, 21, 39, 27, 0, time.UTC)),
   289  			MergeCommitSHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
   290  			ForeignIndex:   3,
   291  		},
   292  		{
   293  			Number:     4,
   294  			Title:      "Test branch",
   295  			Content:    "do not merge this PR",
   296  			Milestone:  "1.0.0",
   297  			PosterID:   1669571,
   298  			PosterName: "mrsdizzie",
   299  			State:      "open",
   300  			Created:    time.Date(2019, 11, 12, 21, 54, 18, 0, time.UTC),
   301  			Updated:    time.Date(2020, 1, 4, 11, 30, 1, 0, time.UTC),
   302  			Labels: []*base.Label{
   303  				{
   304  					Name:        "bug",
   305  					Color:       "d73a4a",
   306  					Description: "Something isn't working",
   307  				},
   308  			},
   309  			PatchURL: "https://github.com/go-gitea/test_repo/pull/4.patch",
   310  			Head: base.PullRequestBranch{
   311  				Ref:       "test-branch",
   312  				SHA:       "2be9101c543658591222acbee3eb799edfc3853d",
   313  				RepoName:  "test_repo",
   314  				OwnerName: "mrsdizzie",
   315  				CloneURL:  "https://github.com/mrsdizzie/test_repo.git",
   316  			},
   317  			Base: base.PullRequestBranch{
   318  				Ref:       "master",
   319  				SHA:       "f32b0a9dfd09a60f616f29158f772cedd89942d2",
   320  				OwnerName: "go-gitea",
   321  				RepoName:  "test_repo",
   322  			},
   323  			Merged:         false,
   324  			MergeCommitSHA: "565d1208f5fffdc1c5ae1a2436491eb9a5e4ebae",
   325  			Reactions: []*base.Reaction{
   326  				{
   327  					UserID:   81045,
   328  					UserName: "lunny",
   329  					Content:  "heart",
   330  				},
   331  				{
   332  					UserID:   81045,
   333  					UserName: "lunny",
   334  					Content:  "+1",
   335  				},
   336  			},
   337  			ForeignIndex: 4,
   338  		},
   339  	}, prs)
   340  
   341  	reviews, err := downloader.GetReviews(&base.PullRequest{Number: 3, ForeignIndex: 3})
   342  	assert.NoError(t, err)
   343  	assertReviewsEqual(t, []*base.Review{
   344  		{
   345  			ID:           315859956,
   346  			IssueIndex:   3,
   347  			ReviewerID:   42128690,
   348  			ReviewerName: "jolheiser",
   349  			CommitID:     "076160cf0b039f13e5eff19619932d181269414b",
   350  			CreatedAt:    time.Date(2019, 11, 12, 21, 35, 24, 0, time.UTC),
   351  			State:        base.ReviewStateApproved,
   352  		},
   353  		{
   354  			ID:           315860062,
   355  			IssueIndex:   3,
   356  			ReviewerID:   1824502,
   357  			ReviewerName: "zeripath",
   358  			CommitID:     "076160cf0b039f13e5eff19619932d181269414b",
   359  			CreatedAt:    time.Date(2019, 11, 12, 21, 35, 36, 0, time.UTC),
   360  			State:        base.ReviewStateApproved,
   361  		},
   362  		{
   363  			ID:           315861440,
   364  			IssueIndex:   3,
   365  			ReviewerID:   165205,
   366  			ReviewerName: "lafriks",
   367  			CommitID:     "076160cf0b039f13e5eff19619932d181269414b",
   368  			CreatedAt:    time.Date(2019, 11, 12, 21, 38, 0, 0, time.UTC),
   369  			State:        base.ReviewStateApproved,
   370  		},
   371  	}, reviews)
   372  
   373  	reviews, err = downloader.GetReviews(&base.PullRequest{Number: 4, ForeignIndex: 4})
   374  	assert.NoError(t, err)
   375  	assertReviewsEqual(t, []*base.Review{
   376  		{
   377  			ID:           338338740,
   378  			IssueIndex:   4,
   379  			ReviewerID:   81045,
   380  			ReviewerName: "lunny",
   381  			CommitID:     "2be9101c543658591222acbee3eb799edfc3853d",
   382  			CreatedAt:    time.Date(2020, 1, 4, 5, 33, 18, 0, time.UTC),
   383  			State:        base.ReviewStateApproved,
   384  			Comments: []*base.ReviewComment{
   385  				{
   386  					ID:        363017488,
   387  					Content:   "This is a good pull request.",
   388  					TreePath:  "README.md",
   389  					DiffHunk:  "@@ -1,2 +1,4 @@\n # test_repo\n Test repository for testing migration from github to gitea\n+",
   390  					Position:  3,
   391  					CommitID:  "2be9101c543658591222acbee3eb799edfc3853d",
   392  					PosterID:  81045,
   393  					CreatedAt: time.Date(2020, 1, 4, 5, 33, 6, 0, time.UTC),
   394  					UpdatedAt: time.Date(2020, 1, 4, 5, 33, 18, 0, time.UTC),
   395  				},
   396  			},
   397  		},
   398  		{
   399  			ID:           338339651,
   400  			IssueIndex:   4,
   401  			ReviewerID:   81045,
   402  			ReviewerName: "lunny",
   403  			CommitID:     "2be9101c543658591222acbee3eb799edfc3853d",
   404  			CreatedAt:    time.Date(2020, 1, 4, 6, 7, 6, 0, time.UTC),
   405  			State:        base.ReviewStateChangesRequested,
   406  			Content:      "Don't add more reviews",
   407  		},
   408  		{
   409  			ID:           338349019,
   410  			IssueIndex:   4,
   411  			ReviewerID:   81045,
   412  			ReviewerName: "lunny",
   413  			CommitID:     "2be9101c543658591222acbee3eb799edfc3853d",
   414  			CreatedAt:    time.Date(2020, 1, 4, 11, 21, 41, 0, time.UTC),
   415  			State:        base.ReviewStateCommented,
   416  			Comments: []*base.ReviewComment{
   417  				{
   418  					ID:        363029944,
   419  					Content:   "test a single comment.",
   420  					TreePath:  "LICENSE",
   421  					DiffHunk:  "@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n+",
   422  					Position:  4,
   423  					CommitID:  "2be9101c543658591222acbee3eb799edfc3853d",
   424  					PosterID:  81045,
   425  					CreatedAt: time.Date(2020, 1, 4, 11, 21, 41, 0, time.UTC),
   426  					UpdatedAt: time.Date(2020, 1, 4, 11, 21, 41, 0, time.UTC),
   427  				},
   428  			},
   429  		},
   430  	}, reviews)
   431  }