|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "awesome-ci/src/acigithub" |
| 5 | + "awesome-ci/src/tools" |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/go-git/go-git/v5" |
| 15 | + "github.com/go-git/go-git/v5/plumbing" |
| 16 | + "github.com/go-git/go-git/v5/plumbing/transport/http" |
| 17 | + "github.com/google/go-github/v39/github" |
| 18 | +) |
| 19 | + |
| 20 | +type TestEnvironment struct { |
| 21 | + ctx context.Context |
| 22 | + testOwner, testRepo string |
| 23 | + repoPath string |
| 24 | + githubRepo *github.Repository |
| 25 | + localGitRep *git.Repository |
| 26 | +} |
| 27 | + |
| 28 | +func getTestEnvironment(preparedReleases *[]string, t *testing.T) (testEnv *TestEnvironment, cleanup func()) { |
| 29 | + |
| 30 | + testRepo, ok := os.LookupEnv("ACI_TEST_REPO") |
| 31 | + |
| 32 | + if !ok { |
| 33 | + t.Errorf("env var ACI_TEST_REPO not set") |
| 34 | + t.FailNow() |
| 35 | + } |
| 36 | + |
| 37 | + if !ok { |
| 38 | + t.Errorf("env var ACI_TEST_FORK not set") |
| 39 | + t.FailNow() |
| 40 | + } |
| 41 | + |
| 42 | + testEnv = &TestEnvironment{ |
| 43 | + ctx: context.Background(), |
| 44 | + testOwner: strings.Split(testRepo, "/")[0], |
| 45 | + testRepo: strings.Split(testRepo, "/")[1], |
| 46 | + } |
| 47 | + |
| 48 | + if tmpDir, err := ioutil.TempDir("/tmp", "aci"); err == nil { |
| 49 | + testEnv.repoPath = tmpDir |
| 50 | + } else { |
| 51 | + t.Errorf("Could not create tmp dir: %s", err) |
| 52 | + t.FailNow() |
| 53 | + } |
| 54 | + |
| 55 | + if _, err := acigithub.NewGitHubClient(); err != nil { |
| 56 | + t.Errorf("Could not create GitHub client: %s", err) |
| 57 | + t.FailNow() |
| 58 | + } |
| 59 | + |
| 60 | + repo, _, err := acigithub.GithubClient.Repositories.Get(testEnv.ctx, testEnv.testOwner, testEnv.testRepo) |
| 61 | + |
| 62 | + if checkError(err, t) { |
| 63 | + t.FailNow() |
| 64 | + } |
| 65 | + |
| 66 | + testEnv.githubRepo = repo |
| 67 | + |
| 68 | + gitRepo, err := git.PlainClone(testEnv.repoPath, false, &git.CloneOptions{ |
| 69 | + URL: *repo.CloneURL, |
| 70 | + Auth: &http.BasicAuth{ |
| 71 | + Username: "notneeded", // yes, this can be anything except an empty string |
| 72 | + Password: os.Getenv("GITHUB_TOKEN"), |
| 73 | + }, |
| 74 | + Progress: os.Stdout, |
| 75 | + }) |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + t.Errorf("Could not clone Repository: %s", err) |
| 79 | + t.FailNow() |
| 80 | + } |
| 81 | + |
| 82 | + testEnv.localGitRep = gitRepo |
| 83 | + |
| 84 | + os.Chdir(testEnv.repoPath) |
| 85 | + |
| 86 | + cleanupReleases, err := prepareReleases(preparedReleases, testEnv, t) |
| 87 | + |
| 88 | + checkError(err, t) |
| 89 | + |
| 90 | + return testEnv, func() { |
| 91 | + cleanupReleases() |
| 92 | + os.RemoveAll(testEnv.repoPath) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func prepareReleases(tagNames *[]string, testEnv *TestEnvironment, t *testing.T) (cleanup func(), err error) { |
| 97 | + _, tagToCommitMap, err := tools.GetGitTagMaps(testEnv.localGitRep) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return func() {}, err |
| 101 | + } |
| 102 | + |
| 103 | + for _, tagName := range *tagNames { |
| 104 | + |
| 105 | + commit := tagToCommitMap[tagName] |
| 106 | + _, _, err := acigithub.GithubClient.Repositories.CreateRelease(testEnv.ctx, testEnv.testOwner, testEnv.testRepo, &github.RepositoryRelease{ |
| 107 | + TagName: &tagName, |
| 108 | + TargetCommitish: &commit, |
| 109 | + Name: &tagName, |
| 110 | + }) |
| 111 | + |
| 112 | + waitingForRelease(tagName, testEnv, t) |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + return func() {}, err |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return func() { |
| 120 | + deleteReleases(tagNames, testEnv, t) |
| 121 | + }, nil |
| 122 | +} |
| 123 | + |
| 124 | +func waitingForRelease(tagName string, testEnv *TestEnvironment, t *testing.T) { |
| 125 | + |
| 126 | + for i := 1; i <= 10; i++ { //waiting for release becoming available |
| 127 | + _, _, err := acigithub.GithubClient.Repositories.GetReleaseByTag(testEnv.ctx, testEnv.testOwner, testEnv.testRepo, tagName) |
| 128 | + if err != nil { |
| 129 | + fmt.Printf("Waiting %sms for Release to become available", time.Duration(i*100)*time.Millisecond) |
| 130 | + |
| 131 | + time.Sleep(time.Duration(i*100) * time.Millisecond) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func deleteReleases(tagNames *[]string, testConfig *TestEnvironment, t *testing.T) { |
| 137 | + |
| 138 | + for _, tagName := range *tagNames { |
| 139 | + |
| 140 | + release, _, err := acigithub.GithubClient.Repositories.GetReleaseByTag(testConfig.ctx, testConfig.testOwner, testConfig.testRepo, tagName) |
| 141 | + |
| 142 | + if !checkError(err, t) { |
| 143 | + _, err = acigithub.GithubClient.Repositories.DeleteRelease(testConfig.ctx, testConfig.testOwner, testConfig.testRepo, *release.ID) |
| 144 | + checkError(err, t) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | +func resetHeadToTag(tagName string, testEnv *TestEnvironment, t *testing.T) bool { |
| 151 | + _, tagToCommitMap, err := tools.GetGitTagMaps(testEnv.localGitRep) |
| 152 | + if checkError(err, t) { |
| 153 | + return false |
| 154 | + } |
| 155 | + worktree, err := testEnv.localGitRep.Worktree() |
| 156 | + |
| 157 | + if checkError(err, t) { |
| 158 | + return false |
| 159 | + } |
| 160 | + |
| 161 | + worktree.Reset(&git.ResetOptions{ |
| 162 | + Commit: plumbing.NewHash(tagToCommitMap["1.1.0"]), |
| 163 | + Mode: git.HardReset, |
| 164 | + }) |
| 165 | + return true |
| 166 | +} |
| 167 | + |
| 168 | +func checkError(err error, t *testing.T) bool { |
| 169 | + |
| 170 | + if err != nil { |
| 171 | + t.Error(err) |
| 172 | + return true |
| 173 | + } |
| 174 | + |
| 175 | + return false |
| 176 | +} |
0 commit comments