在您的公关中立即获取魔术八个球答案或发表评论!
#node #chatgpt #jokes #githubhack23

我建造的

Magic Eight Ball是一种奇妙的设备,可让用户卸载任何重要的决定对塑料球。好吧,这是您甚至不需要摇晃的!我已经建立了一个github动作,可以让您临时命运,并在评论中对公关或问题的评论中最重要的问题收到答案!

类别提交:

古怪的通配符

应用链接

Try in action

屏幕截图

Signs point to a nap first.

My reply is no, but my heart says yes... to pizza.

描述

操作在每个注释上运行,如果它以🎱(或其他定义的前缀)开头 - 它将以随机答案回复。

链接到源代码

GitHub logo ValeriaVG / actions-magic-8-ball

获得魔术球的答案直接传递给您的评论

GitHub Actions: Magic Eight Ball comments

Get Magic ball answers delivered directly to your comments.

How to use

Create an action in koude1:

on:
  issue_comment:
    types: [created]
jobs:
  example_comment_pr:
    runs-on: ubuntu-latest
    name: Magic Eight Ball Comments
    permissions:
        pull-requests: write
        issues: write
    steps:
      - name: Answer
        uses: ValeriaVG/actions-magic-8-ball@v1

Post a comment in PR or and issue starting with "🎱" and get a prediction:

Example response: "🎱 Should I add tests to this repo? - Signs point to a nap first."

使用自定义前缀

您还可以将前缀更改为自定义:

学分

已使用Free Research Preview ChatGPT

生成

Can you generate 10 chaotic funny eight ball responses (in json array, of course)?




允许许可证

MIT

背景

我想构建一些神奇,有趣,混乱和顶级的东西!我问一个魔术八球,我的财富饼干想法是否很好,它说魔术八球是更好的:)

我如何建造它

我从实际的魔术八球脚本开始。当然,最重要的是写答案。我了解到,一个经典的魔术八球有20个答案,但是它们很无聊...

我想要无聊的东西!因此,我去了ChatGPT,并礼貌地要求它产生一些有趣的回答:

Generate 10 chaotic funny eight ball responses

当然,我也要求使用算法,但这太朴素了:

const responses = [
  "It is certain",
  "Without a doubt",
  "Yes – definitely",
  "You may rely on it",
  "As I see it, yes",
  "Most likely",
  "Outlook good",
  "Yes",
  "Signs point to yes",
  "Reply hazy, try again",
  "Better not tell you now",
  "Cannot predict now",
  "Concentrate and ask again",
  "Don't count on it",
  "Outlook not so good",
  "My sources say no",
  "Very doubtful"
];

function generateEightBallResponse() {
  const randomIndex = Math.floor(Math.random() * responses.length);
  return responses[randomIndex];
}

// Example usage
console.log(generateEightBallResponse());

我已经要求使用crypto.randomBytes重写它,因为我肯定想要密码范围内的随机答案(谁不会?):

function generateEightBallResponse() {
  const randomBytes = crypto.randomBytes(1);
  const randomIndex = randomBytes[0] % responses.length;
  return responses[randomIndex];
}

这有点好一些,但是一个人永远都没有足够的魔法,所以我安定了以下过度杀伤版本(这次写作比试图向AI解释我所想的要快得多):

function generateEightBallResponse() {
  const randomSize = crypto.randomBytes(1)[0];
  const randomValues = new Uint32Array(randomSize);
  crypto.getRandomValues(randomValues);
  const randomIndex = randomValues[Math.floor(Math.random() * randomSize)] % responses.length;
  return responses[randomIndex];
}

现在这就是我所说的魔术!

下一步是将其转变为实际的github动作。

我已经寻找了一些类似的动作,而且很容易复制。

我已经在我的存储库的根部创建了一个action.yml文件:

name: 'Magic Eight Ball'
branding:
  icon: 'message-circle'
  color: 'gray-dark'
description: 'Replies to a comment with a prediction'
inputs:
  prefix:
    description: 'Prefix that would trigger the response'
    default: '🎱'
    required: false
  GITHUB_TOKEN:
    description: 'Github token of the repository (automatically created by Github)'
    default: ${{ github.token }}
    required: false
runs:
  using: 'node16'
  main: 'lib/index.js'

使用@actions/core@actions/github软件包添加了脚本以发表评论:

import { context, getOctokit } from "@actions/github";
import { getInput } from "@actions/core";
import generateEightBallResponse from "./generateResponse";

const run = async () => {
  const prefix: string = getInput("prefix");
  const github_token: string = getInput("GITHUB_TOKEN");
  if (
    !context.payload.comment ||
    !context.payload.comment["body"]?.startsWith(prefix)
  ) {
    return;
  }
  const issue_number =
    context.payload.pull_request?.number || context.payload.issue?.number;
  if (!issue_number) {
    return;
  }

  const octokit = getOctokit(github_token);
  const body =
    '> ' + context.payload.comment["body"] + '\n\r' +
    "@" +
    context.payload.comment["user"].login +
    " " +
    generateEightBallResponse();

  await octokit.rest.issues.createComment({
    ...context.repo,
    issue_number,
    body,
  });
};

run();

并创建了一个工作流以在同一存储库中测试此操作:

on:
  issue_comment:
    types: [created]
jobs:
  test_8ball:
    name: Magic Eight Ball
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      issues: write
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Run action from main
        uses: ./

它在第一次尝试中无效,我已经了解了权限(最初我缺少contents: read,而我的工作流程未能结帐回购)。

然后,我了解到默认的动作跑步者最多支持Node16,我现在还不能使用crypto.getRandomValues。我已经沉重的心脏安顿下来,以少一些神奇的随机发电机(两次使用crypto.randomBytes):

export default function generateEightBallResponse() {
  const randomSize = crypto.randomBytes(1)[0];
  const randomValues = crypto.randomBytes(randomSize);
  const randomIndex =
    randomValues[Math.floor(Math.random() * randomSize)] % responses.length;
  return responses[randomIndex];
}

我本可以使用不同的动作和设置我需要的任何版本的节点,但是我希望该动作尽可能快且尽可能快。

我已经将所有代码与esbuild捆绑到一个文件中,终于起作用了!

我想编写一些测试,但是魔术八球也告诉我:p