Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Sync GitHub Issues #144

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions .github/scripts/issue_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python

import os
import sys
import datetime
import json

from github import Github, Auth

GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", None)
if not GITHUB_TOKEN:
raise ValueError("GITHUB_TOKEN is not set")

full_name = sys.argv[1]
number = int(sys.argv[2])

auth = Auth.Token(GITHUB_TOKEN)
g = Github(auth=auth)

repo = g.get_repo(full_name)
issue = repo.get_issue(number)

query_date = datetime.datetime.now(datetime.UTC) - datetime.timedelta(hours=1)

unique_comment_authors = {
comment.user.login for comment in issue.get_comments(query_date) if comment.user
}

unique_reaction_authors = {
reaction.user.login
for reaction in issue.get_reactions()
if reaction.user and reaction.created_at > query_date
}

mentions_count = sum(
map(
lambda e: e.event in ["cross-referenced", "referenced"]
and e.created_at > query_date,
issue.get_timeline(),
)
)

data = {
"unique_comments": len(unique_comment_authors),
"unique_reactions": len(unique_reaction_authors),
"mentions": mentions_count,
}

with open("issue_data.json", "w") as f:
json.dump(data, f)
1 change: 1 addition & 0 deletions .github/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyGithub
57 changes: 57 additions & 0 deletions .github/workflows/issue_sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Issue Sync
run-name: "Syncing issue #${{ github.event.issue.number }}"
on:
issues:
types: [opened, edited, closed]
issue_comment:
types: [created]

jobs:
build-and-deploy:
permissions:
id-token: write
contents: read
issues: read
runs-on: ubuntu-latest
environment: prod
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Setup dependencies
run: |
pip install -r .github/scripts/requirements.txt
sudo apt-get install -y httpie
- name: Run python script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python .github/scripts/issue_data.py "${{ github.event.repository.full_name }}" "${{ github.event.issue.number }}"
echo "COMMENTS=$(cat issue_data.json | jq -r '.unique_comments')" >> $GITHUB_ENV
echo "REACTIONS=$(cat issue_data.json | jq -r '.unique_reactions')" >> $GITHUB_ENV
echo "MENTIONS=$(cat issue_data.json | jq -r '.mentions')" >> $GITHUB_ENV
- name: Azure login
uses: azure/#@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Download Certificate
uses: azure/cli@v2
with:
azcliversion: latest
inlineScript: |
az keyvault secret download --id "${{ secrets.CERTIFICATE_SECRET_ID}}" --file client.pem
- name: Call endpoint
run: |
http --ignore-stdin --cert=client.pem --verbose "${{ secrets.ISSUE_PAYLOAD_URL }}" \
issue_title="${{ github.event.issue.title }}" \
issue_url="${{ github.event.issue.html_url }}" \
issue_number:=${{ github.event.issue.number }} \
state="${{ github.event.issue.state }}" \
comments:=$COMMENTS \
reactions:=$REACTIONS \
mentions:=$MENTIONS \
Loading