From 32ed802eb7fcd2225a926a39bbaf4b44bf703c2b Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Wed, 24 Apr 2024 21:21:51 -0400 Subject: [PATCH] Add support for the JobTokenScopes API (#3571) --- packages/core/src/resources/Gitlab.ts | 3 + packages/core/src/resources/JobTokenScopes.ts | 74 +++++++++++++++++++ packages/core/src/resources/index.ts | 1 + packages/core/test/integration/map.ts | 1 + .../test/unit/resources/JobTokenScopes.ts | 74 +++++++++++++++++++ packages/rest/src/index.ts | 1 + .../rest/test/integration/browser/General.ts | 1 + 7 files changed, 155 insertions(+) create mode 100644 packages/core/src/resources/JobTokenScopes.ts create mode 100644 packages/core/test/unit/resources/JobTokenScopes.ts diff --git a/packages/core/src/resources/Gitlab.ts b/packages/core/src/resources/Gitlab.ts index 8c24a0c08..e4ecaa19c 100644 --- a/packages/core/src/resources/Gitlab.ts +++ b/packages/core/src/resources/Gitlab.ts @@ -86,6 +86,7 @@ import { IssueStateEvents } from './IssueStateEvents'; import { IssueWeightEvents } from './IssueWeightEvents'; import { JobArtifacts } from './JobArtifacts'; import { Jobs } from './Jobs'; +import { JobTokenScopes } from './JobTokenScopes'; import { MergeRequestApprovals } from './MergeRequestApprovals'; import { MergeRequestAwardEmojis } from './MergeRequestAwardEmojis'; import { MergeRequestContextCommits } from './MergeRequestContextCommits'; @@ -278,6 +279,7 @@ export interface Gitlab extends BaseResource { IssueWeightEvents: IssueWeightEvents; JobArtifacts: JobArtifacts; Jobs: Jobs; + JobTokenScopes: JobTokenScopes; MergeRequestApprovals: MergeRequestApprovals; MergeRequestAwardEmojis: MergeRequestAwardEmojis; MergeRequestContextCommits: MergeRequestContextCommits; @@ -468,6 +470,7 @@ const resources = { IssueWeightEvents, JobArtifacts, Jobs, + JobTokenScopes, MergeRequestApprovals, MergeRequestAwardEmojis, MergeRequestContextCommits, diff --git a/packages/core/src/resources/JobTokenScopes.ts b/packages/core/src/resources/JobTokenScopes.ts new file mode 100644 index 000000000..d5c7146b4 --- /dev/null +++ b/packages/core/src/resources/JobTokenScopes.ts @@ -0,0 +1,74 @@ +import { BaseResource } from '@gitbeaker/requester-utils'; +import { RequestHelper, endpoint } from '../infrastructure'; +import type { GitlabAPIResponse, ShowExpanded, Sudo } from '../infrastructure'; +import { SimpleProjectSchema } from './Projects'; + +export interface JobTokenScopeSchema extends Record { + inbound_enabled: boolean; + outbound_enabled: boolean; +} + +export interface AllowListSchema extends Record { + source_project_id: number; + target_project_id: number; +} + +export class JobTokenScopes extends BaseResource { + show( + projectId: string | number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.get()( + this, + endpoint`projects/${projectId}/job_token_scope`, + options, + ); + } + + edit( + projectId: string | number, + enabled: boolean, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.patch()( + this, + endpoint`projects/${projectId}/job_token_scope`, + { ...options, enabled }, + ); + } + + showInboundAllowList( + projectId: string | number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.get()( + this, + endpoint`projects/${projectId}/job_token_scope/allowlist`, + options, + ); + } + + addToInboundAllowList( + projectId: string | number, + targetProjectId: string | number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.post()( + this, + endpoint`projects/${projectId}/job_token_scope/allowlist/${targetProjectId}`, + options, + ); + } + + removeFromInboundAllowList( + projectId: string | number, + targetProjectId: string | number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.del()( + this, + endpoint`projects/${projectId}/job_token_scope/allowlist/${targetProjectId}`, + options, + ); + } +} diff --git a/packages/core/src/resources/index.ts b/packages/core/src/resources/index.ts index dc562ae93..9f20308b7 100644 --- a/packages/core/src/resources/index.ts +++ b/packages/core/src/resources/index.ts @@ -85,6 +85,7 @@ export * from './Issues'; export * from './IssuesStatistics'; export * from './JobArtifacts'; export * from './Jobs'; +export * from './JobTokenScopes'; export * from './MergeRequestApprovals'; export * from './MergeRequestAwardEmojis'; export * from './MergeRequestContextCommits'; diff --git a/packages/core/test/integration/map.ts b/packages/core/test/integration/map.ts index 1580cac24..3d4420057 100644 --- a/packages/core/test/integration/map.ts +++ b/packages/core/test/integration/map.ts @@ -81,6 +81,7 @@ describe('API Map', () => { 'IssueWeightEvents', 'JobArtifacts', 'Jobs', + 'JobTokenScopes', 'MergeRequestApprovals', 'MergeRequestAwardEmojis', 'MergeRequestContextCommits', diff --git a/packages/core/test/unit/resources/JobTokenScopes.ts b/packages/core/test/unit/resources/JobTokenScopes.ts new file mode 100644 index 000000000..f03ccf47a --- /dev/null +++ b/packages/core/test/unit/resources/JobTokenScopes.ts @@ -0,0 +1,74 @@ +import { JobTokenScopes } from '../../../src'; +import { RequestHelper } from '../../../src/infrastructure'; + +jest.mock( + '../../../src/infrastructure/RequestHelper', + () => require('../../__mocks__/RequestHelper').default, +); + +let service: JobTokenScopes; + +beforeEach(() => { + service = new JobTokenScopes({ + requesterFn: jest.fn(), + token: 'abcdefg', + }); +}); + +describe('JobTokenScopes.show', () => { + it('should request GET /projects/:id/job_token_scope', async () => { + await service.show(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'projects/1/job_token_scope', + undefined, + ); + }); +}); + +describe('JobTokenScopes.edit', () => { + it('should request PATCH /projects/:id/job_token_scope', async () => { + await service.edit(1, false); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'projects/1/job_token_scope', { + enabled: false, + }); + }); +}); + +describe('JobTokenScopes.showInboundAllowList', () => { + it('should request GET /projects/:id/job_token_scope/allowlist', async () => { + await service.showInboundAllowList(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'projects/1/job_token_scope/allowlist', + undefined, + ); + }); +}); + +describe('JobTokenScopes.addToInboundAllowList', () => { + it('should request POST /projects/:id/job_token_scope/allowlist/:targetId', async () => { + await service.addToInboundAllowList(1, 2); + + expect(RequestHelper.post()).toHaveBeenCalledWith( + service, + 'projects/1/job_token_scope/allowlist/2', + undefined, + ); + }); +}); + +describe('JobTokenScopes.removeFromInboundAllowList', () => { + it('should request DEL /projects/:id/job_token_scope/allowlist/:targetId', async () => { + await service.removeFromInboundAllowList(1, 2); + + expect(RequestHelper.del()).toHaveBeenCalledWith( + service, + 'projects/1/job_token_scope/allowlist/2', + undefined, + ); + }); +}); diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index 6f72541a4..4e84a64be 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -96,6 +96,7 @@ export const { IssueWeightEvents, JobArtifacts, Jobs, + JobTokenScopes, MergeRequestApprovals, MergeRequestAwardEmojis, MergeRequestContextCommits, diff --git a/packages/rest/test/integration/browser/General.ts b/packages/rest/test/integration/browser/General.ts index 829659383..6cca898b3 100644 --- a/packages/rest/test/integration/browser/General.ts +++ b/packages/rest/test/integration/browser/General.ts @@ -89,6 +89,7 @@ describe('Browser Import', () => { 'IssueWeightEvents', 'JobArtifacts', 'Jobs', + 'JobTokenScopes', 'MergeRequestApprovals', 'MergeRequestAwardEmojis', 'MergeRequestContextCommits',