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

🎉 #14

Merged
merged 13 commits into from
May 14, 2024
Merged

🎉 #14

Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Create Preview

on:
pull_request_target:
branches: [main]
types: [opened, synchronize, reopened]

jobs:
preview:
name: Preview
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Get changed files in posts folder
id: get_changed_files
uses: tj-actions/changed-files@v44
with:
files: posts/**
json: "true"

- name: get published files cache
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
git fetch origin processed_files:processed_files
git checkout processed_files -- processed_files.json

- name: Set up Python
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: actions/setup-python@v3
with:
python-version: 3.9

- name: Install dependencies
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt

- name: Run script to create preview
if: steps.get_changed_files.outputs.any_changed == 'true'
env:
CHANGED_FILES: ${{ steps.get_changed_files.outputs.all_changed_files }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
run: python -u github_run.py --preview
65 changes: 65 additions & 0 deletions .github/workflows/publish_content.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Publish Content

on:
pull_request_target:
branches: [main]
types: [closed]

jobs:
publish:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Get changed files in posts folder
id: get_changed_files
uses: tj-actions/changed-files@v44
with:
files: posts/**
json: "true"

- name: get published files cache
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
git fetch origin processed_files:processed_files
git checkout processed_files -- processed_files.json

- name: Set up Python
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: actions/setup-python@v3
with:
python-version: 3.9

- name: Install dependencies
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt

- name: Run script to publish contents
if: steps.get_changed_files.outputs.any_changed == 'true'
env:
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
BLUESKY_USERNAME: ${{ secrets.BLUESKY_USERNAME }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }}
MATRIX_ROOM_ID: ${{ secrets.MATRIX_ROOM_ID }}
MATRIX_USER_ID: ${{ secrets.MATRIX_USER_ID }}
SLACK_ACCESS_TOKEN: ${{ secrets.SLACK_ACCESS_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
CHANGED_FILES: ${{ steps.get_changed_files.outputs.all_changed_files }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
run: python -u github_run.py --json-out processed_files.json
arash77 marked this conversation as resolved.
Show resolved Hide resolved

- name: Commit changes
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: "processed_files.json"
branch: "processed_files"
25 changes: 0 additions & 25 deletions .github/workflows/toot-together.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
# lib/
lib64/
parts/
sdist/
Expand Down
32 changes: 32 additions & 0 deletions .schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type: object
properties:
media:
type: array
items:
type: string
uniqueItems: true
images:
type: array
items:
type: object
properties:
url:
type: string
format: uri
alt_text:
type: string
required: []
mentions:
type: object
additionalProperties:
type: array
items:
type: string
required: []
hashtags:
type: object
additionalProperties:
type: array
items:
type: string
required: []
Binary file modified README.md
Binary file not shown.
87 changes: 87 additions & 0 deletions github_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import argparse
import fnmatch
import os

import requests

from lib.galaxy_social import galaxy_social


class github_run:
def __init__(self):
self.github_token = os.getenv("GITHUB_TOKEN")
self.repo = os.getenv("GITHUB_REPOSITORY")
self.pr_number = os.getenv("PR_NUMBER")

def comment(self, comment_text):
print(comment_text)
if not comment_text or not self.github_token or not self.repo or not self.pr_number:
return
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {self.github_token}",
"X-GitHub-Api-Version": "2022-11-28",
}
url = (
f"https://api.github.com/repos/{self.repo}/issues/{self.pr_number}/comments"
)
data = {"body": comment_text}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
return True
else:
raise Exception(
f"Failed to create github comment!, {response.json().get('message')}"
)

def get_files(self):
url = f"https://api.github.com/repos/{self.repo}/pulls/{self.pr_number}/files"
response = requests.get(url)
if response.status_code == 200:
changed_files = response.json()
for file in changed_files:
raw_url = file["raw_url"]
if raw_url.endswith(".md"):
response = requests.get(raw_url)
if response.status_code == 200:
changed_file_path = file["filename"]
with open(changed_file_path, "w") as f:
f.write(response.text)

changed_files = os.environ.get("CHANGED_FILES")
files_to_process = []
if changed_files:
for file_path in eval(changed_files.replace("\\", "")):
if file_path.endswith(".md"):
files_to_process.append(file_path)
else:
for root, _, files in os.walk("posts"):
for filename in fnmatch.filter(files, "*.md"):
file_path = os.path.join(root, filename)
files_to_process.append(file_path)

return files_to_process


if __name__ == "__main__":
github = github_run()
files_to_process = github.get_files()
if not files_to_process:
github.comment("No files to process.")
exit()
arash77 marked this conversation as resolved.
Show resolved Hide resolved

parser = argparse.ArgumentParser(description="Galaxy Social.")
parser.add_argument("--preview", action="store_true", help="Preview the post")
parser.add_argument(
"--json-out",
help="Output json file for processed files",
default="processed_files.json",
)
args = parser.parse_args()

gs = galaxy_social(args.preview, args.json_out)
try:
message = gs.process_files(files_to_process)
except Exception as e:
message = e
github.comment(message)
Loading