Skip to content

Commit

Permalink
refactor(branches): BranchesHandler -> BranchProtectionsHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Nov 17, 2023
1 parent 0abacec commit f26fa7a
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 182 deletions.
16 changes: 9 additions & 7 deletions __tests__/setup/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import root from '#fixtures/api.github.com/graphql.json' assert { type: 'json' }
import CLIENT_MUTATION_ID from '#fixtures/client-mutation-id.fixture'
import type Branch from '#src/branches/types/branch'
import type BranchProtection from '#src/branches/types/branch-protection'
import type Environment from '#src/environments/types/environment'
import type CreateLabelInput from '#src/labels/commands/create.command'
import type UpdateLabelInput from '#src/labels/commands/update.command'
Expand All @@ -29,7 +29,7 @@ import {
} from '@flex-development/tutils'
import type { Connection } from '@octokit/graphql'
import type {
RepositoryBranchProtectionRulesArgs as BranchesArgs,
RepositoryBranchProtectionRulesArgs as BranchProtectionsArgs,
CreateEnvironmentInput,
RepositoryEnvironmentsArgs as EnvironmentsArgs,
RepositoryLabelsArgs as LabelsArgs,
Expand Down Expand Up @@ -205,17 +205,19 @@ const server: SetupServer = setupServer(
/**
* Mock repository `branchProtectionRules` query resolver.
*
* @param {BranchesArgs} args - Query arguments
* @return {Connection<Branch>} Protected branch connection object
* @param {BranchProtectionsArgs} args - Query arguments
* @return {Connection<BranchProtection>} Branch protection connection
*/
branchProtectionRules(args: BranchesArgs): Connection<Branch> {
branchProtectionRules(
args: BranchProtectionsArgs
): Connection<BranchProtection> {
return connection('branchProtectionRules', args.after)
},
/**
* Mock repository `environments` query resolver.
*
* @param {EnvironmentsArgs} args - Query arguments
* @return {Connection<Environment>} Environment connection object
* @return {Connection<Environment>} Environment connection
*/
environments(args: EnvironmentsArgs): Connection<Environment> {
return connection('environments', args.after, 1)
Expand All @@ -230,7 +232,7 @@ const server: SetupServer = setupServer(
* Mock repository `labels` query resolver.
*
* @param {LabelsArgs} args - Query arguments
* @return {Connection<Label>} Label connection object
* @return {Connection<Label>} Label connection
*/
labels(args: LabelsArgs): Connection<Label> {
return connection('labels', args.after)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Tests - BranchProtectionsHandler
* @module branches/queries/tests/unit-d/BranchProtectionsHandler
*/

import type { BranchProtection } from '#src/branches/types'
import type { IQueryHandler } from '@nestjs/cqrs'
import type TestSubject from '../branch-protections.handler'
import type BranchProtectionsQuery from '../branch-protections.query'

describe('unit-d:branches/queries/BranchProtectionsHandler', () => {
it('should implement IQueryHandler<BranchProtectionsQuery, Branch[]>', () => {
expectTypeOf<TestSubject>()
.toMatchTypeOf<
IQueryHandler<BranchProtectionsQuery, BranchProtection[]>
>()
})
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* @file Unit Tests - BranchesHandler
* @module branches/queries/tests/unit/BranchesHandler
* @file Unit Tests - BranchProtectionsHandler
* @module branches/queries/tests/unit/BranchProtectionsHandler
*/

import data from '#fixtures/api.github.com/graphql.json' assert { type: 'json' }
import OctokitProvider from '#fixtures/octokit.provider.fixture'
import type { Branch } from '#src/branches/types'
import type { BranchProtection } from '#src/branches/types'
import { Test, type TestingModule } from '@nestjs/testing'
import TestSubject from '../branches.handler'
import BranchesQuery from '../branches.query'
import TestSubject from '../branch-protections.handler'
import BranchProtectionsQuery from '../branch-protections.query'

describe('unit:branches/queries/BranchesHandler', () => {
describe('unit:branches/queries/BranchProtectionsHandler', () => {
let ref: TestingModule
let subject: TestSubject

Expand All @@ -23,15 +23,15 @@ describe('unit:branches/queries/BranchesHandler', () => {
})

describe('#execute', () => {
let branches: Branch[]
let branches: BranchProtection[]

beforeAll(() => {
branches = data.data.repository.branchProtectionRules.nodes
})

it('should return protected branch array', async () => {
it('should return branch protection rules array', async () => {
// Arrange
const query: BranchesQuery = new BranchesQuery({
const query: BranchProtectionsQuery = new BranchProtectionsQuery({
owner: data.data.organization.login,
repo: data.data.repository.name
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @file Functional Tests - BranchesQuery
* @module branches/queries/tests/functional/BranchesQuery
* @file Functional Tests - BranchProtectionsQuery
* @module branches/queries/tests/functional/BranchProtectionsQuery
*/

import data from '#fixtures/api.github.com/graphql.json' assert { type: 'json' }
import { RepositoryQuery } from '#src/queries'
import TestSubject from '../branches.query'
import TestSubject from '../branch-protections.query'

vi.mock('#src/queries/repository.query', () => ({ default: vi.fn() }))

describe('functional:branches/queries/BranchesQuery', () => {
describe('functional:branches/queries/BranchProtectionsQuery', () => {
describe('constructor', () => {
it('should extend RepositoryQuery', () => {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - BranchProtectionsQuery
* @module branches/queries/tests/unit-d/BranchProtectionsQuery
*/

import type { RepositoryQuery } from '#src/queries'
import type TestSubject from '../branch-protections.query'

describe('unit-d:branches/queries/BranchProtectionsQuery', () => {
it('should extend RepositoryQuery', () => {
expectTypeOf<TestSubject>().toMatchTypeOf<RepositoryQuery>()
})
})

This file was deleted.

13 changes: 0 additions & 13 deletions src/subdomains/branches/queries/__tests__/branches.query.spec-d.ts

This file was deleted.

94 changes: 94 additions & 0 deletions src/subdomains/branches/queries/branch-protections.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @file Queries - BranchProtectionsHandler
* @module repostructure/branches/queries/BranchProtectionsHandler
*/

import type { BranchProtection } from '#src/branches/types'
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs'
import { Octokit } from '@octokit/core'
import * as graphql from 'graphql'
import gql from 'graphql-tag'
import BranchProtectionsQuery from './branch-protections.query'

/**
* Branch protection rules query handler.
*
* @see {@linkcode BranchProtection}
* @see {@linkcode BranchProtectionsQuery}
*
* @class
* @implements {IQueryHandler<BranchProtectionsQuery, BranchProtection[]>}
*/
@QueryHandler(BranchProtectionsQuery)
class BranchProtectionsHandler
implements IQueryHandler<BranchProtectionsQuery, BranchProtection[]> {
/**
* GraphQL query.
*
* @see https://docs.github.com/graphql/reference/objects#repository
*
* @protected
* @readonly
* @instance
* @member {string} operation
*/
protected readonly operation: string

/**
* Create a new branch protection rules query handler.
*
* @see {@linkcode Octokit}
*
* @param {Octokit} octokit - Hydrated octokit client
*/
constructor(protected readonly octokit: Octokit) {
this.operation = graphql.print(gql`
query BranchProtections(
$cursor: String,
$owner: String!,
$repo: String!
) {
payload: repository(name: $repo, owner: $owner) {
id
protections: branchProtectionRules(after: $cursor, first: 100) {
nodes {
id
pattern
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
`)
}

/**
* Execute a branch protection rules query.
*
* @see {@linkcode BranchProtection}
* @see {@linkcode BranchProtectionsQuery}
*
* @public
* @async
*
* @param {BranchProtectionsQuery} query - Query to execute
* @return {Promise<BranchProtection[]>} Branch protection rules array
*/
public async execute(
query: BranchProtectionsQuery
): Promise<BranchProtection[]> {
const {
payload
} = await this.octokit.graphql.paginate<'protections', BranchProtection>(
this.operation,
query
)

return payload.protections.nodes
}
}

export default BranchProtectionsHandler
16 changes: 16 additions & 0 deletions src/subdomains/branches/queries/branch-protections.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Queries - BranchProtectionsQuery
* @module repostructure/branches/queries/BranchProtectionsQuery
*/

import { RepositoryQuery } from '#src/queries'

/**
* Branch protection rules query.
*
* @class
* @extends {RepositoryQuery}
*/
class BranchProtectionsQuery extends RepositoryQuery {}

export default BranchProtectionsQuery
87 changes: 0 additions & 87 deletions src/subdomains/branches/queries/branches.handler.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/subdomains/branches/queries/branches.query.ts

This file was deleted.

Loading

0 comments on commit f26fa7a

Please sign in to comment.