Handle organization/user in gql response

This commit is contained in:
Jonathan Clem
2022-01-31 14:24:26 -05:00
parent bdcdb34109
commit 68fd81353b
3 changed files with 55 additions and 47 deletions

43
dist/index.js generated vendored
View File

@@ -41,25 +41,8 @@ const github = __importStar(__nccwpck_require__(5438));
// 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 projectQuery = (ownerType) => {
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
}
}
}`;
};
function run() {
var _a, _b, _c, _d, _e, _f, _g;
var _a, _b, _c, _d, _e, _f, _g, _h;
return __awaiter(this, void 0, void 0, function* () {
const projectUrl = core.getInput('project-url', { required: true });
const ghToken = core.getInput('github-token', { required: true });
@@ -72,16 +55,23 @@ function run() {
const ownerName = (_a = urlMatch.groups) === null || _a === void 0 ? void 0 : _a.ownerName;
const projectNumber = parseInt((_c = (_b = urlMatch.groups) === null || _b === void 0 ? void 0 : _b.projectNumber) !== null && _c !== void 0 ? _c : '', 10);
const ownerType = (_d = urlMatch.groups) === null || _d === void 0 ? void 0 : _d.ownerType;
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType);
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 = yield octokit.graphql(projectQuery(ownerType), {
const idResp = yield octokit.graphql(`query getProject($ownerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $ownerName) {
projectNext(number: $projectNumber) {
id
}
}
}`, {
ownerName,
projectNumber
});
const projectId = idResp.organization.projectNext.id;
const contentId = (_f = (_e = github.context.payload.issue) === null || _e === void 0 ? void 0 : _e.node_id) !== null && _f !== void 0 ? _f : (_g = github.context.payload.pull_request) === null || _g === void 0 ? void 0 : _g.node_id;
const projectId = (_e = idResp[ownerTypeQuery]) === null || _e === void 0 ? void 0 : _e.projectNext.id;
const contentId = (_g = (_f = github.context.payload.issue) === null || _f === void 0 ? void 0 : _f.node_id) !== null && _g !== void 0 ? _g : (_h = github.context.payload.pull_request) === null || _h === void 0 ? void 0 : _h.node_id;
core.debug(`Project node ID: ${projectId}`);
core.debug(`Content ID: ${contentId}`);
// Next, use the GraphQL API to add the issue to the project.
@@ -108,6 +98,17 @@ run()
.then(() => {
process.exit(0);
});
function mustGetOwnerTypeQuery(ownerType) {
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 ownerTypeQuery;
}
/***/ }),

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@@ -8,31 +8,14 @@ import * as github from '@actions/github'
const urlParse =
/^(?: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'`
)
interface ProjectNodeIDResponse {
organization?: {
projectNext: {
id: string
}
}
return `query getProject($ownerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $ownerName) {
projectNext(number: $projectNumber) {
id
}
}
}`
}
interface ProjectNodeIDResponse {
organization: {
user?: {
projectNext: {
id: string
}
@@ -64,6 +47,7 @@ async function run(): Promise<void> {
const ownerName = urlMatch.groups?.ownerName
const projectNumber = parseInt(urlMatch.groups?.projectNumber ?? '', 10)
const ownerType = urlMatch.groups?.ownerType
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType)
core.debug(`Org name: ${ownerName}`)
core.debug(`Project number: ${projectNumber}`)
@@ -71,14 +55,20 @@ async function run(): Promise<void> {
// First, use the GraphQL API to request the project's node ID.
const idResp = await octokit.graphql<ProjectNodeIDResponse>(
projectQuery(ownerType),
`query getProject($ownerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $ownerName) {
projectNext(number: $projectNumber) {
id
}
}
}`,
{
ownerName,
projectNumber
}
)
const projectId = idResp.organization.projectNext.id
const projectId = idResp[ownerTypeQuery]?.projectNext.id
const contentId =
github.context.payload.issue?.node_id ??
github.context.payload.pull_request?.node_id
@@ -114,3 +104,20 @@ run()
.then(() => {
process.exit(0)
})
function mustGetOwnerTypeQuery(ownerType?: string): 'organization' | 'user' {
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 ownerTypeQuery
}