mirror of
https://github.com/actions/add-to-project.git
synced 2025-12-11 12:37:16 +00:00
Fix orgs/users in URL
This commit is contained in:
45
src/main.ts
45
src/main.ts
@@ -4,9 +4,32 @@ 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/<orgName>/projects/<projectNumber>
|
||||
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
|
||||
const urlParse =
|
||||
/^(?:https:\/\/)?github\.com\/orgs|users\/(?<orgName>[^/]+)\/projects\/(?<projectNumber>\d+)/
|
||||
/^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/
|
||||
|
||||
const projectQuery = (ownerType?: string): string => {
|
||||
const ownerTypeQuery =
|
||||
ownerType === 'orgs'
|
||||
? 'organization'
|
||||
: ownerType === 'users'
|
||||
? 'user'
|
||||
: null
|
||||
|
||||
if (!ownerTypeQuery) {
|
||||
throw new Error(
|
||||
`Unsupported ownerType: ${ownerType}. Must be one of 'orgs' or 'users'`
|
||||
)
|
||||
}
|
||||
|
||||
return `query getProject($ownerName: String!, $projectNumber: Int!) {
|
||||
${ownerTypeQuery}(login: $ownerName) {
|
||||
projectNext(number: $projectNumber) {
|
||||
id
|
||||
}
|
||||
}
|
||||
}`
|
||||
}
|
||||
|
||||
interface ProjectNodeIDResponse {
|
||||
organization: {
|
||||
@@ -34,27 +57,23 @@ async function run(): Promise<void> {
|
||||
|
||||
if (!urlMatch) {
|
||||
throw new Error(
|
||||
`Invalid project URL: ${projectUrl}. Project URL should match the format https://github.com/orgs/<orgName>/projects/<projectNumber>`
|
||||
`Invalid project URL: ${projectUrl}. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>`
|
||||
)
|
||||
}
|
||||
|
||||
const orgName = urlMatch.groups?.orgName
|
||||
const ownerName = urlMatch.groups?.ownerName
|
||||
const projectNumber = parseInt(urlMatch.groups?.projectNumber ?? '', 10)
|
||||
const ownerType = urlMatch.groups?.ownerType
|
||||
|
||||
core.debug(`Org name: ${orgName}`)
|
||||
core.debug(`Org name: ${ownerName}`)
|
||||
core.debug(`Project number: ${projectNumber}`)
|
||||
core.debug(`Owner type: ${ownerType}`)
|
||||
|
||||
// First, use the GraphQL API to request the project's node ID.
|
||||
const idResp = await octokit.graphql<ProjectNodeIDResponse>(
|
||||
`query getProject($orgName: String!, $projectNumber: Int!) {
|
||||
organization(login: $orgName) {
|
||||
projectNext(number: $projectNumber) {
|
||||
id
|
||||
}
|
||||
}
|
||||
}`,
|
||||
projectQuery(ownerType),
|
||||
{
|
||||
orgName,
|
||||
ownerName,
|
||||
projectNumber
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user