Skip to content

Commit

Permalink
Move code into separate files and separate concerns a bit better, avo…
Browse files Browse the repository at this point in the history
…id a extra GraphQL request
  • Loading branch information
bordoni committed Feb 22, 2024
1 parent ca5d4c5 commit f498e97
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 153 deletions.
43 changes: 1 addition & 42 deletions __tests__/project-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import * as github from '@actions/github'

import {projectLink, mustGetOwnerTypeQuery} from '../src/project-link'
import {projectLink} from '../src/project-link'

describe('projectLink', () => {
let outputs: Record<string, string>
Expand Down Expand Up @@ -996,47 +996,6 @@ describe('projectLink', () => {
expect(outputs.itemId).toEqual('project-next-item-id')
})
})

describe('mustGetOwnerTypeQuery', () => {
test('returns organization for orgs ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('orgs')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for empty ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery()

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for org ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('org')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for organizations ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('organizations')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns user for users ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('users')

expect(ownerTypeQuery).toEqual('user')
})

test('throws an error when an unsupported ownerType is set', async () => {
expect(() => {
mustGetOwnerTypeQuery('unknown')
}).toThrow(
`Unsupported ownerType: unknown. Must be one of 'orgs', 'organization', 'org', 'organizations' or 'users', 'user'`,
)
})
})

function mockGetInput(mocks: Record<string, string>): jest.SpyInstance {
const mock = (key: string) => mocks[key] ?? ''
return jest.spyOn(core, 'getInput').mockImplementation(mock)
Expand Down
86 changes: 86 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {mustGetOwnerTypeQuery, parseProjectName} from '../src/utils'

describe('mustGetOwnerTypeQuery', () => {
test('returns organization for orgs ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('orgs')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for empty ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery()

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for org ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('org')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for organizations ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('organizations')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns organization for wrong case in param', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('Organizations')

expect(ownerTypeQuery).toEqual('organization')
})

test('returns user for users ownerType', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('users')

expect(ownerTypeQuery).toEqual('user')
})

test('returns user for wrong case in param', async () => {
const ownerTypeQuery = mustGetOwnerTypeQuery('uSeRs')

expect(ownerTypeQuery).toEqual('user')
})

test('throws an error when an unsupported ownerType is set', async () => {
expect(() => {
mustGetOwnerTypeQuery('unknown')
}).toThrow(
`Unsupported ownerType: unknown. Must be one of 'orgs', 'organization', 'org', 'organizations' or 'users', 'user'`,
)
})
})

describe('parseProjectName', () => {
test('returns the given branch without changes when no param', async () => {
const baseBranch = 'main'
const projectName = parseProjectName({baseBranch})

expect(projectName).toEqual('main')
})

test('returns the given branch without the prefix when a prefixRemove is passed', async () => {
const baseBranch = 'feature/work'
const prefixRemove = 'feature/'
const projectName = parseProjectName({baseBranch, prefixRemove})

expect(projectName).toEqual('work')
})

test('returns the given branch without the suffix when a suffixRemove is passed', async () => {
const baseBranch = 'feature/work'
const sufixRemove = '/work'
const projectName = parseProjectName({baseBranch, sufixRemove})

expect(projectName).toEqual('feature')
})

test('returns the given branch with the a set of chars replaced by spaces', async () => {
const baseBranch = 'feature-work-branch_issue'
const replaceWithSpaces = '-_'
const projectName = parseProjectName({baseBranch, replaceWithSpaces})

expect(projectName).toEqual('feature work branch issue')
})
})
24 changes: 18 additions & 6 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export interface ParseProjectName {
baseBranch: string
prefixRemove?: string
sufixRemove?: string
replaceWithSpaces?: string
}

export interface ParsedProjectUrl {
ownerType: 'organization' | 'user'
ownerName: string
projectNumber: number
}

interface ProjectNodeIDResponse {
export interface ProjectNodeIDResponse {
organization?: {
projectV2: {
id: string
Expand All @@ -13,7 +25,7 @@ interface ProjectNodeIDResponse {
}
}

interface OwnerResponse {
export interface OwnerResponse {
user?: {
id: string
}
Expand All @@ -22,15 +34,15 @@ interface OwnerResponse {
}
}

interface ProjectNode {
export interface ProjectNode {
node: {
id: string
title: string
number: number
}
}

interface ProjectsEdgesNodesResponse {
export interface ProjectsEdgesNodesResponse {
organization?: {
projectsV2: {
totalCount: number
Expand All @@ -46,7 +58,7 @@ interface ProjectsEdgesNodesResponse {
}
}

interface ProjectAddItemResponse {
export interface ProjectAddItemResponse {
addProjectV2ItemById: {
item: {
id: string
Expand All @@ -58,7 +70,7 @@ interface ProjectAddItemResponse {
}
}

interface ProjectCopyTemplateResponse {
export interface ProjectCopyTemplateResponse {
copyProjectV2: {
projectV2: {
id: string
Expand Down
Loading

0 comments on commit f498e97

Please sign in to comment.