Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to create comment with results #81

Open
zmiklank opened this issue Feb 16, 2023 · 1 comment
Open

Add possibility to create comment with results #81

zmiklank opened this issue Feb 16, 2023 · 1 comment
Labels
type: feature New feature or request

Comments

@zmiklank
Copy link
Member

zmiklank commented Feb 16, 2023

We can use, e.g., https://github.com/marocchino/sticky-pull-request-comment

Only nice to have.

@jamacku
Copy link
Collaborator

jamacku commented Feb 16, 2023

Here is an example of implementation I use in our GA. It's based on probot/metadata javascript library, which works only for GitHub Apps (Bots).

Example of implementation from redhat-plumbers-in-action/devel-freezer:

Store comment ID in a PR comment

source: https://github.com/redhat-plumbers-in-action/devel-freezer/blob/main/src/metadata.ts#L84-L142

/**
 * Based on probot-metadata - https://github.com/probot/metadata
 */
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class MetadataController {
  static readonly regex = /\n\n<!-- devel-freezer = (.*) -->/;

  static async getMetadata(issueNumber: number, key: string, context: Context) {
    const body =
      (
        await context.octokit.issues.get(
          context.issue({ issue_number: issueNumber })
        )
      ).data.body || '';

    const match = body.match(MetadataController.regex);

    if (match) {
      const data = JSON.parse(match[1]);
      return key ? data && data[key] : data;
    }
  }

  static async setMetadata(
    key: string,
    value: string,
    context: Context,
    issueNumber?: number
  ) {
    let body =
      (
        await context.octokit.issues.get(
          context.issue(issueNumber ? { issue_number: issueNumber } : {})
        )
      ).data.body || '';

    let data = {};

    body = body.replace(MetadataController.regex, (_, json) => {
      data = JSON.parse(json);
      return '';
    });

    if (!data) data = {};

    if (typeof key === 'object') {
      Object.assign(data, key);
    } else {
      (data as { [key: string]: string })[key] = value;
    }

    return context.octokit.issues.update(
      context.issue({
        body: `${body}\n\n<!-- devel-freezer = ${JSON.stringify(data)} -->`,
        ...(issueNumber ? { issue_number: issueNumber } : {}),
      })
    );
  }
}

Use Metadata to create/update comment

source: https://github.com/redhat-plumbers-in-action/devel-freezer/blob/main/src/pull-request.ts#L57-L110

  private async publishComment(
    content: string,
    context: {
      [K in keyof typeof events]: Context<(typeof events)[K][number]>;
    }[keyof typeof events]
  ) {
    if (this.metadata.commentID) {
      this.updateComment(content, context);
      return;
    }

    const commentPayload = (await this.createComment(content, context))?.data;

    if (!commentPayload) {
      warning(`Failed to create comment.`);
      return;
    }

    return commentPayload.id;
  }

  private createComment(
    body: string,
    context: {
      [K in keyof typeof events]: Context<(typeof events)[K][number]>;
    }[keyof typeof events]
  ) {
    if (!body || body === '') return;

    return context.octokit.issues.createComment(
      // !FIXME: This is wrong, don't use `as`
      (context as Context<(typeof events.pull_request)[number]>).issue({
        issue_number: this.id,
        body,
      })
    );
  }

  private async updateComment(
    body: string,
    context: {
      [K in keyof typeof events]: Context<(typeof events)[K][number]>;
    }[keyof typeof events]
  ) {
    if (!this.metadata.commentID) return;

    return context.octokit.issues.updateComment(
      // !FIXME: This is wrong, don't use `as`
      (context as Context<(typeof events.pull_request)[number]>).issue({
        comment_id: +this.metadata.commentID,
        body,
      })
    );
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants