Skip to content

Commit 35b23db

Browse files
committed
feat: POC of a commit logging workflow
1 parent c140584 commit 35b23db

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Workflow to log commit information to a BigQuery table
2+
# BQ Schema:
3+
# [
4+
# {
5+
# "mode": "REQUIRED",
6+
# "name": "commit_sha",
7+
# "type": "STRING"
8+
# },
9+
# {
10+
# "mode": "REQUIRED",
11+
# "name": "commit_author",
12+
# "type": "STRING"
13+
# },
14+
# {
15+
# "mode": "REQUIRED",
16+
# "name": "commit_message",
17+
# "type": "STRING"
18+
# },
19+
# {
20+
# "mode": "REQUIRED",
21+
# "name": "commit_date_time",
22+
# "type": "STRING"
23+
# }
24+
# ]
25+
name: 'commit_logging'
26+
on:
27+
workflow_dispatch:
28+
29+
jobs:
30+
log_commits:
31+
runs-on: 'ubuntu-latest'
32+
33+
permissions:
34+
contents: 'read'
35+
id-token: 'write'
36+
37+
steps:
38+
- id: 'checkout'
39+
uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' # ratchet:actions/checkout@v4
40+
41+
- id: 'auth'
42+
name: 'Authenticate to Google Cloud'
43+
uses: 'google-github-actions/auth@2dd133ffa2bc779c0854b1999e993c416c87164b' # ratchet:google-github-actions/[email protected]
44+
with:
45+
access_token_lifetime: '900s'
46+
workload_identity_provider: 'projects/bradegler-commit-log-exp-1b/locations/global/workloadIdentityPools/becle1b-wif-64840b/providers/becle1b-wif-64840b'
47+
service_account: 'github-automation-bot@bradegler-commit-log-exp-1b.iam.gserviceaccount.com'
48+
create_credentials_file: true
49+
activate_credentials_file: true
50+
51+
- uses: 'actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c' # ratchet:actions/setup-python@v4
52+
with:
53+
python-version: '3.11.2'
54+
55+
- name: 'install_dependencies'
56+
shell: 'bash'
57+
run: |
58+
pip install google-cloud-bigquery
59+
60+
- name: 'read_commits'
61+
shell: 'bash'
62+
run: |
63+
git log --pretty=format:"%H%x7c%aE%x7c%s%x7c%ai%x7c" > data.psv
64+
echo "commit_sha|commit_author|commit_message|commit_date_time|" > labels.psv
65+
66+
echo "{\"commits\": [" > output.psv
67+
jq -nRc '
68+
# From the web:
69+
def objectify($keys):
70+
[$keys, .] | transpose | map({(.[0]): .[1]}) | add;
71+
72+
(input|split("|")) as $keys
73+
| inputs | split("|") | objectify($keys)
74+
| del(..|select(. == ""))
75+
' labels.psv data.psv | sed 's/}/},/g' | sed '$s/,$//' >> output.psv
76+
77+
echo "]}" >> output.psv
78+
79+
- name: 'read_log_write_bq'
80+
shell: python {0}
81+
run: |
82+
import json
83+
84+
from google.cloud import bigquery
85+
86+
table_id = "bradegler-commit-log-exp-1b.commit_log_exp.commit_log"
87+
88+
client = bigquery.Client(project='bradegler-commit-log-exp-1b')
89+
90+
with open('output.psv', 'r') as file:
91+
data = json.load(file)
92+
93+
print(data["commits"])
94+
errors = client.insert_rows_json(table_id, data["commits"])
95+
if errors == []:
96+
print("New rows have been added.")
97+
else:
98+
print("Encountered errors while inserting rows: {}".format(errors))
99+
exit(1)
100+
101+
102+
103+

0 commit comments

Comments
 (0)