Fix orgs/users in URL

This commit is contained in:
Jonathan Clem
2022-01-31 14:19:41 -05:00
parent 902f1883f1
commit bdcdb34109
3 changed files with 61 additions and 29 deletions

View File

@@ -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
}
)