async onCommentCreated()

in functions/src/issues.ts [343:463]


  async onCommentCreated(
    repo: types.internal.Repository,
    issue: types.internal.Issue,
    comment: types.internal.Comment
  ): Promise<types.Action[]> {
    // Trick for testing
    if (comment.body == "eval") {
      log.debug("HANDLING SPECIAL COMMENT: eval");
      return await this.onNewIssue(repo, issue);
    }

    // Basic info
    const org = repo.owner.login;
    const name = repo.name;
    const number = issue.number;

    const actions: types.Action[] = [];

    // Send an email to subscribers
    const comment_html = marked(comment.body);
    const emailAction = this.emailer.getIssueUpdateEmailAction(repo, issue, {
      header: `New Comment by ${comment.user.login}`,
      body: comment_html
    });

    if (emailAction) {
      actions.push(emailAction);
    }

    // Check for staleness things
    const cleanupConfig = this.config.getRepoCleanupConfig(
      repo.owner.login,
      repo.name
    );

    const isBotComment = comment.user.login === "google-oss-bot";
    const isClosed = issue.state === "closed";

    if (cleanupConfig && cleanupConfig.issue && !isBotComment && !isClosed) {
      const issueConfig = cleanupConfig.issue;
      const labelNames = issue.labels.map(label => label.name);

      const isNeedsInfo = labelNames.includes(issueConfig.label_needs_info);
      const isStale = labelNames.includes(issueConfig.label_stale);

      const isAuthorComment = comment.user.login === issue.user.login;

      if (isStale) {
        // Any comment on a stale issue removes the stale flag
        actions.push(
          new types.GitHubRemoveLabelAction(
            org,
            name,
            number,
            issueConfig.label_stale,
            `Comment by ${comment.user.login} on stale issues remove the stale state.`
          )
        );

        // An author comment on a stale issue moves this to "needs attention",
        // a comment by someone else moves this to needs-info.
        const labelToAdd = isAuthorComment
          ? issueConfig.label_needs_attention
          : issueConfig.label_needs_info;

        const reason = isAuthorComment
          ? `Comment by the author (${issue.user.login}) on a stale issue moves this to needs_attention`
          : `Comment by a non-author (${comment.user.login}) on a stale issue moves this to needs_info`;

        if (isAuthorComment && !issueConfig.label_needs_attention) {
          log.debug(
            "Not adding 'needs-attention' label because it is not specified."
          );
        }

        if (labelToAdd) {
          actions.push(
            new types.GitHubAddLabelAction(
              org,
              name,
              number,
              labelToAdd,
              reason
            )
          );
        }
      }

      if (isNeedsInfo && isAuthorComment) {
        // An author comment on a needs-info issue moves it to needs-attention.
        const reason = `Comment by the author (${issue.user.login}) moves this from needs_info to needs_attention.`;
        actions.push(
          new types.GitHubRemoveLabelAction(
            org,
            name,
            number,
            issueConfig.label_needs_info,
            reason
          )
        );

        if (issueConfig.label_needs_attention) {
          actions.push(
            new types.GitHubAddLabelAction(
              org,
              name,
              number,
              issueConfig.label_needs_attention,
              reason
            )
          );
        } else {
          log.debug(
            "Config does not specifiy 'label_needs_attention' so not adding any label"
          );
        }
      }
    }

    return actions;
  }