Skip to content

Commit

Permalink
Move sendQuery to GitHubOrg.sendGraphQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 25, 2023
1 parent ed57c6d commit 596c22f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 116 deletions.
31 changes: 13 additions & 18 deletions src/api/github/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('helpers', function () {
const org = GETSENTRY_ORG;

it('addIssueToGlobalIssuesProject should return project item id from project', async function () {
const octokit = {
org.api = {
graphql: jest
.fn()
.mockReturnValue({ addProjectV2ItemById: { item: { id: '12345' } } }),
Expand All @@ -16,14 +16,13 @@ describe('helpers', function () {
org,
'issueNodeId',
'test-repo',
1,
octokit
1
)
).toEqual('12345');
});

it('getAllProjectFieldNodeIds should return timestamp from project', async function () {
const octokit = {
org.api = {
graphql: jest.fn().mockReturnValue({
node: {
options: [
Expand All @@ -35,7 +34,7 @@ describe('helpers', function () {
}),
};
expect(
await helpers.getAllProjectFieldNodeIds('projectFieldId', octokit)
await helpers.getAllProjectFieldNodeIds(org, 'projectFieldId')
).toEqual({
'Waiting for: Product Owner': 1,
'Waiting for: Support': 2,
Expand All @@ -44,22 +43,18 @@ describe('helpers', function () {
});

it('getKeyValueFromProjectField should return timestamp from project', async function () {
const octokit = {
org.api = {
graphql: jest.fn().mockReturnValue({
node: { fieldValueByName: { name: 'Product Area: Test' } },
}),
};
expect(
await helpers.getKeyValueFromProjectField(
'issueNodeId',
'fieldName',
octokit
)
await helpers.getKeyValueFromProjectField(org, 'issueNodeId', 'fieldName')
).toEqual('Product Area: Test');
});

it('getIssueDueDateFromProject should return timestamp from project', async function () {
const octokit = {
org.api = {
graphql: jest.fn().mockReturnValue({
node: {
fieldValues: {
Expand All @@ -76,18 +71,18 @@ describe('helpers', function () {
}),
};
expect(
await helpers.getIssueDueDateFromProject(org, 'issueNodeId', octokit)
await helpers.getIssueDueDateFromProject(org, 'issueNodeId')
).toEqual('2023-06-23T18:00:00.000Z');
});

it('getIssueDetailsFromNodeId should return timestamp from project', async function () {
const octokit = {
it('getIssueDetailsFromNodeId should return issue details', async function () {
org.api = {
graphql: jest.fn().mockReturnValue({
node: { number: 1, repository: { name: 'test-repo' } },
}),
};
expect(
await helpers.getIssueDetailsFromNodeId('issueNodeId', octokit)
).toEqual({ number: 1, repo: 'test-repo' });
expect(await helpers.getIssueDetailsFromNodeId(org, 'issueNodeId')).toEqual(
{ number: 1, repo: 'test-repo' }
);
});
});
57 changes: 20 additions & 37 deletions src/api/github/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { Octokit } from '@octokit/rest';
import * as Sentry from '@sentry/node';

import { GitHubOrg } from '@api/github/org';

async function sendQuery(query: string, data: object, octokit: Octokit) {
let response: any;
try {
response = await octokit.graphql(query);
} catch (err) {
Sentry.setContext('data', data);
Sentry.captureException(err);
}
return response;
}

export async function addIssueToGlobalIssuesProject(
org: GitHubOrg,
issueNodeId: string | undefined,
repo: string,
issueNumber: number,
octokit: Octokit
issueNumber: number
): Promise<string> {
if (issueNodeId == null) {
Sentry.captureException(
Expand All @@ -38,18 +25,17 @@ export async function addIssueToGlobalIssuesProject(
repo,
issueNumber,
};
const response = await sendQuery(
const response = await org.sendGraphQuery(
addIssueToGlobalIssuesProjectMutation,
data,
octokit
data
);

return response?.addProjectV2ItemById.item.id;
}

export async function getAllProjectFieldNodeIds(
projectFieldId: string,
octokit: Octokit
org: GitHubOrg,
projectFieldId: string
) {
const queryForProjectFieldNodeIDs = `query{
node(id: "${projectFieldId}") {
Expand All @@ -65,7 +51,7 @@ export async function getAllProjectFieldNodeIds(
const data = {
projectFieldId,
};
const response = await sendQuery(queryForProjectFieldNodeIDs, data, octokit);
const response = await org.sendGraphQuery(queryForProjectFieldNodeIDs, data);

return response?.node.options.reduce((acc, { name, id }) => {
acc[name] = id;
Expand All @@ -77,12 +63,11 @@ export async function modifyProjectIssueField(
org: GitHubOrg,
itemId: string,
projectFieldOption: string,
fieldId: string,
octokit: Octokit
fieldId: string
) {
const projectFieldNodeIDMapping = await getAllProjectFieldNodeIds(
fieldId,
octokit
org,
fieldId
);
const singleSelectOptionId = projectFieldNodeIDMapping[projectFieldOption];
const modifyProjectIssueFieldMutation = `mutation {
Expand All @@ -106,15 +91,14 @@ export async function modifyProjectIssueField(
projectFieldOption,
fieldId,
};
await sendQuery(modifyProjectIssueFieldMutation, data, octokit);
await org.sendGraphQuery(modifyProjectIssueFieldMutation, data);
}

export async function modifyDueByDate(
org: GitHubOrg,
itemId: string,
projectFieldOption: string,
fieldId: string,
octokit: Octokit
fieldId: string
) {
const modifyDueByDateMutation = `mutation {
updateProjectV2ItemFieldValue(
Expand All @@ -138,13 +122,13 @@ export async function modifyDueByDate(
projectFieldOption,
fieldId,
};
await sendQuery(modifyDueByDateMutation, data, octokit);
await org.sendGraphQuery(modifyDueByDateMutation, data);
}

export async function getKeyValueFromProjectField(
org: GitHubOrg,
issueNodeId: string,
fieldName: string,
octokit: Octokit
fieldName: string
) {
const query = `query{
node(id: "${issueNodeId}") {
Expand All @@ -168,15 +152,14 @@ export async function getKeyValueFromProjectField(
issueNodeId,
fieldName,
};
const response = await sendQuery(query, data, octokit);
const response = await org.sendGraphQuery(query, data);

return response?.node.fieldValueByName?.name;
}

export async function getIssueDueDateFromProject(
org: GitHubOrg,
issueNodeId: string,
octokit: Octokit
issueNodeId: string
) {
// Use fieldValues (and iterate) instead of fieldValuesByName in case the name ever changes
const query = `query{
Expand Down Expand Up @@ -204,7 +187,7 @@ export async function getIssueDueDateFromProject(
const data = {
issueNodeId,
};
const response = await sendQuery(query, data, octokit);
const response = await org.sendGraphQuery(query, data);
// When the response due date is empty, the node doesn't exist so we default to empty string
const issueDueDateInfoNode =
response?.node.fieldValues.nodes.find(
Expand All @@ -214,8 +197,8 @@ export async function getIssueDueDateFromProject(
}

export async function getIssueDetailsFromNodeId(
issueNodeId: string,
octokit: Octokit
org: GitHubOrg,
issueNodeId: string
) {
const query = `query {
node(id:"${issueNodeId}") {
Expand All @@ -231,7 +214,7 @@ export async function getIssueDetailsFromNodeId(
const data = {
issueNodeId,
};
const response = await sendQuery(query, data, octokit);
const response = await org.sendGraphQuery(query, data);

return {
number: response?.node.number,
Expand Down
12 changes: 12 additions & 0 deletions src/api/github/org.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/rest';
import * as Sentry from '@sentry/node';

import {
AppAuthStrategyOptions,
Expand Down Expand Up @@ -48,4 +49,15 @@ export class GitHubOrg {
});
}
}

async sendGraphQuery(query: string, data: object) {
let response: any;
try {
response = await this.api.graphql(query);
} catch (err) {
Sentry.setContext('data', data);
Sentry.captureException(err);
}
return response;
}
}
15 changes: 5 additions & 10 deletions src/brain/issueLabelHandler/followups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,14 @@ export async function updateCommunityFollowups({
org,
payload.issue.node_id,
repo,
issueNumber,
octokit
issueNumber
);

await modifyProjectIssueField(
org,
itemId,
WAITING_FOR_PRODUCT_OWNER_LABEL,
org.project.status_field_id,
octokit
org.project.status_field_id
);

tx.finish();
Expand Down Expand Up @@ -158,16 +156,14 @@ export async function ensureOneWaitingForLabel({
org,
payload.issue.node_id,
repo,
issueNumber,
octokit
issueNumber
);

await modifyProjectIssueField(
org,
itemId,
labelName,
org.project.status_field_id,
octokit
org.project.status_field_id
);

let timeToRespondBy;
Expand All @@ -189,8 +185,7 @@ export async function ensureOneWaitingForLabel({
org,
itemId,
timeToRespondBy,
org.project.response_due_date_field_id,
octokit
org.project.response_due_date_field_id
);

tx.finish();
Expand Down
Loading

0 comments on commit 596c22f

Please sign in to comment.