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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry possibility #505

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jobs:
_See [Creating a PAT and adding it to your repository](#creating-a-pat-and-adding-it-to-your-repository) for more details_
- <a name="labeled">`labeled`</a> **(optional)** is a comma-separated list of labels used to filter applicable issues. When this key is provided, an issue must have _one_ of the labels in the list to be added to the project. Omitting this key means that any issue will be added.
- <a name="labeled">`label-operator`</a> **(optional)** is the behavior of the labels filter, either `AND`, `OR` or `NOT` that controls if the issue should be matched with `all` `labeled` input or any of them, default is `OR`.
- <a name="retry-limit">`retry-limit`</a> **(optional)** is the number of retries done to add an item to a board, default is 0.

## Supported Events

Expand Down Expand Up @@ -156,6 +157,7 @@ Use the [Add To GitHub Projects](https://github.com/marketplace/actions/add-to-g
## Development

To get started contributing to this project, clone it and install dependencies.
If you have made any changes that you want to contribute back to the project fork this repository and create a PR on that fork.
Note that this action runs in Node.js 16.x, so we recommend using that version
of Node (see "engines" in this action's package.json for details).

Expand Down
25 changes: 24 additions & 1 deletion __tests__/add-to-project.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 {addToProject, mustGetOwnerTypeQuery} from '../src/add-to-project'
import {addToProject, mustGetOwnerTypeQuery, withRetries} from '../src/add-to-project'

describe('addToProject', () => {
let outputs: Record<string, string>
Expand Down Expand Up @@ -800,6 +800,29 @@ describe('mustGetOwnerTypeQuery', () => {
})
})

describe('withRetries', () => {
test('should succeed on successful callback', async () => {
withRetries(0, 0, () => {
return Promise.resolve('some string')
}).then(data => {
expect(data).toBe('some string')
})
})

test('should fail when reaching retry limit', async () => {
let invocations = 0

expect(() =>
withRetries(0, 3, () => {
invocations = invocations + 1
throw new Error('some error')
}),
).toThrow('some error')

expect(invocations).toEqual(4)
})
})

function mockGetInput(mocks: Record<string, string>): jest.SpyInstance {
const mock = (key: string) => mocks[key] ?? ''
return jest.spyOn(core, 'getInput').mockImplementation(mock)
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
label-operator:
required: false
description: The behavior of the labels filter, AND to match all labels, OR to match any label, NOT to exclude any listed label (default is OR)
retry-limit:
required: false
description: The number of retries used to add an item to a project (default is 0)
outputs:
itemId:
description: The ID of the item that was added to the project
Expand Down
67 changes: 41 additions & 26 deletions src/add-to-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function addToProject(): Promise<void> {
.map(l => l.trim().toLowerCase())
.filter(l => l.length > 0) ?? []
const labelOperator = core.getInput('label-operator').trim().toLocaleLowerCase()
const inputRetryLimit = core.getInput('retry-limit')
const retryLimit = inputRetryLimit ? Number(inputRetryLimit) : 0

const octokit = github.getOctokit(ghToken)

Expand Down Expand Up @@ -117,47 +119,60 @@ export async function addToProject(): Promise<void> {
if (issueOwnerName === projectOwnerName) {
core.info('Creating project item')

const addResp = await octokit.graphql<ProjectAddItemResponse>(
`mutation addIssueToProject($input: AddProjectV2ItemByIdInput!) {
addProjectV2ItemById(input: $input) {
item {
id
const addResp = await withRetries(0, retryLimit, () =>
octokit.graphql<ProjectAddItemResponse>(
`mutation addIssueToProject($input: AddProjectV2ItemByIdInput!) {
addProjectV2ItemById(input: $input) {
item {
id
}
}
}
}`,
{
input: {
projectId,
contentId,
}`,
{
input: {
projectId,
contentId,
},
},
},
),
)

core.setOutput('itemId', addResp.addProjectV2ItemById.item.id)
} else {
core.info('Creating draft issue in project')

const addResp = await octokit.graphql<ProjectV2AddDraftIssueResponse>(
`mutation addDraftIssueToProject($projectId: ID!, $title: String!) {
addProjectV2DraftIssue(input: {
projectId: $projectId,
title: $title
}) {
projectItem {
id
const addResp = await withRetries(0, retryLimit, () =>
octokit.graphql<ProjectV2AddDraftIssueResponse>(
`mutation addDraftIssueToProject($projectId: ID!, $title: String!) {
addProjectV2DraftIssue(input: {
projectId: $projectId,
title: $title
}) {
projectItem {
id
}
}
}
}`,
{
projectId,
title: issue?.html_url,
},
}`,
{
projectId,
title: issue?.html_url,
},
),
)

core.setOutput('itemId', addResp.addProjectV2DraftIssue.projectItem.id)
}
}

export function withRetries<T>(retryNumber: number, retryLimit: number, callback: () => Promise<T>): Promise<T> {
try {
return callback()
} catch (err) {
if (retryNumber < retryLimit) return withRetries(retryNumber + 1, retryLimit, callback)
else throw err
}
}

export function mustGetOwnerTypeQuery(ownerType?: string): 'organization' | 'user' {
const ownerTypeQuery = ownerType === 'orgs' ? 'organization' : ownerType === 'users' ? 'user' : null

Expand Down