code-intelligence.com/cifuzz@v0.40.0/internal/api/project.go (about) 1 package api 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "net/url" 8 9 "github.com/pkg/errors" 10 ) 11 12 type ProjectBody struct { 13 Project Project `json:"project"` 14 } 15 16 type Project struct { 17 Name string `json:"name"` 18 DisplayName string `json:"display_name"` 19 OwnerOrganizationName string `json:"owner_organization_name,omitempty"` 20 } 21 22 type ProjectResponse struct { 23 Name string `json:"name"` 24 Done bool `json:"done"` 25 Response Response `json:"response"` 26 } 27 28 type Response struct { 29 Type string `json:"@type"` 30 Name string `json:"name"` 31 DisplayName string `json:"display_name"` 32 Location Location `json:"location"` 33 OwnerUsername string `json:"owner_username"` 34 } 35 36 type Location struct { 37 GitPath GitPath `json:"git_path"` 38 } 39 40 type GitPath struct{} 41 42 func (client *APIClient) ListProjects(token string) ([]*Project, error) { 43 url, err := url.JoinPath("/v1", "projects") 44 if err != nil { 45 return nil, err 46 } 47 resp, err := client.sendRequest("GET", url, nil, token) 48 if err != nil { 49 return nil, err 50 } 51 defer resp.Body.Close() 52 53 if resp.StatusCode != 200 { 54 return nil, responseToAPIError(resp) 55 } 56 57 body, err := io.ReadAll(resp.Body) 58 if err != nil { 59 return nil, errors.WithStack(err) 60 } 61 62 var objmap map[string]json.RawMessage 63 err = json.Unmarshal(body, &objmap) 64 if err != nil { 65 return nil, errors.WithStack(err) 66 } 67 var projects []*Project 68 // If the projects field is not present, it means there are no projects 69 // so we return an empty list of projects and no error. 70 if _, ok := objmap["projects"]; !ok { 71 return []*Project{}, nil 72 } 73 err = json.Unmarshal(objmap["projects"], &projects) 74 if err != nil { 75 return nil, errors.WithStack(err) 76 } 77 78 // Filter out featured projects 79 var filteredProjects []*Project 80 for _, p := range projects { 81 if p.OwnerOrganizationName == FeaturedProjectsOrganization { 82 continue 83 } 84 filteredProjects = append(filteredProjects, p) 85 } 86 87 return filteredProjects, nil 88 } 89 90 func (client *APIClient) CreateProject(name string, token string) (*Project, error) { 91 projectBody := &ProjectBody{ 92 Project: Project{ 93 DisplayName: name, 94 }, 95 } 96 97 body, err := json.Marshal(projectBody) 98 if err != nil { 99 return nil, errors.WithStack(err) 100 } 101 102 url, err := url.JoinPath("/v1", "projects") 103 if err != nil { 104 return nil, err 105 } 106 resp, err := client.sendRequest("POST", url, bytes.NewReader(body), token) 107 if err != nil { 108 return nil, err 109 } 110 defer resp.Body.Close() 111 112 if resp.StatusCode != 200 { 113 return nil, responseToAPIError(resp) 114 } 115 116 body, err = io.ReadAll(resp.Body) 117 if err != nil { 118 return nil, errors.WithStack(err) 119 } 120 121 var projectResponse ProjectResponse 122 err = json.Unmarshal(body, &projectResponse) 123 if err != nil { 124 return nil, errors.WithStack(err) 125 } 126 127 projectBody.Project.Name = projectResponse.Response.Name 128 129 return &projectBody.Project, nil 130 }