Merge pull request #11 from anmol098/dev

Dev
This commit is contained in:
Anmol Pratap Singh
2020-07-27 02:11:54 +05:30
committed by GitHub
3 changed files with 57 additions and 8 deletions

View File

@@ -94,23 +94,27 @@ jobs:
with: with:
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }} WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
GH_TOKEN: ${{ secrets.GH_TOKEN }} GH_TOKEN: ${{ secrets.GH_TOKEN }}
USERNAME: <username> # optional, it will automatically use the username of the owner of the repository who's executing the workflow.
``` ```
## Extras ## Extras
1. If you want to add the other info to your stats, you can add multiple `FLAGS` in your workflow file by default all flags are enabled 1. If you want to add the other info to your stats, you can add multiple `FLAGS` in your workflow file by default all flags are enabled
>except the lines of code flag due to heavy operation performed
```yml ```yml
- uses: anmol098/waka-readme-stats@master - uses: anmol098/waka-readme-stats@master
with: with:
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }} WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
GH_TOKEN: ${{ secrets.GH_TOKEN }} GH_TOKEN: ${{ secrets.GH_TOKEN }}
USERNAME: <username>
SHOW_OS: "False" SHOW_OS: "False"
SHOW_PROJECTS: "False" SHOW_PROJECTS: "False"
``` ```
#### Flags Available #### Flags Available
`SHOW_LINES_OF_CODE` flag can be set to `True` to show the Lines of code writen till date
![Lines of code](https://img.shields.io/badge/From%20Hello%20World%20I've%20written-12,66,814%20Lines%20of%20code-blue)
`SHOW_COMMIT` flag can be set to `False` to hide the commit stats `SHOW_COMMIT` flag can be set to `False` to hide the commit stats
**I'm an early 🐤** **I'm an early 🐤**
@@ -120,6 +124,18 @@ jobs:
🌃 Evening 112 commits █████████░░░░░░░░░░░░░░░░ 36.01% 🌃 Evening 112 commits █████████░░░░░░░░░░░░░░░░ 36.01%
🌙 Night 26 commits ██░░░░░░░░░░░░░░░░░░░░░░░ 8.36% 🌙 Night 26 commits ██░░░░░░░░░░░░░░░░░░░░░░░ 8.36%
```
📅 **I'm Most Productive on Sundays**
```text
Monday 50 commits ███░░░░░░░░░░░░░░░░░░░░░░ 13.19%
Tuesday 85 commits █████░░░░░░░░░░░░░░░░░░░░ 22.43%
Wednesday 56 commits ███░░░░░░░░░░░░░░░░░░░░░░ 14.78%
Thursday 44 commits ███░░░░░░░░░░░░░░░░░░░░░░ 11.61%
Friday 28 commits █░░░░░░░░░░░░░░░░░░░░░░░░ 7.39%
Saturday 30 commits ██░░░░░░░░░░░░░░░░░░░░░░░ 7.92%
Sunday 86 commits █████░░░░░░░░░░░░░░░░░░░░ 22.69%
``` ```
`SHOW_LANGUAGE` flag can be set to `False` to hide the Coding Language You use `SHOW_LANGUAGE` flag can be set to `False` to hide the Coding Language You use

View File

@@ -47,6 +47,11 @@ inputs:
description: "Show the Coding language used in dev metrics" description: "Show the Coding language used in dev metrics"
default: "True" default: "True"
SHOW_LINES_OF_CODE:
required: false
description: "Show the Total Lines of code written Badge till date"
default: "False"

36
main.py
View File

@@ -8,8 +8,9 @@ import base64
import sys import sys
from pytz import timezone from pytz import timezone
import pytz import pytz
import locale
import requests import requests
from github import Github, GithubException from github import Github
import datetime import datetime
from string import Template from string import Template
@@ -26,6 +27,7 @@ showEditors = os.getenv('INPUT_SHOW_EDITORS')
showOs = os.getenv('INPUT_SHOW_OS') showOs = os.getenv('INPUT_SHOW_OS')
showCommit = os.getenv('INPUT_SHOW_COMMIT') showCommit = os.getenv('INPUT_SHOW_COMMIT')
showLanguage = os.getenv('INPUT_SHOW_LANGUAGE') showLanguage = os.getenv('INPUT_SHOW_LANGUAGE')
show_loc = os.getenv('INPUT_SHOW_LINES_OF_CODE')
# The GraphQL query to get commit data. # The GraphQL query to get commit data.
userInfoQuery = """ userInfoQuery = """
@@ -70,6 +72,17 @@ query {
} }
""") """)
get_loc_url = Template("""/repos/$owner/$repo/stats/code_frequency""")
def run_v3_api(query):
request = requests.get('https://api.github.com' + query, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception(
"Query failed to run by returning code of {}. {},... {}".format(request.status_code, query, request.json()))
def run_query(query): def run_query(query):
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers) request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
@@ -114,7 +127,7 @@ def generate_commit_list(tz):
result = run_query(userInfoQuery) # Execute the query result = run_query(userInfoQuery) # Execute the query
username = result["data"]["viewer"]["login"] username = result["data"]["viewer"]["login"]
id = result["data"]["viewer"]["id"] id = result["data"]["viewer"]["id"]
print("user {} id".format(username)) print("user {}".format(username))
result = run_query(createContributedRepoQuery.substitute(username=username)) result = run_query(createContributedRepoQuery.substitute(username=username))
nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"] nodes = result["data"]["user"]["repositoriesContributedTo"]["nodes"]
@@ -133,14 +146,25 @@ def generate_commit_list(tz):
Saturday = 0 Saturday = 0
Sunday = 0 Sunday = 0
total_loc = 0
for repository in repos: for repository in repos:
if show_loc.lower() in ['true', '1', 't', 'y', 'yes']:
try:
datas = run_v3_api(get_loc_url.substitute(owner=repository["owner"]["login"], repo=repository["name"]))
for data in datas:
total_loc = total_loc + data[1] - data[2]
except Exception as e:
print(e)
result = run_query( result = run_query(
createCommittedDateQuery.substitute(owner=repository["owner"]["login"], name=repository["name"], id=id)) createCommittedDateQuery.substitute(owner=repository["owner"]["login"], name=repository["name"], id=id))
try: try:
committed_dates = result["data"]["repository"]["ref"]["target"]["history"]["edges"] committed_dates = result["data"]["repository"]["ref"]["target"]["history"]["edges"]
for committedDate in committed_dates: for committedDate in committed_dates:
date = datetime.datetime.strptime(committedDate["node"]["committedDate"], date = datetime.datetime.strptime(committedDate["node"]["committedDate"],
"%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.utc).astimezone(timezone(tz)) "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.utc).astimezone(
timezone(tz))
hour = date.hour hour = date.hour
weekday = date.strftime('%A') weekday = date.strftime('%A')
if 6 <= hour < 12: if 6 <= hour < 12:
@@ -199,6 +223,10 @@ def generate_commit_list(tz):
if day['percent'] > max_element['percent']: if day['percent'] > max_element['percent']:
max_element = day max_element = day
days_title = 'I\'m Most Productive on ' + max_element['name'] + 's' days_title = 'I\'m Most Productive on ' + max_element['name'] + 's'
if show_loc.lower() in ['true', '1', 't', 'y', 'yes']:
string = string + '![Lines of code](https://img.shields.io/badge/From%20Hello%20World%20I\'ve%20written-' + locale.format_string(
"%d", total_loc,
grouping=True) + '%20Lines%20of%20code-blue)\n\n'
string = string + '**' + title + '** \n\n' + '```text\n' + make_commit_list(one_day) + '\n\n```\n' string = string + '**' + title + '** \n\n' + '```text\n' + make_commit_list(one_day) + '\n\n```\n'
string = string + '📅 **' + days_title + '** \n\n' + '```text\n' + make_commit_list(dayOfWeek) + '\n\n```\n' string = string + '📅 **' + days_title + '** \n\n' + '```text\n' + make_commit_list(dayOfWeek) + '\n\n```\n'
@@ -211,7 +239,7 @@ def get_stats():
request = requests.get(f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}") request = requests.get(f"https://wakatime.com/api/v1/users/current/stats/last_7_days?api_key={waka_key}")
if request.status_code == 200: if request.status_code != 401:
data = request.json() data = request.json()
if showCommit.lower() in ['true', '1', 't', 'y', 'yes']: if showCommit.lower() in ['true', '1', 't', 'y', 'yes']:
stats = stats + generate_commit_list(tz=data['data']['timezone']) + '\n\n' stats = stats + generate_commit_list(tz=data['data']['timezone']) + '\n\n'