Skip to content

Commit e0ebd2f

Browse files
committed
remove debug job, restrict create-pull-request to only awards.txt, add documentation
1 parent 8a764f0 commit e0ebd2f

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

.github/workflows/extend-awards.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: extend-awards
2+
run-name: Extending awards
3+
on:
4+
pull_request:
5+
types: [ closed ]
6+
branches:
7+
- master
8+
jobs:
9+
if_merged:
10+
if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-python@v5
15+
with:
16+
python-version: '3.13'
17+
- run: pip install requests
18+
- run: python extend-awards.py
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
GITHUB_CONTEXT: ${{ toJson(github) }}
22+
- uses: peter-evans/create-pull-request@v7
23+
with:
24+
add-paths: awards.csv
25+
commit-message: Extending awards.csv
26+
title: Extending awards.csv
27+
body: A PR was merged that solves an issue and awards.csv should be extended.

docs/dev/extend-awards.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Automatically extend awards.csv
2+
3+
## Overview
4+
5+
Whenever a pull request (PR) is merged in the [stacker.news](https://github.com/stackernews/stacker.news) repository, a [GitHub Action](https://docs.github.com/en/actions) is triggered:
6+
7+
If the merged PR solves an issue with [award tags](https://github.com/stackernews/stacker.news?tab=readme-ov-file#contributing),
8+
the amounts due to the PR and issue authors are calculated and corresponding lines are added to the [awards.csv](https://github.com/stackernews/stacker.news/blob/master/awards.csv) file,
9+
and a PR is opened for this change.
10+
11+
## Action
12+
13+
The action is defined in [.github/workflows/extend-awards.yml](.github/workflows/extend-awards.yml).
14+
15+
Filters on the event type and parameters ensure the action is [triggered only on merged PRs](https://stackoverflow.com/questions/60710209/trigger-github-actions-only-when-pr-is-merged).
16+
17+
The primary job consists of several steps:
18+
- [checkout](https://github.com/actions/checkout) checks out the repository
19+
- [setup-python](https://github.com/actions/setup-python) installs [Python](https://en.wikipedia.org/wiki/Python_(programming_language))
20+
- [pip](https://en.wikipedia.org/wiki/Pip_%28package_manager%29) installs the [requests](https://docs.python-requests.org/en/latest/index.html) module
21+
- a script (see below) is executed, which appends lines to [awards.csv](awards.csv) if needed
22+
- [create-pull-request](https://github.com/peter-evans/create-pull-request) looks for modified files and creates (or updates) a PR
23+
24+
## Script
25+
26+
The script is [extend-awards.py](extend-awards.py).
27+
28+
The script extracts from the [environment](https://en.wikipedia.org/wiki/Environment_variable) an authentication token needed for the [GitHub REST API](https://docs.github.com/en/rest/about-the-rest-api/about-the-rest-api) and the [context](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs) containing the event details including the merged PR (formatted in [JSON](https://en.wikipedia.org/wiki/JSON)).
29+
30+
In the merged PR's title and body it searches for the first [GitHub issue URL](https://github.com/stackernews/stacker.news/issues/) or any number with a hash symbol (#) prefix, and takes this as the issue being solved by the PR.
31+
32+
Using the GitHub REST API it fetches the issue and analyzes its tags for difficulty and priority.
33+
34+
It fetches the issue's timeline and counts the number of reviews completed with status 'changes requested' to calculate the amount reduction.
35+
36+
It calculates the amounts due to the PR author and the issue author.
37+
38+
It reads the existing awards.csv file to suppress appending redundant lines (same user, PR, and issue) and fill known receive methods (same user).
39+
40+
Finally, it appends zero, one, or two lines to the awards.csv file.
41+
42+
## Diagnostics
43+
44+
In the GitHub web interface under 'Actions' each invokation of the action can be viewed, including environment and [output and errors](https://en.wikipedia.org/wiki/Standard_streams) of the script. First, the specific invokation is selected, then the job 'if_merged', then the step 'Run python extend-awards.py'. The environment is found by expanding the inner 'Run python extended-awards.py' on the first line.
45+
46+
The normal output includes details about the issue number found, the amount calculation, or the reason for not appending lines.
47+
48+
The error output may include a [Python traceback](https://realpython.com/python-traceback/) which helps to explain the error.
49+
50+
The environment contains in GITHUB_CONTEXT the event details, which may be required to understand the error.
51+
52+
## Security considerations
53+
54+
The create-pull-request step requires [workflow permissions](https://github.com/peter-evans/create-pull-request#workflow-permissions).

extend-awards.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import json, os, re, requests
2+
3+
difficulties = {'good-first-issue':20000,'easy':100000,'medium':250000,'medium-hard':500000,'hard':1000000}
4+
priorities = {'low':0.5,'medium':1.5,'high':2,'urgent':3}
5+
ignored = ['huumn', 'ekzyis']
6+
fn = 'awards.csv'
7+
8+
sess = requests.Session()
9+
headers = {'Authorization':'Bearer %s' % os.getenv('GITHUB_TOKEN') }
10+
awards = []
11+
12+
def getIssue(n):
13+
url = 'https://api.github.com/repos/stackernews/stacker.news/issues/' + n
14+
r = sess.get(url, headers=headers)
15+
j = json.loads(r.text)
16+
return j
17+
18+
def findIssueInPR(j):
19+
p = re.compile('(#|https://github.com/stackernews/stacker.news/issues/)([0-9]+)')
20+
for m in p.finditer(j['title']):
21+
return m.group(2)
22+
if not 'body' in j or j['body'] is None:
23+
return
24+
for s in j['body'].split('\n'):
25+
for m in p.finditer(s):
26+
return m.group(2)
27+
28+
def addAward(user, kind, pr, issue, difficulty, priority, count, amount):
29+
if amount >= 1000000 and amount % 1000000 == 0:
30+
amount = str(int(amount / 1000000)) + 'm'
31+
elif amount >= 1000 and amount % 1000 == 0:
32+
amount = str(int(amount / 1000)) + 'k'
33+
for a in awards:
34+
if a[0] == user and a[1] == kind and a[2] == pr:
35+
print('found existing entry %s' % a)
36+
if a[8] != amount:
37+
print('warning: amount %s != %s' % (a[8], amount))
38+
return
39+
if count < 1:
40+
count = ''
41+
addr = '???'
42+
for a in awards:
43+
if a[0] == user and a[9] != '???':
44+
addr = a[9]
45+
print('adding %s,%s,%s,%s,%s,%s,%s,,%s,%s,???' % (user, kind, pr, issue, difficulty, priority, count, amount, addr))
46+
with open(fn, 'a') as f:
47+
print('%s,%s,%s,%s,%s,%s,%s,,%s,%s,???' % (user, kind, pr, issue, difficulty, priority, count, amount, addr), file=f)
48+
49+
def countReviews(pr):
50+
url = 'https://api.github.com/repos/stackernews/stacker.news/issues/%s/timeline' % pr
51+
r = sess.get(url, headers=headers)
52+
j = json.loads(r.text)
53+
count = 0
54+
for e in j:
55+
if e['event'] == 'reviewed' and e['state'] == 'changes_requested':
56+
count += 1
57+
return count
58+
59+
def checkPR(i):
60+
pr = str(i['number'])
61+
print('pr %s' % pr)
62+
n = findIssueInPR(i)
63+
if not n:
64+
print('pr %s does not solve an issue' % pr)
65+
return
66+
print('solves issue %s' % n)
67+
j = getIssue(n)
68+
difficulty = ''
69+
amount = 0
70+
priority = ''
71+
multiplier = 1
72+
for l in j['labels']:
73+
for d in difficulties:
74+
if l['name'] == 'difficulty:' + d:
75+
difficulty = d
76+
amount = difficulties[d]
77+
for p in priorities:
78+
if l['name'] == 'priority:' + p:
79+
priority = p
80+
multiplier = priorities[p]
81+
if amount * multiplier <= 0:
82+
print('issue gives no award')
83+
return
84+
count = countReviews(pr)
85+
if count >= 10:
86+
print('too many reviews, no award')
87+
return
88+
if count > 0:
89+
print('%d reviews, %d%% reduction' % (count, count * 10))
90+
award = amount * multiplier * (10 - count) / 10
91+
print('award is %d' % award)
92+
if i['user']['login'] not in ignored:
93+
addAward(i['user']['login'], 'pr', '#' + pr, '#' + n, difficulty, priority, count, award)
94+
if j['user']['login'] not in ignored:
95+
count = 0
96+
addAward(j['user']['login'], 'issue', '#' + pr, '#' + n, difficulty, priority, count, int(award / 10))
97+
98+
with open(fn, 'r') as f:
99+
for s in f:
100+
s = s.split('\n')[0]
101+
awards.append(s.split(','))
102+
103+
j = json.loads(os.getenv('GITHUB_CONTEXT'))
104+
checkPR(j['event']['pull_request'])

0 commit comments

Comments
 (0)