Skip to content

Commit

Permalink
WKS-890 - Add BEFORE_CREATE_TOKEN action support
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliil-jfrog committed May 14, 2024
1 parent 4ef5b7c commit 7b10643
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
26 changes: 26 additions & 0 deletions commands/templates/BEFORE_CREATE_TOKEN.spec.ts_template
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
}))
})
});
30 changes: 30 additions & 0 deletions commands/templates/BEFORE_CREATE_TOKEN.ts_template
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.',
}
};
4 changes: 3 additions & 1 deletion model/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ const (
ActionAfterBuildInfoSave = "AFTER_BUILD_INFO_SAVE"
ActionAfterMove = "AFTER_MOVE"
ActionGenericEvent = "GENERIC_EVENT"
ActionBeforeCreateToken = "BEFORE_CREATE_TOKEN"
)

var (
actionsNames = fmt.Sprintf("%s|%s|%s|%s|%s|%s|%s", ActionBeforeDownload, ActionAfterDownload, ActionBeforeUpload, ActionAfterCreate, ActionAfterBuildInfoSave, ActionAfterMove, ActionGenericEvent)
actionsNames = fmt.Sprintf("%s|%s|%s|%s|%s|%s|%s|%s", ActionBeforeDownload, ActionAfterDownload, ActionBeforeUpload, ActionAfterCreate, ActionAfterBuildInfoSave, ActionAfterMove, ActionGenericEvent, ActionBeforeCreateToken)
actionsNamesPattern = regexp.MustCompile("(" + actionsNames + ")")
)

var actionsWithoutCriteria = map[string]any{
ActionAfterBuildInfoSave: struct{}{},
ActionGenericEvent: struct{}{},
ActionBeforeCreateToken: struct{}{},
}

func ActionNames() string {
Expand Down
2 changes: 1 addition & 1 deletion model/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestActionNames(t *testing.T) {
func TestActionNeedsCriteria(t *testing.T) {
for _, action := range strings.Split(ActionNames(), "|") {
t.Run(action, func(t *testing.T) {
assert.Equalf(t, action != "AFTER_BUILD_INFO_SAVE" && action != "GENERIC_EVENT", ActionNeedsCriteria(action), "ActionNeedsCriteria(%v)", action)
assert.Equalf(t, action != "AFTER_BUILD_INFO_SAVE" && action != "GENERIC_EVENT" && action != "BEFORE_CREATE_TOKEN", ActionNeedsCriteria(action), "ActionNeedsCriteria(%v)", action)
})
}
}
Expand Down

0 comments on commit 7b10643

Please sign in to comment.