Skip to content

Commit

Permalink
adds option to fetch issues with iteration fields (closes #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfroeller committed Dec 18, 2024
1 parent 10d02a0 commit ba257af
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 50 deletions.
40 changes: 35 additions & 5 deletions src/v1/adapters/github/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Repository } from "./scopes";
import { Iteration, Repository } from "./scopes";
import {
type GITHUB_REPOSITORY_SCOPES,
GITHUB_PROJECT_SCOPES,
type PageSize,
GRAMMATICAL_NUMBER,
type GITHUB_MILESTONE_ISSUE_STATES,
GITHUB_ITERATION_SCOPES,
} from "./types";
import { DEV_MODE } from "../../../environment";

Expand All @@ -24,7 +25,7 @@ export const GITHUB_QUOTA = `{
export const Project = (
project_name: string | number,
project_scopes: GITHUB_PROJECT_SCOPES[],
repository_query: string | null = null,
child_query: string | null = null
) => {
const name_is_text = typeof project_name === "string";
const head = name_is_text
Expand All @@ -47,9 +48,10 @@ export const Project = (
readme
` : ""}
${project_scopes.includes(GITHUB_PROJECT_SCOPES.REPOSITORIES_LINKED) &&
repository_query
? repository_query
${(project_scopes.includes(GITHUB_PROJECT_SCOPES.REPOSITORIES_LINKED) ||
project_scopes.includes(GITHUB_PROJECT_SCOPES.ITERATIONS)) &&
child_query
? child_query
: ""
}
Expand Down Expand Up @@ -120,3 +122,31 @@ export const AccountScopeEntryRoot = (

return query;
};

/**
* Retrieves the iterations of a project.
*
* @param {string | number} project_name - The name or ID of the project.
* @param {number} [pageSize=100] - The amount of iterations to fetch at once.
* @param {string | null} [continueAfter=null] - The cursor to continue after.
* @param {string} [iterationFieldName="Sprint"] - The name of the iteration field.
* @param {GITHUB_ITERATION_SCOPES[]} [scopes=[GITHUB_ITERATION_SCOPES.INFO]] - The scopes of the iterations.
*
* @return {string} The generated GraphQL query.
*/
export const getProjectIterationIssues = (
project_name: string | number,
pageSize: number = 100,
continueAfter: string | null = null,
iterationFieldName: string = "Sprint",
scopes: GITHUB_ITERATION_SCOPES[] = [GITHUB_ITERATION_SCOPES.INFO]
) => {
const iteration = new Iteration({
pageSize,
continueAfter,
iterationFieldName,
scopes
});

return Project(project_name, [GITHUB_PROJECT_SCOPES.ITERATIONS], iteration.getQuery());
};
94 changes: 78 additions & 16 deletions src/v1/adapters/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GITHUB_QUOTA,
Project,
getAllRepositoriesInProject,
getProjectIterationIssues,
} from "./graphql";
import { fetchGithubDataUsingGraphql, fetchRateLimit } from "./functions/fetch";
import { createPinoLogger } from "@bogeychan/elysia-logger";
Expand All @@ -18,6 +19,7 @@ import { guardEndpoints } from "../plugins";
import {
GITHUB_ACCOUNT_SCOPES,
GITHUB_AUTHENTICATION_STRATEGY_OPTIONS,
GITHUB_ITERATION_SCOPES,
GITHUB_MILESTONE_ISSUE_STATES,
GITHUB_PROJECT_SCOPES,
GITHUB_REPOSITORY_SCOPES,
Expand All @@ -26,6 +28,7 @@ import {
} from "../github/types";
import {
GITHUB_ACCOUNT_PARAMS,
GITHUB_ITERATION_PARAMS,
GITHUB_PROJECT_PARAMS,
GITHUB_REPOSITORY_PARAMS,
} from "./params";
Expand All @@ -50,17 +53,17 @@ export const GITHUB_APP_WEBHOOKS = new Elysia({ prefix: "/webhooks" }).post(
"",
async (ctx) => {
const child = log.child(ctx);
if (DEV_MODE) child.info("webhook received");
if (DEV_MODE) child.info("webhook received");

const eventType = ctx.headers["x-github-event"] as keyof WebhookEventMap;

if (eventType === "projects_v2_item") {
if (DEV_MODE) log.info("projects_v2_item webhook received");

const payload = ctx.body as ProjectsV2ItemEvent;

if (
(payload.action === "edited" || payload.action === "created" || payload.action === "converted" || payload.action === "restored") &&
(payload.action === "edited" || payload.action === "created" || payload.action === "converted" || payload.action === "restored") &&
"changes" in payload &&
"field_value" in payload.changes &&
payload.changes?.field_value?.field_type === "iteration" &&
Expand All @@ -69,8 +72,8 @@ export const GITHUB_APP_WEBHOOKS = new Elysia({ prefix: "/webhooks" }).post(
) {
const octokit = await getOctokitObject(
GITHUB_AUTHENTICATION_STRATEGY_OPTIONS.APP,
payload.installation.id,
ctx.set
payload.installation.id,
ctx.set
);

const fieldValue = await octokit.graphql(`
Expand All @@ -93,19 +96,19 @@ export const GITHUB_APP_WEBHOOKS = new Elysia({ prefix: "/webhooks" }).post(

const result = await handleProjectItemChange(octokit, {
id: payload.projects_v2_item.node_id,
content: {
content: {
id: payload.projects_v2_item.content_node_id
},
fieldValues: {
nodes: [{
field: {
field: {
name: "Sprint"
},
value: fieldValue.node.fieldValueByName?.title || null
}]
},
});

if (DEV_MODE) child.info({ result }, '[Sprint Label Mutation]');
}
}
Expand All @@ -115,11 +118,11 @@ export const GITHUB_APP_WEBHOOKS = new Elysia({ prefix: "/webhooks" }).post(
return ctx.body;
},
{
detail: {
description:
"Receives a webhook event of the changes that happened in the scopes that this microservice is subscribed to, on your GitHub-App installation.",
tags: ["github", "webhooks"],
},
detail: {
description:
"Receives a webhook event of the changes that happened in the scopes that this microservice is subscribed to, on your GitHub-App installation.",
tags: ["github", "webhooks"],
},
},
);

Expand Down Expand Up @@ -428,6 +431,65 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
},
)

/**
* Request project iteration issues
*/
.get(
"/iterations/issues",
async ({ fetchParams, params: { login_name, project_id_or_name }, query, set }) => {
const response = await fetchGithubDataUsingGraphql<{
organization?: { projectV2: ProjectV2 };
user?: { projectV2: ProjectV2 };
}>(
AccountScopeEntryRoot(
login_name,
getProjectIterationIssues(
project_id_or_name,
query.pageSize,
query.continueAfter,
query.iterationFieldName,
(query.scopes?.split(",") ?? []) as GITHUB_ITERATION_SCOPES[]
),
login_type
),
fetchParams.auth,
set,
fetchParams.auth_type,
);

const user = FETCH_PROJECTS_FROM_ORGANIZATION ? "organization" : "user";
if (response?.data && user in response.data && response.data[user]?.projectV2) {
const items = response.data[user].projectV2.items;
// Property 'state' does not exist on type 'DraftIssue | Issue | PullRequest'. -> This is not typed correctly...
// @ts-ignore
const openNodes = items?.nodes?.filter(node => node?.content?.state === 'OPEN') ?? [];
// @ts-ignore
const closedNodes = items?.nodes?.filter(node => node?.content?.state === 'CLOSED') ?? [];

response.data[user].projectV2.items = {
totalCount: items.totalCount,
pageInfo: items.pageInfo,
open_issues: { nodes: openNodes },
closed_issues: { nodes: closedNodes }
} as any;
}

return response;
},
{
query: t.Object({
pageSize: t.Optional(t.Numeric({ minimum: 1, maximum: 100 })),
continueAfter: t.Optional(t.String()),
iterationFieldName: t.Optional(t.String()),
scopes: t.Optional(t.String()) // t.Optional(t.Array(t.Enum(GITHUB_ITERATION_SCOPES)))
}),
detail: {
description: `Request project iteration issues for ${login_type}. Scopes: ${GITHUB_ITERATION_PARAMS} (/projects/{project_id}/iteration/issues?iteration_field_name=Sprint).`,
tags: ["github"],
},
},
)

/**
* Request repositories only in the account project. No infos.
*/
Expand Down Expand Up @@ -1476,7 +1538,7 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
},
},
),
),
),
),
),
),
Expand Down
4 changes: 4 additions & 0 deletions src/v1/adapters/github/params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
GITHUB_ACCOUNT_SCOPES,
GITHUB_ITERATION_SCOPES,
GITHUB_PROJECT_SCOPES,
GITHUB_REPOSITORY_SCOPES,
} from "./types";
Expand All @@ -13,3 +14,6 @@ export const GITHUB_PROJECT_PARAMS = JSON.stringify(
export const GITHUB_REPOSITORY_PARAMS = JSON.stringify(
Object.values(GITHUB_REPOSITORY_SCOPES),
);
export const GITHUB_ITERATION_PARAMS = JSON.stringify(
Object.values(GITHUB_ITERATION_SCOPES),
);
Loading

0 comments on commit ba257af

Please sign in to comment.