diff --git a/web-app/package-lock.json b/web-app/package-lock.json index 7cfe3b5..271a7aa 100644 --- a/web-app/package-lock.json +++ b/web-app/package-lock.json @@ -1410,6 +1410,12 @@ "@types/node": "*" } }, + "@types/crypto-js": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.1.1.tgz", + "integrity": "sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==", + "dev": true + }, "@types/express": { "version": "4.17.13", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.13.tgz", @@ -2629,6 +2635,11 @@ "which": "^2.0.1" } }, + "crypto-js": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz", + "integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==" + }, "crypto-random-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", diff --git a/web-app/package.json b/web-app/package.json index 53ad302..73c53e4 100644 --- a/web-app/package.json +++ b/web-app/package.json @@ -25,11 +25,13 @@ "@octokit/plugin-paginate-rest": "^2.17.0", "@octokit/plugin-rest-endpoint-methods": "^5.13.0", "@probot/octokit-plugin-config": "^1.1.2", + "crypto-js": "^4.1.1", "probot": "^11.0.1", "query-string": "^7.0.1", "yaml": "^1.10.2" }, "devDependencies": { + "@types/crypto-js": "^4.1.1", "@types/jest": "^26.0.19", "@types/node": "^14.14.19", "@typescript-eslint/eslint-plugin": "^5.5.0", diff --git a/web-app/src/eventHandlers/pullRequestEventHandler.ts b/web-app/src/eventHandlers/pullRequestEventHandler.ts index f92e9d9..8bcacb9 100644 --- a/web-app/src/eventHandlers/pullRequestEventHandler.ts +++ b/web-app/src/eventHandlers/pullRequestEventHandler.ts @@ -50,20 +50,23 @@ export class PullRequestEventHandler { const appGitHubService = GitHubService.buildForApp(context.octokit as unknown as OctokitPlus, logger, appConfig); const payload = context.payload; + + const pullRepo = payload.repository; + const pullRequest = payload.pull_request; const pullInfo: PullInfo = { - owner: payload.repository.owner.login, - repo: payload.repository.name, - repoName: payload.repository.name, - pull_number: payload.pull_request.number, + owner: pullRepo.owner.login, + repo: pullRepo.name, + repoName: pullRepo.name, + pull_number: pullRequest.number, }; - const isDefaultBranch = payload.pull_request.base.ref === payload.repository.default_branch; + const isDefaultBranch = pullRequest.base.ref === pullRepo.default_branch; if (!isDefaultBranch) { logger.info("The PR is not targeting the default branch, will not post anything"); return; } - if (payload.pull_request.draft) { + if (pullRequest.draft) { logger.info("This is a draft PR. Exiting."); return; } @@ -100,7 +103,7 @@ export class PullRequestEventHandler { const shouldCreateDiscussionForFile = this.shouldCreateDiscussionForFile(appConfig.appSettings, filepath); if (shouldCreateDiscussionForFile) { try { - const fileref = payload.pull_request.head.ref; + const fileref = pullRequest.head.ref; // Parse the markdown to get the discussion metadata and details const parsedMarkdown = await this.getParsedMarkdownDiscussion(appGitHubService, logger, { filepath: filepath, @@ -108,7 +111,7 @@ export class PullRequestEventHandler { fileref: fileref, }); - const authorLogin = payload.pull_request.user.login; + const authorLogin = pullRequest.user.login; if (!parsedMarkdown.repo && !parsedMarkdown.team) { throw new Error("Markdown is missing a repo or team to post the discussion to"); @@ -127,16 +130,16 @@ export class PullRequestEventHandler { await this._tokenService.deleteRefreshToken(authorLogin); } if (!userRefreshToken || isNonProd) { - const fullAuthUrl = `${appConfig.base_url}${appConfig.auth_url}`; - commentBody += `- @${authorLogin} must [authenticate](${fullAuthUrl}) before merging this PR\n`; + const fullAuthUrl = `${appConfig.base_url}${appConfig.auth_url}?pull_url=${pullRequest.html_url}`; + commentBody += `- @${authorLogin}: you must [authorize the app](${fullAuthUrl}) before merging this pull request so the discussion can be created as you. This is not required every time.\n`; } commentBody += - "- Do not use relative links to files in your repo. Instead, use full URLs and for media drag/drop or paste the file into the markdown. The link generated for media should contain `https://user-images.githubusercontent.com`\n"; + "- Do not use relative links to files in your repo. Instead, use full URLs and for media drag/drop or paste the file into the markdown. The link generated for media should contain `https://user-images.githubusercontent.com`.\n"; if (!mostRecentBotCommentForFile) { await appGitHubService.createPullRequestComment({ ...pullInfo, - commit_id: payload.pull_request.head.sha, + commit_id: pullRequest.head.sha, start_line: 1, end_line: parsedMarkdown.headerEndLine, body: commentBody, @@ -177,7 +180,7 @@ export class PullRequestEventHandler { } else { await appGitHubService.createPullRequestComment({ ...pullInfo, - commit_id: payload.pull_request.head.sha, + commit_id: pullRequest.head.sha, start_line: 1, end_line: 1, body: errorMessage, @@ -198,14 +201,16 @@ export class PullRequestEventHandler { const appGitHubService = GitHubService.buildForApp(context.octokit as unknown as OctokitPlus, logger, appConfig); const payload = context.payload; + const pullRepo = payload.repository; + const pullRequest = payload.pull_request; const pullInfo: PullInfo = { - owner: payload.repository.owner.login, - repo: payload.repository.name, - repoName: payload.repository.name, - pull_number: payload.pull_request.number, + owner: pullRepo.owner.login, + repo: pullRepo.name, + repoName: pullRepo.name, + pull_number: pullRequest.number, }; - const isDefaultBranch = payload.pull_request.base.ref === payload.repository.default_branch; + const isDefaultBranch = pullRequest.base.ref === pullRepo.default_branch; if (!isDefaultBranch) { logger.info("The PR is not targeting the default branch, will not post anything"); @@ -215,7 +220,7 @@ export class PullRequestEventHandler { // Get pull request comments const app = await appGitHubService.getAuthenticatedApp(); const appLogin = `${app.slug}[bot]`; - const optionalPullRequestFooterMarkdown = payload.repository.private ? "" : `from a pull request `; + const optionalPullRequestFooterMarkdown = pullRepo.private ? "" : `from a pull request `; const postFooter = `\n\n
Sending you back to the ${redirectLocationText} in just a few seconds...