Skip to content

Commit

Permalink
add stuff that i stole from geode indexer hahahaha
Browse files Browse the repository at this point in the history
  • Loading branch information
The Click Sounds Bot authored Mar 23, 2024
1 parent 1e4c0d2 commit e548b65
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .github/issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import json
import hashlib
import os
import sys
import zipfile
import urllib.request
import re
from pathlib import Path
import subprocess

def fail(msg):
print(f'Fail: {msg}', file=sys.stderr)
sys.exit(1)


index_path = Path(sys.argv[1])
issue_author = sys.argv[2]
if len(sys.argv) == 3:
issue_body = os.environ['ISSUE_BODY']
else:
# not passing issue body as a system argument for injection reasons
# but its still here for testing
issue_body = sys.argv[3]

is_old = sys.argv[4] == 'old' if len(sys.argv) > 4 else False

if 'Click Sound Name' not in issue_body and not is_old:
print('Not a valid entry', file=sys.stderr)
sys.exit(2)



def send_webhook(mod_id):
from urllib import request
import json
import os

COLOR = 0x8d73ce

issue_author = os.getenv('ISSUE_AUTHOR', '?')
comment_author = os.getenv('COMMENT_AUTHOR', '?')

description = f'''https://geode-sdk.org/mods/{mod_id}
Uploaded by: [{issue_author}](https://github.com/{issue_author})
Accepted by: [{comment_author}](https://github.com/{comment_author})'''

title = f'Added `{mod_id}`
description = 'New mod!\n' + description


embeds = [
{
'color': COLOR,
'title': title,
'description': description,
'thumbnail': {
'url': f'https://raw.githubusercontent.com/geode-sdk/mods/main/mods-v2/{mod_id}/logo.png'
}
}
]

req = request.Request(os.getenv('DISCORD_WEBHOOK_URL'), method='POST')
req.add_header('User-Agent', 'python urllib')
req.add_header('Content-Type', 'application/json')
data = {
'content': None,
'embeds': embeds,
}
request.urlopen(req, data=json.dumps(data).encode('utf-8'))



potential_issues = []
if potential_issues:
print('## Potential issues')
print('\n'.join(f'* {x}' for x in potential_issues))

if os.getenv('GITHUB_OUTPUT'):
with open(os.getenv('GITHUB_OUTPUT'), 'a') as file:
file.write('has_issues=YES\n')


# mod only gets auto accepted when there are no issues
try:
# ignore potential issues if this is triggered by a staff !accept command
if (os.getenv('ACTUALLY_ACCEPTING') == 'YES' or not potential_issues) and os.getenv('VERIFY_USER_RESULT') == 'YES':
send_webhook(mod_id)
else:
with open('silly_log.txt', 'a') as file:
file.write("not sending webhook :P\n")
except Exception as e:
# dont care about webhook failing
with open('silly_log.txt', 'a') as file:
file.write(str(e) + "\n")

if os.getenv('GITHUB_OUTPUT'):
with open(os.getenv('GITHUB_OUTPUT'), 'a') as file:
file.write(f'mod_id={mod_id}\n')
9 changes: 9 additions & 0 deletions .github/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
import json
from pathlib import Path

index_path = Path(sys.argv[1])
issue_author = sys.argv[2]

config = json.load(open(index_path / "indexer-config.json", "r"))
print("YES" if issue_author in config["verified"] else "NO")
9 changes: 9 additions & 0 deletions .github/verify2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
import json
from pathlib import Path

index_path = Path(sys.argv[1])
issue_author = sys.argv[2]

config = json.load(open(index_path / "indexer-config.json", "r"))
print("YES" if issue_author in config["approve"] else "NO")
114 changes: 114 additions & 0 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: Add entry new

on:
issues:
types: [opened, edited]

# Stolen from geodes indexer lol
jobs:
issue_created:
name: Issue test
if: (!github.event.issue.pull_request && github.event.issue.state == 'open')
runs-on: ubuntu-latest
steps:
- name: Checkout Indexer
uses: actions/checkout@v4
with:
path: code

- name: Checkout Index
uses: actions/checkout@v4
with:
repository: geode-sdk/mods
path: index
token: '${{ secrets.GEODE_BOT_PUSH_BIN_TOKEN }}'

- name: Verify User
id: verify_user
shell: bash
run: |
echo result=`python3 code/.github/verify.py index $ISSUE_AUTHOR` >> $GITHUB_OUTPUT
env:
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}

