|
| 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 | +jobs: |
| 29 | + log_commits: |
| 30 | + runs-on: 'ubuntu-latest' |
| 31 | + permissions: |
| 32 | + contents: 'read' |
| 33 | + id-token: 'write' |
| 34 | + steps: |
| 35 | + - id: 'checkout' |
| 36 | + uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' # ratchet:actions/checkout@v4 |
| 37 | + - id: 'auth' |
| 38 | + name: 'Authenticate to Google Cloud' |
| 39 | + uses: 'google-github-actions/auth@2dd133ffa2bc779c0854b1999e993c416c87164b' # ratchet:google-github-actions/[email protected] |
| 40 | + with: |
| 41 | + access_token_lifetime: '900s' |
| 42 | + workload_identity_provider: 'projects/bradegler-commit-log-exp-1b/locations/global/workloadIdentityPools/becle1b-wif-64840b/providers/becle1b-wif-64840b' |
| 43 | + service_account: 'github-automation-bot@bradegler-commit-log-exp-1b.iam.gserviceaccount.com' |
| 44 | + create_credentials_file: true |
| 45 | + activate_credentials_file: true |
| 46 | + - uses: 'actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c' # ratchet:actions/setup-python@v4 |
| 47 | + with: |
| 48 | + python-version: '3.11.2' |
| 49 | + - name: 'install_dependencies' |
| 50 | + shell: 'bash' |
| 51 | + run: | |
| 52 | + pip install google-cloud-bigquery |
| 53 | + - name: 'read_commits' |
| 54 | + shell: 'bash' |
| 55 | + run: |- |
| 56 | + git log --pretty=format:"%H%x7c%aE%x7c%s%x7c%ai%x7c" > data.psv |
| 57 | + echo "commit_sha|commit_author|commit_message|commit_date_time|" > labels.psv |
| 58 | +
|
| 59 | + echo "{\"commits\": [" > output.psv |
| 60 | + jq -nRc ' |
| 61 | + def objectify($keys): |
| 62 | + [$keys, .] | transpose | map({(.[0]): .[1]}) | add; |
| 63 | +
|
| 64 | + (input|split("|")) as $keys |
| 65 | + | inputs | split("|") | objectify($keys) |
| 66 | + | del(..|select(. == "")) |
| 67 | + ' labels.psv data.psv | sed 's/}/},/g' | sed '$s/,$//' >> output.psv |
| 68 | +
|
| 69 | + echo "]}" >> output.psv |
| 70 | + - name: 'read_log_write_bq' |
| 71 | + shell: python {0} |
| 72 | + run: |- |
| 73 | + import json |
| 74 | +
|
| 75 | + from google.cloud import bigquery |
| 76 | +
|
| 77 | + table_id = "bradegler-commit-log-exp-1b.commit_log_exp.commit_log" |
| 78 | +
|
| 79 | + client = bigquery.Client(project='bradegler-commit-log-exp-1b') |
| 80 | +
|
| 81 | + with open('output.psv', 'r') as file: |
| 82 | + data = json.load(file) |
| 83 | +
|
| 84 | + print(data["commits"]) |
| 85 | + errors = client.insert_rows_json(table_id, data["commits"]) |
| 86 | + if errors == []: |
| 87 | + print("New rows have been added.") |
| 88 | + else: |
| 89 | + print("Encountered errors while inserting rows: {}".format(errors)) |
| 90 | + exit(1) |
0 commit comments