Before Create Property Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for a Before Create Property worker.

export default async (context: PlatformContext, data: BeforePropertyCreateRequest): Promise<BeforePropertyCreateResponse> => 
{
let status: BeforePropertyCreateStatus = BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_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 = 
  BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_PROCEED;
  console.log("Artifactory ping success");
  } 
else {
  status = BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_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 = BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_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 create details sent by Artifactory.

{
  "metadata": {  // Object containing metadata information about the artifact
    "repoPath": {  // Current repository path information for the artifact
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfolder/my-file",  // Current 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)
    },
    "contentLength": 100,  // Length of the content in bytes
    "lastModified": 0,  // Timestamp of the last modification (0 indicates it has not been modified)
    "trustServerChecksums": false,  // Indicates whether to trust server checksums for validation
    "servletContextUrl": "https://jpd.jfrog.io/artifactory",  // URL for accessing the servlet context
    "skipJarIndexing": false,  // Indicates whether to skip indexing for JAR files
    "disableRedirect": false,  // Indicates whether HTTP redirects should be disabled
    "repoType": 1  // Numeric identifier representing the type of repository (e.g., local, remote, virtual)
  },
  "userContext": {  // Object containing information about the user making the request
    "id": "id",  // Unique identifier for the user
    "isToken": false,  // Indicates if the user is authenticated using a token (false means they are not)
    "realm": "realm"  // Realm for user authentication context
  },
  "itemInfo": {  // Object containing information about the specific item (artifact)
    "repoPath": {  // Current repository path information for the item
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfolder/my-file",  // Current 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 of when the item was created (assumed to be Unix timestamp)
    "lastModified": 0  // Timestamp of the last modification (0 indicates no modification)
  },
  "name": "property-name",  // Name of the property being set or modified
  "values": [  // Array of values associated with the property
    "value1",  // First value of the property
    "value2"   // Second value of the property
  ]
}
Response
{
  status: BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_PROCEED, // The instruction of how to proceed
  message: 'proceed',  // Message to print to the log. In case of an error, it will be printed as a warning.
}
  • message and status : These are mandatory fields.

Possible Statuses
  • BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_PROCEED - The worker allows Artifactory to proceed with creating a property.

  • BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_STOP - The worker does not allow Artifactory to create a property.

  • BeforePropertyCreateStatus.BEFORE_PROPERTY_CREATE_WARN - The worker provides a warning before Artifactory can proceed with creating a property.