Allow users to add an issue/PR to a board in a different organization

At present, this Action only supports adding an issue or pull
request to a project in the *same organization* as the issue or
pull request itself.

This removes that limitation. If the issue/pull request and
project have different owners, then instead of directly
creating a project item, we will instead create a "draft issue"
which will be added to the board.

Fixes #74.
This commit is contained in:
Tim Rogers
2022-06-30 10:48:09 +01:00
committed by Tim Rogers
parent 31c3cce717
commit 9115f344bd
5 changed files with 321 additions and 79 deletions

68
dist/index.js generated vendored
View File

@@ -46,7 +46,7 @@ const github = __importStar(__nccwpck_require__(5438));
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
const urlParse = /^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/;
function addToProject() {
var _a, _b, _c, _d, _e, _f, _g, _h;
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return __awaiter(this, void 0, void 0, function* () {
const projectUrl = core.getInput('project-url', { required: true });
const ghToken = core.getInput('github-token', { required: true });
@@ -57,9 +57,10 @@ function addToProject() {
.filter(l => l.length > 0)) !== null && _a !== void 0 ? _a : [];
const labelOperator = core.getInput('label-operator').trim().toLocaleLowerCase();
const octokit = github.getOctokit(ghToken);
const urlMatch = projectUrl.match(urlParse);
const issue = (_b = github.context.payload.issue) !== null && _b !== void 0 ? _b : github.context.payload.pull_request;
const issueLabels = ((_c = issue === null || issue === void 0 ? void 0 : issue.labels) !== null && _c !== void 0 ? _c : []).map((l) => l.name.toLowerCase());
const issueOwnerName = (_d = github.context.payload.repository) === null || _d === void 0 ? void 0 : _d.owner.login;
core.debug(`Issue/PR owner: ${issueOwnerName}`);
// Ensure the issue matches our `labeled` filter based on the label-operator.
if (labelOperator === 'and') {
if (!labeled.every(l => issueLabels.includes(l))) {
@@ -80,45 +81,68 @@ function addToProject() {
}
}
core.debug(`Project URL: ${projectUrl}`);
const urlMatch = projectUrl.match(urlParse);
if (!urlMatch) {
throw new Error(`Invalid project URL: ${projectUrl}. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>`);
}
const ownerName = (_d = urlMatch.groups) === null || _d === void 0 ? void 0 : _d.ownerName;
const projectNumber = parseInt((_f = (_e = urlMatch.groups) === null || _e === void 0 ? void 0 : _e.projectNumber) !== null && _f !== void 0 ? _f : '', 10);
const ownerType = (_g = urlMatch.groups) === null || _g === void 0 ? void 0 : _g.ownerType;
const projectOwnerName = (_e = urlMatch.groups) === null || _e === void 0 ? void 0 : _e.ownerName;
const projectNumber = parseInt((_g = (_f = urlMatch.groups) === null || _f === void 0 ? void 0 : _f.projectNumber) !== null && _g !== void 0 ? _g : '', 10);
const ownerType = (_h = urlMatch.groups) === null || _h === void 0 ? void 0 : _h.ownerType;
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType);
core.debug(`Org name: ${ownerName}`);
core.debug(`Project owner: ${projectOwnerName}`);
core.debug(`Project number: ${projectNumber}`);
core.debug(`Owner type: ${ownerType}`);
core.debug(`Project owner type: ${ownerType}`);
// First, use the GraphQL API to request the project's node ID.
const idResp = yield octokit.graphql(`query getProject($ownerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $ownerName) {
const idResp = yield octokit.graphql(`query getProject($projectOwnerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $projectOwnerName) {
projectV2(number: $projectNumber) {
id
}
}
}`, {
ownerName,
projectOwnerName,
projectNumber
});
const projectId = (_h = idResp[ownerTypeQuery]) === null || _h === void 0 ? void 0 : _h.projectV2.id;
const projectId = (_j = idResp[ownerTypeQuery]) === null || _j === void 0 ? void 0 : _j.projectV2.id;
const contentId = issue === null || issue === void 0 ? void 0 : issue.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.
const addResp = yield octokit.graphql(`mutation addIssueToProject($input: AddProjectV2ItemByIdInput!) {
addProjectV2ItemById(input: $input) {
item {
id
// If the issue has the same owner as the project, we can directly
// add a project item. Otherwise, we add a draft issue.
if (issueOwnerName === projectOwnerName) {
core.info('Creating project item');
const addResp = yield octokit.graphql(`mutation addIssueToProject($input: AddProjectV2ItemByIdInput!) {
addProjectV2ItemById(input: $input) {
item {
id
}
}
}
}`, {
input: {
}`, {
input: {
projectId,
contentId
}
});
core.setOutput('itemId', addResp.addProjectV2ItemById.item.id);
}
else {
core.info('Creating draft issue in project');
const addResp = yield octokit.graphql(`mutation addDraftIssueToProject($projectId: ID!, $title: String!) {
addProjectV2DraftIssue(input: {
projectId: $projectId,
title: $title
}) {
projectItem {
id
}
}
}`, {
projectId,
contentId
}
});
core.setOutput('itemId', addResp.addProjectV2ItemById.item.id);
title: issue === null || issue === void 0 ? void 0 : issue.html_url
});
core.setOutput('itemId', addResp.addProjectV2DraftIssue.projectItem.id);
}
});
}
exports.addToProject = addToProject;