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

Allow projects outside github.com #338

Merged
merged 8 commits into from
Apr 3, 2023
44 changes: 29 additions & 15 deletions __tests__/add-to-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,24 +572,19 @@ describe('addToProject', () => {
const infoSpy = jest.spyOn(core, 'info')
const gqlMock = mockGraphQL()
await expect(addToProject()).rejects.toThrow(
'https://github.com/orgs/github/repositories. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>',
'Invalid project URL: https://github.com/orgs/github/repositories. Project URL should match the format <GitHub server domain name>/<orgs-or-users>/<ownerName>/projects/<projectNumber>',
)
expect(infoSpy).not.toHaveBeenCalled()
expect(gqlMock).not.toHaveBeenCalled()
})

test(`throws an error when url isn't under the github.com domain`, async () => {
mockGetInput({
'project-url': 'https://notgithub.com/orgs/github/projects/1',
'github-token': 'gh_token',
})

test(`works with URLs that are not under the github.com domain`, async () => {
github.context.payload = {
issue: {
number: 1,
labels: [],
labels: [{name: 'bug'}],
// eslint-disable-next-line camelcase
html_url: 'https://github.com/actions/add-to-project/issues/74',
html_url: 'https://notgithub.com/actions/add-to-project/issues/74',
},
repository: {
name: 'add-to-project',
Expand All @@ -599,13 +594,32 @@ describe('addToProject', () => {
},
}

const infoSpy = jest.spyOn(core, 'info')
const gqlMock = mockGraphQL()
await expect(addToProject()).rejects.toThrow(
'https://notgithub.com/orgs/github/projects/1. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>',
mockGraphQL(
{
test: /getProject/,
return: {
organization: {
projectV2: {
id: 'project-id',
},
},
},
},
{
test: /addProjectV2ItemById/,
return: {
addProjectV2ItemById: {
item: {
id: 'project-item-id',
},
},
},
},
)
expect(infoSpy).not.toHaveBeenCalled()
expect(gqlMock).not.toHaveBeenCalled()

await addToProject()

expect(outputs.itemId).toEqual('project-item-id')
})

test('constructs the correct graphQL query given an organization owner', async () => {
Expand Down
6 changes: 2 additions & 4 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/add-to-project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as core from '@actions/core'
import * as github from '@actions/github'

// TODO: Ensure this (and the Octokit client) works for non-github.com URLs, as well.
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
const urlParse =
/^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/
const urlParse = /\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/

interface ProjectNodeIDResponse {
organization?: {
Expand Down Expand Up @@ -80,7 +77,7 @@ export async function addToProject(): Promise<void> {

if (!urlMatch) {
throw new Error(
`Invalid project URL: ${projectUrl}. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>`,
`Invalid project URL: ${projectUrl}. Project URL should match the format <GitHub server domain name>/<orgs-or-users>/<ownerName>/projects/<projectNumber>`,
)
}

Expand Down