diff --git a/.github/workflows/archiveTooting.yml b/.github/workflows/archiveTooting.yml new file mode 100644 index 0000000..cbde06b --- /dev/null +++ b/.github/workflows/archiveTooting.yml @@ -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<> $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 }} diff --git a/parseDiffForMastodon.py b/parseDiffForMastodon.py new file mode 100644 index 0000000..c6c2ca6 --- /dev/null +++ b/parseDiffForMastodon.py @@ -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") \ No newline at end of file