Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Some fixes:
Browse files Browse the repository at this point in the history
* `discussions-trigger`: added some uses of `txt` to make it more
  readable and other minor reformatting for the gql queries

* Call to `verifyIsFromGitHub` should be negated

* `verifyIsFromGitHub` doesn't need to be exported
  • Loading branch information
elibarzilay committed Aug 11, 2021
1 parent 05587bd commit 17fdbe2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
77 changes: 39 additions & 38 deletions src/discussions-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { gql } from "@apollo/client/core";
import * as crypto from "crypto";
import { reply } from "./util/reply";
import { httpLog, shouldRunRequest } from "./util/verify";
import { txt } from "./util/util";

export async function run(context: Context, req: HttpRequest) {
httpLog(context, req);
Expand Down Expand Up @@ -43,18 +44,21 @@ function extractNPMReference(discussion: Discussion) {
return undefined;
}

const couldNotFindMessage = `Hi, we could not find a reference to the types you are talking about in this discussion.
Please edit the title to include the name on npm inside square brackets.
E.g.
- \`"[@typescript/vfs] Does not x, y"\`
- \`"Missing x inside [node]"\`
- \`"[express] Broken support for template types"\`
const couldNotFindMessage = txt`
|Hi, we could not find a reference to the types you are talking about in this discussion.
|Please edit the title to include the name on npm inside square brackets.
|
|E.g.
|- \`"[@typescript/vfs] Does not x, y"\`
|- \`"Missing x inside [node]"\`
|- \`"[express] Broken support for template types"\`
`;

const gotAReferenceMessage = (module: string, owners: string[]) => `Thanks for the discussion about "${module}", some useful links for everyone: [npm](https://www.npmjs.com/package/${module}) / etc
Pinging the DT module owners: ${owners.join(", ")}.
const gotAReferenceMessage = (module: string, owners: string[]) => txt`
|Thanks for the discussion about "${module}", some useful links for everyone:
[npm](https://www.npmjs.com/package/${module}) / etc
|
|Pinging the DT module owners: ${owners.join(", ")}.
`;


Expand Down Expand Up @@ -108,22 +112,20 @@ async function addLabel(discussion: Discussion, labelName: string, description?:
}
}


async function getLabelByName(name: string) {
const info = await client.query({
query: gql`
query GetLabel($name: String!) {
repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") {
name
labels(query: $name, first: 1) {
nodes {
id
query GetLabel($name: String!) {
repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") {
name
labels(query: $name, first: 1) {
nodes {
id
name
}
}
}
}
}
}
`,
}`,
variables: { name },
fetchPolicy: "no-cache",
});
Expand All @@ -134,23 +136,22 @@ async function getLabelByName(name: string) {
async function getCommentsForDiscussionNumber(number: number) {
const info = await client.query({
query: gql`
query GetDiscussionComments($discussionNumber: Int!) {
repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") {
name
discussion(number: $discussionNumber) {
comments(first: 100) {
nodes {
author {
login
}
id
body
}
}
}
}
}
`,
query GetDiscussionComments($discussionNumber: Int!) {
repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") {
name
discussion(number: $discussionNumber) {
comments(first: 100) {
nodes {
author {
login
}
id
body
}
}
}
}
}`,
variables: { discussionNumber: number },
fetchPolicy: "no-cache",
});
Expand Down
4 changes: 2 additions & 2 deletions src/util/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export async function shouldRunRequest(req: HttpRequest, canHandleRequest?: (eve
const { headers, body } = req;
const event = headers["x-github-event"]!;
const action = body.action;
if (!isDev && await verifyIsFromGitHub(req)) return false;
if (!isDev && await !verifyIsFromGitHub(req)) return false;
// Optional function for early bailing if it returns false
if (canHandleRequest && !canHandleRequest(event, action)) return false;
return true;
}

export async function verifyIsFromGitHub(req: HttpRequest) {
async function verifyIsFromGitHub(req: HttpRequest) {
// For process.env.GITHUB_WEBHOOK_SECRET see
// https://ms.portal.azure.com/#blade/WebsitesExtension/FunctionsIFrameBlade/id/%2Fsubscriptions%2F57bfeeed-c34a-4ffd-a06b-ccff27ac91b8%2FresourceGroups%2Fdtmergebot%2Fproviders%2FMicrosoft.Web%2Fsites%2FDTMergeBot
const secret = process.env.GITHUB_WEBHOOK_SECRET;
Expand Down

0 comments on commit 17fdbe2

Please sign in to comment.