Skip to content

Commit

Permalink
feat(gcf-utils): change the method signature of addOrUpdateIssueComme…
Browse files Browse the repository at this point in the history
…nt (#1131)

fixes #1130 

Now it only requires Octokit, instead of Context.
The intention is to allow us to use this function from the cron hander.
  • Loading branch information
Takashi Matsuo committed Nov 17, 2020
1 parent 854ef0e commit 700927f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions packages/gcf-utils/src/gcf-utils.ts
Expand Up @@ -15,10 +15,10 @@
import {
createProbot,
Probot,
ProbotOctokit,
ApplicationFunction,
Options,
Application,
Context,
} from 'probot';

import getStream from 'get-stream';
Expand All @@ -38,6 +38,8 @@ import {v4} from 'uuid';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const LoggingOctokitPlugin = require('../src/logging/logging-octokit-plugin.js');

type ProbotOctokitType = InstanceType<typeof ProbotOctokit>;

interface Repos {
repos: [
{
Expand Down Expand Up @@ -96,15 +98,15 @@ export enum TriggerType {
}

export const addOrUpdateIssueComment = async (
context: Context,
github: ProbotOctokitType,
owner: string,
repo: string,
issueNumber: number,
installationId: number,
commentBody: string
) => {
const installationId = context.payload.installation.id;
const commentMark = `<!-- probot comment [${installationId}]-->`;
const listCommentsResponse = await context.github.issues.listComments({
const listCommentsResponse = await github.issues.listComments({
owner: owner,
repo: repo,
per_page: 50, // I think 50 is enough, but I may be wrong.
Expand All @@ -114,7 +116,7 @@ export const addOrUpdateIssueComment = async (
for (const comment of listCommentsResponse.data) {
if (comment.body.includes(commentMark)) {
// We found the existing comment, so updating it
await context.github.issues.updateComment({
await github.issues.updateComment({
owner: owner,
repo: repo,
comment_id: comment.id,
Expand All @@ -124,7 +126,7 @@ export const addOrUpdateIssueComment = async (
}
}
if (!found) {
await context.github.issues.createComment({
await github.issues.createComment({
owner: owner,
repo: repo,
issue_number: issueNumber,
Expand Down
3 changes: 2 additions & 1 deletion packages/gcf-utils/test/gcf-utils.ts
Expand Up @@ -30,10 +30,11 @@ const fixturesPath = resolve(__dirname, '../../test/fixtures');
const app = (app: Application) => {
app.on('issues.opened', async context => {
await addOrUpdateIssueComment(
context,
context.github,
context.payload.repository.owner.login,
context.payload.repository.name,
context.payload.issue.number,
context.payload.installation.id,
'test comment'
);
});
Expand Down

0 comments on commit 700927f

Please sign in to comment.