-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WKS-890 - Add BEFORE_CREATE_TOKEN action support
- Loading branch information
1 parent
4ef5b7c
commit 7b10643
Showing
4 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PlatformContext, BeforeCreateTokenRequest, PlatformClients, PlatformHttpClient, CreateTokenStatus } from 'jfrog-workers'; | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import runWorker from './worker'; | ||
|
||
describe("{{.WorkerName}} tests", () => { | ||
let context: DeepMocked<PlatformContext>; | ||
let request: DeepMocked<BeforeCreateTokenRequest>; | ||
|
||
beforeEach(() => { | ||
context = createMock<PlatformContext>({ | ||
clients: createMock<PlatformClients>({ | ||
platformHttp: createMock<PlatformHttpClient>({ | ||
get: jest.fn().mockResolvedValue({ status: 200 }) | ||
}) | ||
}) | ||
}); | ||
request = createMock<BeforeCreateTokenRequest>(); | ||
}) | ||
|
||
it('should run', async () => { | ||
await expect(runWorker(context, request)).resolves.toEqual(expect.objectContaining({ | ||
message: 'Overwritten by worker-service if an error occurs.', | ||
status: CreateTokenStatus.CREATE_TOKEN_PROCEED | ||
})) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { PlatformContext, BeforeCreateTokenRequest, BeforeCreateTokenResponse, CreateTokenStatus } from 'jfrog-workers'; | ||
|
||
export default async (context: PlatformContext, data: BeforeCreateTokenRequest): Promise<BeforeCreateTokenResponse> => { | ||
|
||
let status: CreateTokenStatus = CreateTokenStatus.CREATE_TOKEN_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 = CreateTokenStatus.CREATE_TOKEN_PROCEED; | ||
console.log("Artifactory ping success"); | ||
} else { | ||
status = CreateTokenStatus.CREATE_TOKEN_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 = CreateTokenStatus.CREATE_TOKEN_STOP; | ||
console.error(`Request failed with status code ${ error.status || '<none>' } caused by : ${ error.message }`) | ||
} | ||
|
||
return { | ||
status, | ||
message: 'Overwritten by worker-service if an error occurs.', | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters