mirror of
https://github.com/actions/add-to-project.git
synced 2025-12-11 04:32:47 +00:00
Handle organization/user in gql response
This commit is contained in:
43
dist/index.js
generated
vendored
43
dist/index.js
generated
vendored
@@ -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.
|
// TODO: Ensure this (and the Octokit client) works for non-github.com URLs, as well.
|
||||||
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
|
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
|
||||||
const urlParse = /^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/;
|
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() {
|
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* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const projectUrl = core.getInput('project-url', { required: true });
|
const projectUrl = core.getInput('project-url', { required: true });
|
||||||
const ghToken = core.getInput('github-token', { 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 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 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 ownerType = (_d = urlMatch.groups) === null || _d === void 0 ? void 0 : _d.ownerType;
|
||||||
|
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType);
|
||||||
core.debug(`Org name: ${ownerName}`);
|
core.debug(`Org name: ${ownerName}`);
|
||||||
core.debug(`Project number: ${projectNumber}`);
|
core.debug(`Project number: ${projectNumber}`);
|
||||||
core.debug(`Owner type: ${ownerType}`);
|
core.debug(`Owner type: ${ownerType}`);
|
||||||
// First, use the GraphQL API to request the project's node ID.
|
// 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,
|
ownerName,
|
||||||
projectNumber
|
projectNumber
|
||||||
});
|
});
|
||||||
const projectId = idResp.organization.projectNext.id;
|
const projectId = (_e = idResp[ownerTypeQuery]) === null || _e === void 0 ? void 0 : _e.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 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(`Project node ID: ${projectId}`);
|
||||||
core.debug(`Content ID: ${contentId}`);
|
core.debug(`Content ID: ${contentId}`);
|
||||||
// Next, use the GraphQL API to add the issue to the project.
|
// Next, use the GraphQL API to add the issue to the project.
|
||||||
@@ -108,6 +98,17 @@ run()
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
process.exit(0);
|
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
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
57
src/main.ts
57
src/main.ts
@@ -8,31 +8,14 @@ import * as github from '@actions/github'
|
|||||||
const urlParse =
|
const urlParse =
|
||||||
/^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/
|
/^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/
|
||||||
|
|
||||||
const projectQuery = (ownerType?: string): string => {
|
interface ProjectNodeIDResponse {
|
||||||
const ownerTypeQuery =
|
organization?: {
|
||||||
ownerType === 'orgs'
|
projectNext: {
|
||||||
? 'organization'
|
id: string
|
||||||
: 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!) {
|
user?: {
|
||||||
${ownerTypeQuery}(login: $ownerName) {
|
|
||||||
projectNext(number: $projectNumber) {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProjectNodeIDResponse {
|
|
||||||
organization: {
|
|
||||||
projectNext: {
|
projectNext: {
|
||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
@@ -64,6 +47,7 @@ async function run(): Promise<void> {
|
|||||||
const ownerName = urlMatch.groups?.ownerName
|
const ownerName = urlMatch.groups?.ownerName
|
||||||
const projectNumber = parseInt(urlMatch.groups?.projectNumber ?? '', 10)
|
const projectNumber = parseInt(urlMatch.groups?.projectNumber ?? '', 10)
|
||||||
const ownerType = urlMatch.groups?.ownerType
|
const ownerType = urlMatch.groups?.ownerType
|
||||||
|
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType)
|
||||||
|
|
||||||
core.debug(`Org name: ${ownerName}`)
|
core.debug(`Org name: ${ownerName}`)
|
||||||
core.debug(`Project number: ${projectNumber}`)
|
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.
|
// First, use the GraphQL API to request the project's node ID.
|
||||||
const idResp = await octokit.graphql<ProjectNodeIDResponse>(
|
const idResp = await octokit.graphql<ProjectNodeIDResponse>(
|
||||||
projectQuery(ownerType),
|
`query getProject($ownerName: String!, $projectNumber: Int!) {
|
||||||
|
${ownerTypeQuery}(login: $ownerName) {
|
||||||
|
projectNext(number: $projectNumber) {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
{
|
{
|
||||||
ownerName,
|
ownerName,
|
||||||
projectNumber
|
projectNumber
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const projectId = idResp.organization.projectNext.id
|
const projectId = idResp[ownerTypeQuery]?.projectNext.id
|
||||||
const contentId =
|
const contentId =
|
||||||
github.context.payload.issue?.node_id ??
|
github.context.payload.issue?.node_id ??
|
||||||
github.context.payload.pull_request?.node_id
|
github.context.payload.pull_request?.node_id
|
||||||
@@ -114,3 +104,20 @@ run()
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
process.exit(0)
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user