Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Project V2 field #5

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TODO
_eg: `https://github.com/orgs|users/<ownerName>/projects/<projectNumber>`_
- `github-token` **(required)** is a [personal access
token](https://github.com/settings/tokens/new) with `repo` and `project` scopes.
- `field-name` **(required)** is a field name of the project v2 item to update.

## Outputs

Expand Down
80 changes: 78 additions & 2 deletions __tests__/ex-octokit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('fetchProjectV2Id', () => {

it('fetches ProjectV2 ID from organization', async () => {
mockGraphQL({
test: /getProject/,
test: /fetchProjectV2Id/,
return: {
organization: {
projectV2: {
Expand All @@ -30,7 +30,7 @@ describe('fetchProjectV2Id', () => {

it('fetches ProjectV2 ID from user', async () => {
mockGraphQL({
test: /getProject/,
test: /fetchProjectV2Id/,
return: {
user: {
projectV2: {
Expand All @@ -47,6 +47,82 @@ describe('fetchProjectV2Id', () => {
})
})

describe('fetchProjectV2FieldByName', () => {
afterEach(() => {
jest.restoreAllMocks()
})

it('fetches field on ProjectV2Field', async () => {
mockGraphQL({
test: /fetchProjectV2FieldByName/,
return: {
node: {
field: {
__typename: 'ProjectV2Field',
id: 'field-id',
name: 'text-field',
dataType: 'TEXT'
}
}
}
})

const exOctokit = new ExOctokit('gh_token')
const projectV2Field = await exOctokit.fetchProjectV2FieldByName(
'project-id',
'text-field'
)

expect(projectV2Field).toEqual({
__typename: 'ProjectV2Field',
id: 'field-id',
name: 'text-field',
dataType: 'TEXT'
})
})

it('fetches field on ProjectV2SingleSelectField', async () => {
mockGraphQL({
test: /fetchProjectV2FieldByName/,
return: {
node: {
field: {
__typename: 'ProjectV2SingleSelectField',
id: 'field-id',
name: 'select-field',
dataType: 'SINGLE_SELECT',
options: [
{
id: 'option-id',
name: 'option-name'
}
]
}
}
}
})

const exOctokit = new ExOctokit('gh_token')
const projectV2Field = await exOctokit.fetchProjectV2FieldByName(
'project-id',
'select-field'
)

expect(projectV2Field).toEqual({
__typename: 'ProjectV2SingleSelectField',
id: 'field-id',
name: 'select-field',
dataType: 'SINGLE_SELECT',
options: [
{
id: 'option-id',
name: 'option-name'
}
]
})
})
})

function mockGraphQL(...mocks: { test: RegExp; return: unknown }[]): jest.Mock {
const mock = jest.fn().mockImplementation((query: string) => {
const match = mocks.find(m => m.test.test(query))
Expand Down
16 changes: 16 additions & 0 deletions __tests__/update-project-v2-item-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ExOctokit } from '../src/ex-octokit'

describe('updateProjectV2ItemField', () => {
let outputs: Record<string, string>
let debug: jest.SpyInstance

beforeEach(() => {
jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
Expand All @@ -17,6 +18,7 @@ describe('updateProjectV2ItemField', () => {
'github-token': 'gh_token'
})

debug = mockDebug()
outputs = mockSetOutput()
})

Expand All @@ -42,9 +44,12 @@ describe('updateProjectV2ItemField', () => {
}

mockFetchProjectV2Id().mockResolvedValue('project-id')
mockFetchProjectV2FieldByName().mockResolvedValue({ id: 'field-id' })

await updateProjectV2ItemField()

expect(debug).toHaveBeenCalledWith('ProjectV2 ID: project-id')
expect(debug).toHaveBeenCalledWith('Field ID: field-id')
expect(outputs.projectV2Id).toEqual('project-id')
})

Expand Down Expand Up @@ -95,9 +100,12 @@ describe('updateProjectV2ItemField', () => {
}

mockFetchProjectV2Id().mockResolvedValue('project-id')
mockFetchProjectV2FieldByName().mockResolvedValue({ id: 'field-id' })

await updateProjectV2ItemField()

expect(debug).toHaveBeenCalledWith('ProjectV2 ID: project-id')
expect(debug).toHaveBeenCalledWith('Field ID: field-id')
expect(outputs.projectV2Id).toEqual('project-id')
})
})
Expand All @@ -115,6 +123,14 @@ function mockSetOutput(): Record<string, string> {
return output
}

function mockDebug(): jest.SpyInstance {
return jest.spyOn(core, 'debug').mockImplementation()
}

function mockFetchProjectV2Id(): jest.SpyInstance {
return jest.spyOn(ExOctokit.prototype, 'fetchProjectV2Id')
}

function mockFetchProjectV2FieldByName(): jest.SpyInstance {
return jest.spyOn(ExOctokit.prototype, 'fetchProjectV2FieldByName')
}
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ inputs:
github-token:
required: true
description: A GitHub personal access token with write access to the project
field-name:
required: true
description: The name of the field to update

outputs:
projectId:
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 39 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 58 additions & 1 deletion src/ex-octokit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ interface ProjectV2IdResponse {
}
}

interface ProjectV2FieldResponse {
node: {
field: ProjectV2Field | null
} | null
}

interface ProjectV2Field {
__typename: 'ProjectV2Field' | 'ProjectV2SingleSelectField'
id: string
name: string
dataType: string

options?: {
id: string
name: string
}[]
}

export class ExOctokit {
octokit: ReturnType<typeof getOctokit>

Expand All @@ -27,7 +45,7 @@ export class ExOctokit {
projectNumber: number
): Promise<string | undefined> {
const projectV2IdResponse = await this.octokit.graphql<ProjectV2IdResponse>(
`query getProject($projectOwnerName: String!, $projectNumber: Int!) {
`query fetchProjectV2Id($projectOwnerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $projectOwnerName) {
projectV2(number: $projectNumber) {
id
Expand All @@ -42,4 +60,43 @@ export class ExOctokit {

return projectV2IdResponse[ownerTypeQuery]?.projectV2.id
}

// TODO: support 'ProjectV2IterationField' Type
async fetchProjectV2FieldByName(
projectV2Id: string,
fieldName: string
): Promise<ProjectV2Field | null | undefined> {
const projectV2FieldResponse =
await this.octokit.graphql<ProjectV2FieldResponse>(
`query fetchProjectV2FieldByName($projectOwnerName: String!, $fieldName: Int!) {
node(id: $projectV2Id) {
... on ProjectV2 {
field(name: $fieldName) {
__typename
... on ProjectV2Field {
id
name
dataType
}
... on ProjectV2SingleSelectField {
id
name
dataType
options {
id
name
}
}
}
}
}
}`,
{
projectV2Id,
fieldName
}
)

return projectV2FieldResponse.node?.field
}
}
13 changes: 13 additions & 0 deletions src/update-project-v2-item-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function updateProjectV2ItemField(): Promise<void> {
// Get the action inputs
const projectUrl = core.getInput('project-url', { required: true })
const ghToken = core.getInput('github-token', { required: true })
const fieldName = core.getInput('field-name', { required: true })

// Get the issue/PR owner name and node ID from payload
const issue =
Expand Down Expand Up @@ -40,9 +41,21 @@ export async function updateProjectV2ItemField(): Promise<void> {
projectOwnerName,
projectNumber
)
if (!projectV2Id) {
throw new Error(`ProjectV2 ID is undefined`)
}

const contentId = issue?.node_id

// Fetch the field node ID
const field = await exOctokit.fetchProjectV2FieldByName(
projectV2Id,
fieldName
)
const fieldId = field?.id

core.debug(`ProjectV2 ID: ${projectV2Id}`)
core.debug(`Field ID: ${fieldId}`)
core.debug(`Content ID: ${contentId}`)

// Set outputs for other workflow steps to use
Expand Down
Loading