Skip to content

Commit 1831c33

Browse files
committed
test github stats
1 parent 78f9dc2 commit 1831c33

2 files changed

Lines changed: 91 additions & 23 deletions

File tree

.github/workflows/main.yml

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1-
21
on:
3-
schedule:
4-
# Run this once per day, towards the end of the day for keeping the most
5-
# recent data point most meaningful (hours are interpreted in UTC).
6-
- cron: "0 23 * * *"
7-
workflow_dispatch: # Allow for running this manually.
8-
2+
schedule:
3+
- cron: "0 0 */14 * *"
4+
95
jobs:
10-
j1:
11-
name: repostats-for-nice-project
6+
# This workflow contains a single job called "traffic"
7+
traffic:
8+
# The type of runner that the job will run on
129
runs-on: ubuntu-latest
10+
permissions:
11+
# Give the default GITHUB_TOKEN write permission to commit and push the
12+
# added or changed files to the repository.
13+
contents: write
14+
1315
steps:
14-
- name: run-ghrs
15-
uses: jgehrcke/github-repo-stats@RELEASE
16-
with:
17-
# Define the stats repository (the repo to fetch
18-
# stats for and to generate the report for).
19-
# Remove the parameter when the stats repository
20-
# and the data repository are the same.
21-
repository: CellProfiling/subcell-embed/
22-
# Set a GitHub API token that can read the GitHub
23-
# repository traffic API for the stats repository,
24-
# and that can push commits to the data repository
25-
# (which this workflow file lives in, to store data
26-
# and the report files).
27-
ghtoken: ${{ secrets.ghrs_github_api_token }}
16+
- name: Checkout current repo
17+
uses: actions/checkout@v4.1.1
18+
19+
- name: Setup Python
20+
uses: actions/setup-python@v5.0.0
21+
with:
22+
python-version: '3.10'
23+
24+
- name: install depedencies
25+
run: pip install requests
26+
27+
- name: Run Python script to generate summary
28+
run: python3 github_stats.py
29+
env:
30+
TRAFFIC_ACTION_TOKEN: ${{ secrets.GHRS_GITHUB_API_TOKEN }}
31+
TRAFFIC_ACTION_OWNER: ${{ secrets.TRAFFIC_ACTION_OWNER }}
32+
TRAFFIC_ACTION_REPO: ${{ secrets.TRAFFIC_ACTION_REPO }}
33+
34+
- name: view summary file
35+
run: cat traffic/summary.csv
36+
37+
- name: list generated files
38+
run: ls -ltrh
39+
40+
- name: Commit and push changes
41+
uses: stefanzweifel/git-auto-commit-action@v5
42+
with:
43+
commit_message: "Update CSV with new data from github action"

github_stats.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import requests
3+
from datetime import datetime
4+
5+
### setup env variables
6+
GITHUB_PAT = os.getenv("GHRS_GITHUB_API_TOKEN")
7+
GITHUB_OWNER = os.getenv("TRAFFIC_ACTION_OWNER")
8+
GITHUB_REPO = os.getenv("TRAFFIC_ACTION_REPO")
9+
10+
11+
output_dir="traffic/"
12+
os.makedirs(output_dir, exist_ok=True)
13+
current_date = datetime.today().strftime('%Y-%m-%d')
14+
15+
# Define API endpoint URLs
16+
endpoints = [
17+
"/repos/{}/{}/traffic/clones".format(GITHUB_OWNER, GITHUB_REPO),
18+
"/repos/{}/{}/traffic/popular/paths".format(GITHUB_OWNER, GITHUB_REPO),
19+
"/repos/{}/{}/traffic/popular/referrers".format(GITHUB_OWNER, GITHUB_REPO),
20+
"/repos/{}/{}/traffic/views".format(GITHUB_OWNER, GITHUB_REPO)
21+
]
22+
23+
# Function to fetch data from GitHub API
24+
def fetch_data(endpoint):
25+
url = "https://api.github.com" + endpoint
26+
headers = {
27+
"Accept": "application/vnd.github.v3+json",
28+
"Authorization": f"token {GITHUB_PAT}"
29+
}
30+
response = requests.get(url, headers=headers)
31+
return response.json()
32+
33+
34+
# Loop through the endpoints and fetch traffic data
35+
for endpoint in endpoints:
36+
data = fetch_data(endpoint)
37+
print(data)
38+
with open(os.path.join(output_dir, current_date, endpoint.split('/')[-1] + ".json"), "w") as f:
39+
json.dump(data, f)
40+
41+
42+
# Write summary to a CSV file
43+
with open(os.path.join(output_dir, "summary.csv"), "a") as summary_file:
44+
summary_file.write(
45+
"{},{},{},{},{}\n".format(
46+
current_date,
47+
json.loads(open(os.path.join(output_dir, current_date, "clones.json")).read())["count"],
48+
json.loads(open(os.path.join(output_dir, current_date, "clones.json")).read())["uniques"],
49+
json.loads(open(os.path.join(output_dir, current_date, "views.json")).read())["count"],
50+
json.loads(open(os.path.join(output_dir, current_date, "views.json")).read())["uniques"]
51+
)
52+
)

0 commit comments

Comments
 (0)