-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from poseidon-framework/mastodonTooting
Add mastodon tooting GA from community-archive
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
name: Archive Tooting | ||
|
||
on: | ||
pull_request: | ||
types: [closed] | ||
workflow_dispatch: # manual triggering | ||
|
||
jobs: | ||
toot_update: | ||
if: | | ||
github.event.pull_request.merged == true && | ||
github.event.pull_request.title == '[Automatic PR] Update chronicle file' | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
pip install requests | ||
- name: Construct diff URL | ||
id: construct_diff_url | ||
run: | | ||
echo ${{ github.event.pull_request.diff_url }} > diff_url.txt | ||
- name: Fetch PR diff | ||
run: | | ||
curl -L $(cat diff_url.txt) -o diff.txt | ||
- name: Run parseDiffForMastodon.py | ||
run: | | ||
python3 parseDiffForMastodon.py < diff.txt > message.txt | ||
- name: Print message | ||
run: cat message.txt | ||
|
||
- name: Append message to a variable | ||
run: | | ||
echo 'MESSAGE_ENV<<EOF' >> $GITHUB_ENV | ||
cat message.txt >> $GITHUB_ENV | ||
echo 'EOF' >> $GITHUB_ENV | ||
- name: Toot! | ||
uses: rzr/fediverse-action@master | ||
with: | ||
access-token: ${{ secrets.MASTODON_ACCESS_TOKEN }} | ||
host: "ecoevo.social" # custom host if not "mastodon.social" (default) | ||
message: ${{ env.MESSAGE_ENV }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import re | ||
import sys | ||
|
||
"""Regex : ^ from the start of the line. | ||
\+: matches + sign. \s*: matches whitespace between + and other text. - matches hypen. title: matches title. | ||
(.*?): Matches package name. "\n:" matches newline. \+ matches plus sign. again (.*?) to capture version """ | ||
|
||
|
||
def parse_diff(diff): | ||
new_packages = [] | ||
# using raw string (r'...') instead of \+ | ||
package_pattern = re.compile(r'^\+\s*-\s*title:\s*(.*?)\n\+\s*version:\s*(.*?)$', re.MULTILINE) | ||
|
||
matches = package_pattern.findall(diff) | ||
for match in matches: | ||
title, version = match | ||
new_packages.append(f"{title} (v{version})") | ||
|
||
return new_packages | ||
|
||
|
||
def create_toot_content(new_packages, max_length=480): | ||
base_content = "New packages in the minotaur-archive:\n" | ||
url_and_tags = "\nhttps://github.com/poseidon-framework/minotaur-archive #aDNA #OpenData" | ||
available_length = max_length - len(base_content) - len(url_and_tags) #tracking the remaining character limit. | ||
|
||
toot_content = base_content #initial cha | ||
for package in new_packages: | ||
if len(toot_content) + len(package) + 2 > available_length: # +2 for ", " | ||
toot_content += "..." | ||
break | ||
if toot_content != base_content: | ||
toot_content += "\n" | ||
toot_content += package | ||
|
||
toot_content += url_and_tags | ||
return toot_content | ||
|
||
if __name__ == "__main__": | ||
diff = sys.stdin.read() | ||
new_packages = parse_diff(diff) | ||
|
||
if new_packages: | ||
toot_content = create_toot_content(new_packages) | ||
# console | ||
print(toot_content) | ||
else: | ||
|
||
print("No new package information has been found in this PR") |