Before Delete Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for a Before Delete worker.

export default async (context: PlatformContext, data: BeforeDeleteRequest): Promise<BeforeDeleteResponse> => {
    let status: BeforeDeleteStatus = BeforeDeleteStatus.BEFORE_DELETE_UNSPECIFIED;

    try {
        // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
        //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
        const res = await context.clients.platformHttp.get('/artifactory/api/v1/system/readiness');

        // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
        if (res.status === 200) {
            status = BeforeDeleteStatus.BEFORE_DELETE_PROCEED;
            console.log("Artifactory ping success");
        } else {
            status = BeforeDeleteStatus.BEFORE_DELETE_WARN;
            console.warn(`Request is successful but returned status other than 200. Status code : ${res.status}`);
        }
    } catch(error) {
        // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
        status = BeforeDeleteStatus.BEFORE_DELETE_STOP;
        console.error(`Request failed with status code ${error.status || '<none>'} caused by : ${error.message}`)
    }

    return {
        message: "proceed",
        status
    }
}

Input Parameters

context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).

data

The request with delete details sent by Artifactory.

{
  "metadata": {  // Object containing metadata information about the artifact
    "repoPath": {  // Repository path information for the artifact
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfolder/my-file",  // Path to the specific file within the repository
      "id": "local-repo:folder/subfolder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates if the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the path is a folder (false means it is a file)
    },
    "repoType": 1,  // Numeric identifier representing the type of repository (1 typically denotes a local repository)
    "triggerMetadataCalculation": false,  // Indicates whether metadata calculation should be triggered (false means it does not)
    "allowAsyncDelete": false,  // Indicates if asynchronous deletion of the artifact is allowed (false means it is not)
    "skipTrashcan": false,  // Indicates if the deletion should skip the trashcan (false means it will go to the trashcan)
    "isTriggeredByGc": false,  // Indicates if the deletion was triggered by garbage collection (false means it was not)
    "triggeredByMove": false  // Indicates if the action was triggered by moving the artifact (false means it was not)
  },
  "userContext": {  // Object containing information about the user making the request
    "id": "id",  // Unique identifier for the user
    "isToken": false,  // Indicates whether the user is authenticated using a token (false means not)
    "realm": "realm"  // Realm for user authentication context
  },
  "itemInfo": {  // Object containing information about the specific item (artifact)
    "repoPath": {  // Repository path information for the item
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfolder/my-file",  // Path to the specific item within the repository
      "id": "local-repo:folder/subfolder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates if the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the path is a folder (false means it is a file)
    },
    "name": "my-artifact",  // Name of the item (artifact)
    "created": 1,  // Timestamp indicating when the item was created (assumed to be Unix timestamp)
    "lastModified": 0  // Timestamp of the last modification (0 indicates no modification)
  }
}
Response
{
  "data": {  // Main data object containing the result of the operation
    "message": "proceed",  // Message to print to the log. In case of an error, it will be printed as a warning
    "status": 1  // The instruction of how to proceed
  },
  "executionStatus": "STATUS_SUCCESS"  // Overall status indicating that the execution was successful
}
  • message and status : These are mandatory fields.

Possible Statuses
  • BeforePropertyDeleteStatus.BEFORE_PROPERTY_DELETE_PROCEED - The worker allows Artifactory to proceed with deleting a property.

  • BeforePropertyDeleteStatus.BEFORE_PROPERTY_DELETE_STOP - The worker does not allow Artifactory to delete a property.

  • BeforePropertyDeleteStatus.BEFORE_PROPERTY_Delete_WARN - The worker provides a warning before Artifactory can proceed with deleting a property.