The following section provides a sample code for a Before Create worker.
Note
Authorized JFrog Advanced Security (JAS) users can block upload of artifacts by before create Worker. Users without JAS can still use this Worker, but without the Stop Action feature: when trying to use it, the upload will not be blocked.
export default async (
context: PlatformContext,
data: BeforeCreateRequest
): Promise<BeforeCreateResponse> => {
let status: ActionStatus = ActionStatus.UNSPECIFIED;
try {
// The HTTP client facilitates calls to the JFrog Platform 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 = ActionStatus.PROCEED;
console.log("Artifactory ping success");
} else {
status = ActionStatus.WARN;
console.warn(
`Request was successful and returned status code : ${res.status}`
);
}
} catch (error) {
// The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
status = ActionStatus.STOP;
console.error(
`Request failed with status code ${
error.status || "<none>"
} caused by : ${error.message}`
);
}
return {
status,
message: "proceed",
};
};
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.
For Artifactory Versions before 7.123
{
"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 whether the path is a root directory (false means it is nested)
"isFolder": false // Indicates whether 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 if server checksums should be trusted (false means not)
"servletContextUrl": "servlet.com", // URL for accessing the servlet context
"skipJarIndexing": false, // Indicates whether to skip indexing for JAR files (false means indexing will occur)
"disableRedirect": false, // Indicates whether HTTP redirects should be disabled
"repoType": 1 // Numeric identifier representing the type of repository (1 typically represents a local repository)
},
"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 whether the path is a root directory (false means it is nested)
"isFolder": false // Indicates whether 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 in Unix timestamp format)
"lastModified": 0 // Timestamp of the last modification (0 indicates no modification)
},
"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 via a token (false means not)
"realm": "realm" // Realm for user authentication context
}
}
For Artifactory Versions from 7.123
{
"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 whether the path is a root directory (false means it is nested)
"isFolder": false // Indicates whether the path is a folder (false means it is a file)
},
"lastModified": 0, // Timestamp of the last modification (0 indicates it has not been modified)
"repoType": 1 // Numeric identifier representing the type of repository (1 typically represents a local repository)
},
"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 whether the path is a root directory (false means it is nested)
"isFolder": false // Indicates whether 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 in Unix timestamp format)
"lastModified": 0 // Timestamp of the last modification (0 indicates no modification)
},
"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 via a token (false means not)
"realm": "realm" // Realm for user authentication context
}
}
Response
{
"message": "proceed", // Message to print to the log, in case of an error, it will be printed as a warning
"status": "proceed" // The instruction of how to proceed
}
message and status : These are mandatory fields.
Possible Statuses
ActionStatus.PROCEED- The worker allows Artifactory to proceed with creating an artifact in storage.ActionStatus.STOP- The worker does not allow Artifactory to create an artifact in storage.ActionStatus.WARN- The worker provides a warning before Artifactory can proceed with creating an artifact in storage.