//coverage:ignore package provider import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "code.gitea.io/sdk/gitea" ) var validTags = map[string]bool{ "v5.0.0": true, "5.0.0": true, } var ( server *httptest.Server zexlabUser = "owner" zexlabRepo = "test-repo" zexlabDefaultBranch = "master" zexlabCommits = []*gitea.Commit{ createGiteaCommit(testSHA, "fix: Removed lint as not go project\n", "2024-04-23T13:20:33+12:00"), createGiteaCommit(testSHA, "fix: Oops, need a tidyup\n", "2024-04-23T13:17:11+12:00"), createGiteaCommit(testSHA, "fix: Update CI\n", "2024-04-23T13:15:00+12:00"), } testSHA = "deadbeef" ) func CreateTestServer() *httptest.Server { ts := httptest.NewServer(http.HandlerFunc(ZexlabHandler)) return ts } //gocyclo:ignore func ZexlabHandler(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet && r.URL.Path == "/api/v1/version" { // Client performs a request to check version // Get json string from file data, _ := retrieveData("data/Version.json") _, _ = fmt.Fprint(w, string(data)) return } if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v1/repos/%s/%s", zexlabUser, zexlabRepo) { // Get json string from file data, _ := retrieveData("data/GetRepoInfo.json") _, _ = fmt.Fprint(w, string(data)) return } if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v1/repos/%s/%s/commits", zexlabUser, zexlabRepo) { // Get json string from file data, _ := retrieveData("data/GetCommits.json") _, _ = fmt.Fprint(w, string(data)) return } if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v1/repos/%s/%s/git/refs/", zexlabUser, zexlabRepo) { // Get json string from file data, _ := retrieveData("data/GetRefs.json") _, _ = fmt.Fprint(w, string(data)) return } if r.Method == http.MethodPost && r.URL.Path == fmt.Sprintf("/api/v1/repos/%s/%s/releases", zexlabUser, zexlabRepo) { var data map[string]string _ = json.NewDecoder(r.Body).Decode(&data) _ = r.Body.Close() if _, ok := validTags[data["tag_name"]]; !ok { http.Error(w, "invalid tag name", http.StatusBadRequest) return } fmt.Fprint(w, "{}") return } http.Error(w, "invalid route", http.StatusNotImplemented) } func ZexlabHandlerFailed(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet && r.URL.Path == "/api/v1/version" { // Client performs a request to check version // Get json string from file data, _ := retrieveData("data/Version.json") _, _ = fmt.Fprint(w, string(data)) return } if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v1/repos/%s/%s", zexlabUser, zexlabRepo) { // Get json string from file data, _ := retrieveData("data/GetRepoInfo.json") _, _ = fmt.Fprint(w, string(data)) return } http.Error(w, "invalid route", http.StatusNotImplemented) }