- name: Add Entry
id: add_entry
shell: bash
run: |
echo has_issues=NO >> $GITHUB_OUTPUT
set +e
python3 code/.github/issue.py index $ISSUE_AUTHOR > result.txt 2> err.txt
RESULT=$?
set -e
echo "output is"
cat result.txt
echo "any silly errors?"
if [ -f silly_log.txt ]; then
cat silly_log.txt
fi
EOF=$(openssl rand -hex 8)
{
echo "issue_output<<$EOF"
cat result.txt
echo "$EOF"
} >> "$GITHUB_OUTPUT"
{
echo "issue_err<<$EOF"
cat err.txt
echo "$EOF"
} >> "$GITHUB_OUTPUT"
if [ $RESULT -eq 0 ]; then
echo push_entry=YES >> $GITHUB_OUTPUT
else
echo push_entry=NO >> $GITHUB_OUTPUT
fi
env:
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
COMMENT_AUTHOR: ${{ github.event.issue.user.login }}
ISSUE_BODY: ${{ github.event.issue.body }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
VERIFY_USER_RESULT: ${{ steps.verify_user.outputs.result }}

- name: Push to index
shell: bash
if: steps.add_entry.outputs.push_entry == 'YES' && steps.verify_user.outputs.result == 'YES' && steps.add_entry.outputs.has_issues == 'NO'
working-directory: ${{ github.workspace }}/index
run: |
git config --local user.email "${{ secrets.GEODE_BOT_EMAIL }}"
git config --local user.name "GeodeBot"
git add -A
git commit -m "Update from $ISSUE_AUTHOR - ${{ steps.add_entry.outputs.mod_id }}"
git remote set-url origin "https://GeodeBot:${{ secrets.GEODE_BOT_PUSH_BIN_TOKEN }}@github.com/geode-sdk/mods.git"
git push -u origin main
env:
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}

- name: Close because invalid
if: steps.add_entry.outputs.issue_err != ''
run: gh issue close ${{ github.event.issue.number }} --comment "$BODY" -R "${{ github.repository }}" --reason "not planned"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BODY: "```\n${{ steps.add_entry.outputs.issue_err }}\n```"

- name: Close because verified and valid
if: steps.verify_user.outputs.result == 'YES' && steps.add_entry.outputs.push_entry == 'YES' && steps.add_entry.outputs.has_issues == 'NO'
run: gh issue close ${{ github.event.issue.number }} --comment "$BODY" -R "${{ github.repository }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BODY: ${{ steps.add_entry.outputs.issue_output }}

- name: Comment waiting for staff
uses: peter-evans/create-or-update-comment@v4
if: (steps.verify_user.outputs.result == 'NO' || steps.add_entry.outputs.has_issues == 'YES') && steps.add_entry.outputs.push_entry == 'YES'
with:
issue-number: ${{ github.event.issue.number }}
body: |
Waiting for a person who can approve to comment "!accept" to accept the entry.
87 changes: 87 additions & 0 deletions .github/workflows/issuecomment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Review Entry

on:
issue_comment:
types: [created]

env:
PR_AUTHOR: ${{ github.event.issue.user.login }}
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
COMMENT_AUTHOR: ${{ github.event.comment.user.login }}

jobs:
check_issues:
runs-on: ubuntu-latest
outputs:
dirs: ${{ steps.dirs.outputs.dirs }}
verified: ${{ steps.verify.outputs.verify }}

if: ${{ !github.event.issue.pull_request }}

steps:
- name: Checkout Indexer
uses: actions/checkout@v3
with:
path: code

- name: Checkout Index
uses: actions/checkout@v3
with:
repository: geode-sdk/mods
path: index
token: '${{ secrets.GEODE_BOT_PUSH_BIN_TOKEN }}'

- name: Verify User
id: verify_user
shell: bash
run: |
echo result=`python3 code/.github/verify2.py index $COMMENT_AUTHOR` >> $GITHUB_OUTPUT
- name: Add Entry
if: steps.verify_user.outputs.result == 'YES' && github.event.comment.body == '!accept'
id: add_entry
shell: bash
run: |
set +e
python3 code/.github/issue.py index $ISSUE_AUTHOR >> result.txt
RESULT=$?
set -e
cat result.txt
echo "any silly errors?"
if [ -f silly_log.txt ]; then
cat silly_log.txt
fi
if [ $RESULT -eq 0 ]; then
echo push_entry=YES >> $GITHUB_OUTPUT
else
echo push_entry=NO >> $GITHUB_OUTPUT
fi
env:
ISSUE_BODY: ${{ github.event.issue.body }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
VERIFY_USER_RESULT: ${{ steps.verify_user.outputs.result }}
ACTUALLY_ACCEPTING: YES

- name: Push to index
shell: bash
if: steps.verify_user.outputs.result == 'YES' && steps.add_entry.outputs.push_entry == 'YES' && github.event.comment.body == '!accept'
working-directory: ${{ github.workspace }}/index
run: |
git config --local user.email "${{ secrets.GEODE_BOT_EMAIL }}"
git config --local user.name "GeodeBot"
git add -A
git commit -m "Update from $ISSUE_AUTHOR - ${{ steps.add_entry.outputs.mod_id }}" -m "Accepted by: $COMMENT_AUTHOR"
git remote set-url origin "https://GeodeBot:${{ secrets.GEODE_BOT_PUSH_BIN_TOKEN }}@github.com/geode-sdk/mods.git"
git push -u origin main
- name: Close Issue
uses: peter-evans/close-issue@v3
if: steps.verify_user.outputs.result == 'YES' && steps.add_entry.outputs.push_entry == 'YES' && github.event.comment.body == '!accept'
with:
issue-number: ${{ github.event.issue.number }}
comment: Successfully added the mod!

0 comments on commit e548b65

Please sign in to comment.