-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
890714d
commit e18d07a
Showing
18 changed files
with
448 additions
and
32 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
# Pre-Commit Workflow | ||
# | ||
# References: | ||
# | ||
# - https://github.com/okonet/lint-staged#command-line-flags | ||
|
||
yarn build | ||
yarn check:types:build | ||
git add dist/** --verbose | ||
cross-env LINT_STAGED=1 lint-staged --config=.lintstagedrc.json --relative --verbose |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @file Unit Tests - BranchQueryHandler | ||
* @module commit-action/queries/tests/unit/BranchQueryHandler | ||
*/ | ||
|
||
import * as github from '@actions/github' | ||
import pathe from '@flex-development/pathe' | ||
import { join } from '@flex-development/tutils' | ||
import { Test } from '@nestjs/testing' | ||
import { Octokit } from '@octokit/core' | ||
import TestSubject from '../branch.handler' | ||
import BranchQuery from '../branch.query' | ||
|
||
describe('unit:queries/BranchQueryHandler', () => { | ||
let subject: TestSubject | ||
|
||
beforeAll(async () => { | ||
subject = (await Test.createTestingModule({ | ||
providers: [ | ||
TestSubject, | ||
{ | ||
provide: Octokit, | ||
useValue: github.getOctokit(process.env.GITHUB_TOKEN!, { | ||
log: { | ||
debug: vi.fn().mockName('octokit.log.debug'), | ||
error: vi.fn().mockName('octokit.log.error'), | ||
info: vi.fn().mockName('octokit.log.info'), | ||
warn: vi.fn().mockName('octokit.log.warn') | ||
} | ||
}) | ||
} | ||
] | ||
}).compile()).get(TestSubject) | ||
}) | ||
|
||
describe('execute', () => { | ||
let owner: string | ||
let repo: string | ||
|
||
beforeAll(() => { | ||
owner = 'flex-development' | ||
repo = 'commit-action' | ||
}) | ||
|
||
it('should return branch payload if query.ref is found', async () => { | ||
// Arrange | ||
const ref: string = 'main' | ||
const query: BranchQuery = new BranchQuery({ owner, ref, repo }) | ||
|
||
// Act | ||
const result = await subject.execute(query) | ||
|
||
// Expect | ||
expect(result).to.have.keys(['head', 'name', 'repository']) | ||
expect(result).toMatchObject({ | ||
head: <string>expect.any(String), | ||
name: ref, | ||
repository: join([owner, repo], pathe.sep) | ||
}) | ||
}) | ||
|
||
it('should return null if query.ref is not found', async () => { | ||
// Arrange | ||
const query: BranchQuery = new BranchQuery({ owner, ref: '', repo }) | ||
|
||
// Act + Expect | ||
expect(await subject.execute(query)).to.be.null | ||
}) | ||
}) | ||
}) |
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,95 @@ | ||
/** | ||
* @file Queries - BranchQueryHandler | ||
* @module commit-action/queries/BranchQueryHandler | ||
*/ | ||
|
||
import type { Branch } from '#src/types' | ||
import { isNIL, type Nullable } from '@flex-development/tutils' | ||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs' | ||
import { Octokit } from '@octokit/core' | ||
import type { Repository as Payload } from '@octokit/graphql-schema' | ||
import * as graphql from 'graphql' | ||
import gql from 'graphql-tag' | ||
import BranchQuery from './branch.query' | ||
|
||
/** | ||
* Branch query handler. | ||
* | ||
* @see {@linkcode Branch} | ||
* @see {@linkcode BranchQuery} | ||
* | ||
* @class | ||
* @implements {IQueryHandler<BranchQuery,Nullable<Branch>>} | ||
*/ | ||
@QueryHandler(BranchQuery) | ||
class BranchQueryHandler | ||
implements IQueryHandler<BranchQuery, Nullable<Branch>> { | ||
/** | ||
* GraphQL query. | ||
* | ||
* @see {@linkcode graphql.DocumentNode} | ||
* @see https://docs.github.com/graphql/reference/objects#repository | ||
* | ||
* @protected | ||
* @readonly | ||
* @instance | ||
* @member {graphql.DocumentNode} operation | ||
*/ | ||
protected readonly operation: graphql.DocumentNode | ||
|
||
/** | ||
* Create a new branch query handler. | ||
* | ||
* @see {@linkcode Octokit} | ||
* | ||
* @param {Octokit} octokit - Hydrated octokit client | ||
*/ | ||
constructor(protected readonly octokit: Octokit) { | ||
this.operation = gql` | ||
query ($owner: String!, $ref: String!, $repo: String!) { | ||
payload: repository(name: $repo, owner: $owner) { | ||
ref(qualifiedName: $ref) { | ||
name | ||
repository { nameWithOwner } | ||
target { oid } | ||
} | ||
} | ||
} | ||
` | ||
} | ||
|
||
/** | ||
* Execute a branch query. | ||
* | ||
* @see {@linkcode Branch} | ||
* @see {@linkcode BranchQuery} | ||
* | ||
* @public | ||
* @async | ||
* | ||
* @param {BranchQuery} query - Query to execute | ||
* @return {Promise<Nullable<Branch>>} Branch payload or `null` | ||
*/ | ||
public async execute(query: BranchQuery): Promise<Nullable<Branch>> { | ||
// fetch branch reference from repository | ||
const { payload } = await this.octokit.graphql<{ payload: Payload }>({ | ||
owner: query.owner, | ||
query: graphql.print(this.operation), | ||
ref: query.ref, | ||
repo: query.repo | ||
}) | ||
|
||
// format branch payload if reference was found | ||
if (!isNIL(payload.ref) && !isNIL(payload.ref.target)) { | ||
return { | ||
head: payload.ref.target.oid, | ||
name: payload.ref.name, | ||
repository: payload.ref.repository.nameWithOwner | ||
} | ||
} | ||
|
||
return null | ||
} | ||
} | ||
|
||
export default BranchQueryHandler |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @file Type Tests - Branch | ||
* @module commit-action/types/tests/unit-d/Branch | ||
*/ | ||
|
||
import type TestSubject from '../branch' | ||
|
||
describe('unit-d:types/Branch', () => { | ||
it('should match [head: string]', () => { | ||
expectTypeOf<TestSubject>().toHaveProperty('head').toEqualTypeOf<string>() | ||
}) | ||
|
||
it('should match [name: string]', () => { | ||
expectTypeOf<TestSubject>().toHaveProperty('name').toEqualTypeOf<string>() | ||
}) | ||
|
||
it('should match [repository: string]', () => { | ||
expectTypeOf<TestSubject>() | ||
.toHaveProperty('repository') | ||
.toEqualTypeOf<string>() | ||
}) | ||
}) |
Oops, something went wrong.