@Summary Create Repository @Description **Create a local repository** @Description @Description Distribution and component are used as defaults when publishing repo either directly or via snapshot. @Description @Description ``` @Description $ curl -X POST -H 'Content-Type: application/json' --data '
(c *gin.Context)
| 130 | // @Failure 500 {object} Error "Internal error" |
| 131 | // @Router /api/repos [post] |
| 132 | func apiReposCreate(c *gin.Context) { |
| 133 | var b repoCreateParams |
| 134 | |
| 135 | if c.Bind(&b) != nil { |
| 136 | return |
| 137 | } |
| 138 | |
| 139 | // Handler: Pre-task validations (shallow) |
| 140 | collectionFactory := context.NewCollectionFactory() |
| 141 | |
| 142 | var resources []string |
| 143 | if b.FromSnapshot != "" { |
| 144 | snapshot, err := collectionFactory.SnapshotCollection().ByName(b.FromSnapshot) |
| 145 | if err != nil { |
| 146 | AbortWithJSONError(c, http.StatusNotFound, fmt.Errorf("source snapshot not found: %s", err)) |
| 147 | return |
| 148 | } |
| 149 | resources = append(resources, string(snapshot.Key())) |
| 150 | } |
| 151 | |
| 152 | taskName := fmt.Sprintf("Create repository %s", b.Name) |
| 153 | |
| 154 | maybeRunTaskInBackground(c, taskName, resources, func(_ aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) { |
| 155 | // Task: Create fresh collection and check/create ATOMIC inside task |
| 156 | taskCollectionFactory := context.NewCollectionFactory() |
| 157 | taskCollection := taskCollectionFactory.LocalRepoCollection() |
| 158 | |
| 159 | // Check duplicate inside lock |
| 160 | if _, err := taskCollection.ByName(b.Name); err == nil { |
| 161 | return &task.ProcessReturnValue{Code: http.StatusConflict, Value: nil}, |
| 162 | fmt.Errorf("local repo with name %s already exists", b.Name) |
| 163 | } |
| 164 | |
| 165 | // Create repo |
| 166 | repo := deb.NewLocalRepo(b.Name, b.Comment) |
| 167 | repo.DefaultComponent = b.DefaultComponent |
| 168 | repo.DefaultDistribution = b.DefaultDistribution |
| 169 | |
| 170 | if b.FromSnapshot != "" { |
| 171 | snapshotCollection := taskCollectionFactory.SnapshotCollection() |
| 172 | |
| 173 | snapshot, err := snapshotCollection.ByName(b.FromSnapshot) |
| 174 | if err != nil { |
| 175 | return &task.ProcessReturnValue{Code: http.StatusNotFound, Value: nil}, |
| 176 | fmt.Errorf("source snapshot not found: %s", err) |
| 177 | } |
| 178 | |
| 179 | err = snapshotCollection.LoadComplete(snapshot) |
| 180 | if err != nil { |
| 181 | return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, |
| 182 | fmt.Errorf("unable to load source snapshot: %s", err) |
| 183 | } |
| 184 | |
| 185 | repo.UpdateRefList(snapshot.RefList()) |
| 186 | } |
| 187 | |
| 188 | err := taskCollection.Add(repo) |
| 189 | if err != nil { |
nothing calls this directly
no test coverage detected