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 9cf4b1d
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 175 deletions.
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.

6 changes: 4 additions & 2 deletions src/subdomains/branches/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
* @module repostructure/branches/queries
*/

export { default as BranchesHandler } from './branches.handler'
export { default as BranchesQuery } from './branches.query'
export {
default as BranchProtectionsHandler
} from './branch-protections.handler'
export { default as BranchProtectionsQuery } from './branch-protections.query'
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @file Type Tests - Branch
* @module branches/types/tests/unit-d/Branch
* @file Type Tests - BranchProtection
* @module branches/types/tests/unit-d/BranchProtection
*/

import type { ReadonlyKeys } from '@flex-development/tutils'
import type TestSubject from '../branch'
import type TestSubject from '../branch-protection'

describe('unit-d:branches/types/Branch', () => {
describe('unit-d:branches/types/BranchProtection', () => {
type RK = ReadonlyKeys<TestSubject>

it('should match [readonly id: string]', () => {
Expand Down
23 changes: 23 additions & 0 deletions src/subdomains/branches/types/branch-protection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file Type Definitions - BranchProtection
* @module repostructure/branches/types/BranchProtection
*/

/**
* Branch protection rule object.
*
* @see https://docs.github.com/graphql/reference/objects#branchprotectionrule
*/
type BranchProtection = {
/**
* Node ID of branch protection rule.
*/
readonly id: string

/**
* Branch pattern.
*/
readonly pattern: string
}

export type { BranchProtection as default }
Loading

0 comments on commit 9cf4b1d

Please sign in to comment.