@@ -216,6 +216,32 @@ func (h *HarborOCI) SetMemberPermissions(ctx context.Context, roleID int, org st
216
216
return nil
217
217
}
218
218
219
+ type HarborProject struct {
220
+ ProjectID int `json:"project_id"`
221
+ }
222
+
223
+ func (h * HarborOCI ) GetProjectID (ctx context.Context , org string , displayName string ) (int , error ) {
224
+ URL := h .harborHost + "/api/v2.0/projects/" + HarborProjectName (org , displayName )
225
+
226
+ projectResults := HarborProject {}
227
+ resp , err := h .doHarborREST (ctx , http .MethodGet , URL , nil , AddHeaders )
228
+ if err != nil {
229
+ return 0 , err
230
+ }
231
+ if resp .StatusCode != http .StatusOK {
232
+ responseBody , _ := io .ReadAll (resp .Body )
233
+ responseJSON := string (responseBody )
234
+ return 0 , fmt .Errorf ("%s" , responseJSON )
235
+ }
236
+
237
+ err = json .NewDecoder (resp .Body ).Decode (& projectResults )
238
+ if err != nil {
239
+ return 0 , err
240
+ }
241
+
242
+ return projectResults .ProjectID , nil
243
+ }
244
+
219
245
type RobotAccess struct {
220
246
Action string `json:"action"`
221
247
Resource string `json:"resource"`
@@ -319,9 +345,9 @@ type HarborRobot struct {
319
345
UpdateTime time.Time `json:"update_time"`
320
346
}
321
347
322
- func (h * HarborOCI ) GetRobot (ctx context.Context , org string , displayName string , robotName string ) (* HarborRobot , error ) {
348
+ func (h * HarborOCI ) GetRobot (ctx context.Context , org string , displayName string , robotName string , projectID int ) (* HarborRobot , error ) {
323
349
robotName = fmt .Sprintf (`robot$%s+%s` , HarborProjectName (org , displayName ), robotName )
324
- URL := h .harborHost + "/api/v2.0/projects/ " + HarborProjectName ( org , displayName ) + "/robots"
350
+ URL := h .harborHost + "/api/v2.0/robots?q=Level=project,ProjectID= " + fmt . Sprintf ( "%d" , projectID )
325
351
326
352
robotsResults := []HarborRobot {}
327
353
resp , err := h .doHarborREST (ctx , http .MethodGet , URL , nil , AddHeaders )
@@ -349,8 +375,8 @@ func (h *HarborOCI) GetRobot(ctx context.Context, org string, displayName string
349
375
return nil , fmt .Errorf ("harbor robot %s not found" , robotName )
350
376
}
351
377
352
- func (h * HarborOCI ) DeleteRobot (ctx context.Context , org string , displayName string , robotID int ) error {
353
- URL := fmt .Sprintf ("%s/api/v2.0/projects/%s/ robots/%d" , h .harborHost , HarborProjectName ( org , displayName ) , robotID )
378
+ func (h * HarborOCI ) DeleteRobot (ctx context.Context , robotID int ) error {
379
+ URL := fmt .Sprintf ("%s/api/v2.0/robots/%d" , h .harborHost , robotID )
354
380
resp , err := h .doHarborREST (ctx , http .MethodDelete , URL , nil , AddHeaders )
355
381
if err != nil {
356
382
return err
0 commit comments