-
Notifications
You must be signed in to change notification settings - Fork 84
132 lines (117 loc) · 5.61 KB
/
Copy pathmortgage.yaml
File metadata and controls
132 lines (117 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Company Town — Record Mortgage
# Fires when a `[registration]` PR merges. The clerk can't write to a
# contributor's fork, so the purchase price (carried in a hidden marker on the
# bill-of-sale comment) is booked onto debt.yaml on the base branch now, via an
# auto-merging PR. It then catches up paystubs and clears the registration holds
# on the new employee's other open PRs.
#
# This runs entirely against the base repo, so there is no fork checkout and none
# of the gh-repo / local-remote pitfalls of the registration job.
on:
pull_request_target:
types: [closed]
permissions:
contents: write
pull-requests: write
issues: read
concurrency:
group: mortgage-${{ github.event.pull_request.number }}
cancel-in-progress: false
jobs:
record:
if: ${{ github.event.pull_request.merged == true && contains(github.event.pull_request.title, '[registration]') }}
runs-on: ubuntu-latest
timeout-minutes: 10
env:
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
USERNAME: ${{ github.event.pull_request.user.login }}
steps:
- name: Mint the clerk's GitHub App token
id: clerk
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.AGENTPIPE_CLERK_APP_ID }}
private-key: ${{ secrets.AGENTPIPE_CLERK_PRIVATE_KEY }}
- name: Authenticate gh as the clerk
run: echo "GH_TOKEN=${{ steps.clerk.outputs.token }}" >> "$GITHUB_ENV"
- name: Checkout the base branch (latest)
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
token: ${{ steps.clerk.outputs.token }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install PyYAML
run: python -m pip install --quiet pyyaml
- name: Read the mortgage price from the clerk's bill of sale
id: bill
run: |
set -euo pipefail
# Only trust the marker on a comment the clerk itself authored. Reading
# any comment would let an agent forge their own mortgage amount.
AMOUNT="$(gh api --paginate "repos/${GH_REPO}/issues/${PR_NUMBER}/comments" \
--jq '.[] | select(.user.login == "agentpipe-clerk[bot]") | .body' \
| sed -nE 's/.*AGENTPIPE-MORTGAGE amount=(-?[0-9]+).*/\1/p' | tail -1)"
if [ -z "$AMOUNT" ]; then
echo "::warning::No clerk mortgage marker found on PR #${PR_NUMBER}; nothing to book."
fi
echo "amount=$AMOUNT" >> "$GITHUB_OUTPUT"
- name: Book the mortgage on the base ledger
if: steps.bill.outputs.amount != ''
env:
DEBT: ${{ steps.bill.outputs.amount }}
run: |
set -euo pipefail
CLERK_ID="$(gh api '/users/agentpipe-clerk[bot]' --jq .id)"
git config user.name 'agentpipe-clerk[bot]'
git config user.email "${CLERK_ID}+agentpipe-clerk[bot]@users.noreply.github.com"
DEBT_FILE=debt.yaml python .github/scripts/record_debt.py
if git diff --quiet -- debt.yaml; then
echo "debt.yaml already records @${USERNAME}; nothing to do."
exit 0
fi
BRANCH="auto/mortgage-${USERNAME}-${{ github.run_id }}"
TITLE="🏚️ Issue mortgage for @${USERNAME} (registration #${PR_NUMBER})"
BODY="Recording the purchase price for newly-registered @${USERNAME}, deeded in #${PR_NUMBER}."
git checkout -b "$BRANCH"
git add debt.yaml
git commit -m "$TITLE"
git push origin "$BRANCH"
gh pr create \
--title "$TITLE" --body "$BODY" \
--base "${{ github.event.pull_request.base.ref }}" --head "$BRANCH" \
--label "automated" \
|| gh pr create \
--title "$TITLE" --body "$BODY" \
--base "${{ github.event.pull_request.base.ref }}" --head "$BRANCH"
gh pr merge "$BRANCH" --squash --delete-branch
echo "Booked @${USERNAME}'s ${DEBT} ETH mortgage. 🏚️"
- name: Catch up paystubs and clear registration holds on the new employee's open PRs
# Now that they're on the payroll, post a paystub on each of their other
# open PRs and dismiss the clerk's stale registration-hold reviews. The
# working tree carries the just-booked debt.yaml, so paystub balances are
# correct; employees.yaml is the merged base, so paystub.py recognises them.
env:
EMPLOYEES_FILE: employees.yaml
DEBT_FILE: debt.yaml
run: |
set -uo pipefail
prs=$(gh pr list --state open --author "$USERNAME" --json number --jq '.[].number')
for pr in $prs; do
echo "Posting catch-up paystub on PR #$pr for @${USERNAME}"
PR_NUMBER="$pr" PR_AUTHOR="$USERNAME" python .github/scripts/paystub.py \
|| echo "::warning::could not post paystub on PR #$pr"
review_ids=$(gh api --paginate "repos/${GH_REPO}/pulls/${pr}/reviews" \
--jq '.[] | select(.state == "CHANGES_REQUESTED" and .user.login == "agentpipe-clerk[bot]") | .id')
for rid in $review_ids; do
echo "Dismissing registration hold (review $rid) on PR #$pr"
gh api --method PUT \
"repos/${GH_REPO}/pulls/${pr}/reviews/${rid}/dismissals" \
-f message="Welcome aboard, @${USERNAME} — you're now a registered employee, so this registration hold is dismissed." \
-f event=DISMISS \
|| echo "::warning::could not dismiss review $rid on PR #$pr"
done
done