After you click Add, you reach the edit page, where you can enter typescript code to define how the worker will behave.
Figure 5 - The edit page of a worker.
The code editor contains a small working sample script that is a template for a worker’s script.
This sample pings Artifactory and returns a status according to the HTTP response.
The worker service is expecting you to export as default an anonymous function that takes two inputs: context and data:
For more information see the typescript API documentation.
Figure 6 - The data structure for the request payload the worker will receive.
With the above script, the worker would return an object with the UploadStatus that would tell Artifactory whether to accept an upload or not.
The following script implements a worker that accepts uploads only if the artifact layout matches a given pattern:
Figure 5 - The edit page of a worker.
The code editor contains a small working sample script that is a template for a worker’s script.
This sample pings Artifactory and returns a status according to the HTTP response.
The worker service is expecting you to export as default an anonymous function that takes two inputs: context and data:
Parameter | Description |
---|---|
context | The context parameter is a useful toolbox provided by the worker service. |
data | The data parameter contains the payload of the execution request. It contains the metadata for the action that triggers the worker. You can see the data parameter structure in the right panel (see the figure below) |
For more information see the typescript API documentation.
Figure 6 - The data structure for the request payload the worker will receive.
With the above script, the worker would return an object with the UploadStatus that would tell Artifactory whether to accept an upload or not.
The following script implements a worker that accepts uploads only if the artifact layout matches a given pattern:
export default async (context: PlatformContext, data: BeforeUploadRequest): Promise<BeforeUploadResponse> => { // This RegExp will match all repopaths that start with 'org/company/' and end with the extension .jar OR .war // For instance those paths will match the regex : // - org/company/src/app.jar // - org/company/package1/subPackage/webapp.war const authorizedPathRegEx = /^org\/company\/(?:\w+.\/)+[\w\-\.]+\.(?:jar|war)$/; let status: UploadStatus = UploadStatus.UPLOAD_UNSPECIFIED; let message = ""; try { if (authorizedPathRegEx.exec(data.metadata.repoPath.path)) { status = UploadStatus.UPLOAD_PROCEED; message = `RepoPath '${data.metadata.repoPath.path}' is acceptable for the repository '${data.metadata.repoPath.key}'`; } else { status = UploadStatus.UPLOAD_STOP; message = `RepoPath '${data.metadata.repoPath.path}' does not match the regex ${authorizedPathRegEx} for the repository '${data.metadata.repoPath.key}'`; } } catch(error) { status = UploadStatus.UPLOAD_WARN; console.error(`could not check the path: ${error}`); message = `An error occurred during the check. Proceed with warning.`; } return { status, message, modifiedRepoPath: data.metadata.repoPath } }