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

update project v2 item field value #8

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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ TODO
- `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.
_note: Supported field types are `text`, `number`, `date` and `single_select`._
- `field-value` **(required)** is a field value of the project v2 item to update.

## Outputs

TODO
- `item-id` is the ID of the project v2 updated item.

## Development

Expand Down
67 changes: 67 additions & 0 deletions __tests__/ex-octokit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,73 @@ describe('addProjectV2ItemById', () => {
id: 'item-id'
})
})

it('returns undefined when request failed', async () => {
mockGraphQL({
test: /addProjectV2ItemById/,
return: {
addProjectV2ItemById: null
}
})

const exOctokit = new ExOctokit('gh_token')
const projectV2Item = await exOctokit.addProjectV2ItemByContentId(
'project-id',
'content-id'
)

expect(projectV2Item).toBeUndefined()
})
})

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

it('returns ProjectV2FieldItem', async () => {
mockGraphQL({
test: /updateProjectV2ItemFieldValue/,
return: {
updateProjectV2ItemFieldValue: {
projectV2Item: {
id: 'item-id'
}
}
}
})

const exOctokit = new ExOctokit('gh_token')
const projectV2Item = await exOctokit.updateProjectV2ItemFieldValue(
'project-id',
'item-id',
'field-id',
{ singleSelectOptionId: 'option-id' }
)

expect(projectV2Item).toEqual({
id: 'item-id'
})
})

it('returns undefined when request failed', async () => {
mockGraphQL({
test: /updateProjectV2ItemFieldValue/,
return: {
updateProjectV2ItemFieldValue: null
}
})

const exOctokit = new ExOctokit('gh_token')
const projectV2Item = await exOctokit.updateProjectV2ItemFieldValue(
'project-id',
'item-id',
'field-id',
{ singleSelectOptionId: 'option-id' }
)

expect(projectV2Item).toBeUndefined()
})
})

function mockGraphQL(...mocks: { test: RegExp; return: unknown }[]): jest.Mock {
Expand Down
21 changes: 14 additions & 7 deletions __tests__/update-project-v2-item-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('updateProjectV2ItemField', () => {
beforeEach(() => {
mockGetInput({
'project-url': 'https://github.com/orgs/nipe0324/projects/1',
'github-token': 'gh_token'
'github-token': 'gh_token',
'field-name': 'Status',
'field-value': 'In Progress'
})

debug = mockDebug()
Expand Down Expand Up @@ -45,14 +47,19 @@ describe('updateProjectV2ItemField', () => {

mockFetchProjectV2Id().mockResolvedValue('project-id')
mockAddProjectV2ItemByContentId().mockResolvedValue({ id: 'item-id' })
mockFetchProjectV2FieldByName().mockResolvedValue({ id: 'field-id' })
mockFetchProjectV2FieldByName().mockResolvedValue({
id: 'field-id',
dataType: 'TEXT'
})
mockUpdateProjectV2ItemFieldValue().mockResolvedValue({ id: 'item-id' })

await updateProjectV2ItemField()

expect(debug).toHaveBeenCalledWith('ProjectV2 ID: project-id')
expect(debug).toHaveBeenCalledWith('Item ID: item-id')
expect(debug).toHaveBeenCalledWith('Field ID: field-id')
expect(outputs.projectV2Id).toEqual('project-id')
expect(debug).toHaveBeenCalledWith('Field Value: {"text":"In Progress"}')
expect(outputs.itemId).toEqual('item-id')
})

it(`throws an error when url isn't a valid project url`, async () => {
Expand All @@ -76,13 +83,9 @@ describe('updateProjectV2ItemField', () => {
}
}

const infoSpy = jest.spyOn(core, 'info')
const fetchProjectV2IdMock = mockFetchProjectV2Id()
await expect(updateProjectV2ItemField()).rejects.toThrow(
'Invalid project URL: https://github.com/orgs/github/repositories.'
)
expect(infoSpy).not.toHaveBeenCalled()
expect(fetchProjectV2IdMock).not.toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -114,3 +117,7 @@ function mockAddProjectV2ItemByContentId(): jest.SpyInstance {
function mockFetchProjectV2FieldByName(): jest.SpyInstance {
return jest.spyOn(ExOctokit.prototype, 'fetchProjectV2FieldByName')
}

function mockUpdateProjectV2ItemFieldValue(): jest.SpyInstance {
return jest.spyOn(ExOctokit.prototype, 'updateProjectV2ItemFieldValue')
}
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ inputs:
description: A GitHub personal access token with write access to the project
field-name:
required: true
description: The name of the field to update
description: The name of the field to update. Supported field types are `text`, `number`, `date` and `single_select`
field-value:
required: true
description: The value 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.
109 changes: 78 additions & 31 deletions dist/index.js

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

Loading
Loading