Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

174 lines · 9.6 KBRaw History
1name: Import campaign
2
3# Imports a varied set of public GitHub repositories into Diffinery as private test
4# repositories (<owner>/campaign-*), optionally runs their push workflows there, and reports what
5# came across and what ran (scripts/import-campaign.sh). Started by hand.
6#
7# Dispatched on GitHub, it starts the same workflow on Diffinery and prints its report;
8# on Diffinery it does the work. Imports use the IMPORT_GITHUB_TOKEN secret when it is
9# set (a read-only token for public repositories), otherwise GitHub's anonymous rate
10# limit applies and large repositories take hours. Set on GitHub, it is copied to
11# Diffinery's repository secrets before the run starts (the main sync doesn't copy it).
12on:
13 workflow_dispatch:
14 inputs:
15 repos:
16 description: GitHub repositories to import, separated by spaces
17 type: string
18 default: >-
19 octocat/git-consortium sindresorhus/slugify expressjs/cors spf13/pflag
20 pallets/markupsafe BurntSushi/walkdir actions/hello-world-javascript-action
21 actions/hello-world-docker-action jekyll/minima
22 minutes:
23 description: How long to wait for imports and workflow runs, in minutes
24 type: string
25 default: "300"
26 run_workflows:
27 description: Also run the imported repositories' push workflows (third-party code)
28 type: boolean
29 default: false
30 follow_run:
31 description: Instead of starting a campaign, print the report and logs of this Diffinery run (its id)
32 type: string
33 default: ""
34 peek:
35 description: Instead of starting a campaign, print how the running one is doing (imports, steps, runs) and stop
36 type: boolean
37 default: false
38
39permissions:
40 contents: read
41
42# A peek runs next to the campaign it looks at; campaigns queue behind each other.
43concurrency:
44 group: ${{ inputs.peek && 'import-campaign-peek' || 'import-campaign' }}
45 cancel-in-progress: false
46
47jobs:
48 relay:
49 name: Start the campaign on Diffinery and print its report
50 if: ${{ github.server_url == 'https://github.com' }}
51 runs-on: ubuntu-latest
52 timeout-minutes: 360
53 environment: production
54 env:
55 DIFFINERY: https://diffinery.com
56 TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }}
57 R: codician-team/diffinery
58 steps:
59 - name: Copy IMPORT_GITHUB_TOKEN to Diffinery
60 if: ${{ inputs.follow_run == '' && !inputs.peek }}
61 env:
62 IMPORT_GITHUB_TOKEN: ${{ secrets.IMPORT_GITHUB_TOKEN }}
63 run: |
64 set -euo pipefail
65 if [ -n "$IMPORT_GITHUB_TOKEN" ]; then
66 jq -n --arg v "$IMPORT_GITHUB_TOKEN" '{value: $v}' | curl -fsS --retry 4 --retry-delay 30 -o /dev/null -X PUT "$DIFFINERY/api/v3/repos/$R/actions/secrets/IMPORT_GITHUB_TOKEN" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data-binary @-
67 echo "IMPORT_GITHUB_TOKEN is set on GitHub and was copied to Diffinery"
68 else
69 echo "IMPORT_GITHUB_TOKEN is not set on GitHub; Diffinery's own copy is used if it has one"
70 fi
71 - name: Dispatch and follow
72 env:
73 REPOS: ${{ inputs.repos }}
74 MINUTES: ${{ inputs.minutes }}
75 RUN_WORKFLOWS: ${{ inputs.run_workflows }}
76 FOLLOW_RUN: ${{ inputs.follow_run }}
77 PEEK: ${{ inputs.peek }}
78 run: |
79 set -euo pipefail
80 api() { curl -fsS --retry 3 -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; }
81 if [ "$PEEK" = true ]; then
82 for id in $(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=5" | jq -r '.workflow_runs[] | select(.status != "completed") | .id'); do
83 echo "Campaign run $id on Diffinery:"
84 api GET "/repos/$R/actions/runs/$id/jobs" | jq -r '.jobs[] | " job \(.name): \(.status) \(.conclusion // "")"'
85 done
86 owner=$(api GET /user | jq -r .login)
87 since=$(date -u -d '-12 hours' +%Y-%m-%dT%H:%M:%S)
88 imports=$(api GET /diffinery/imports | jq -c --arg since "$since" '[.[] | select((.target.name // "") | startswith("campaign-")) | select(.created_at >= $since)] | sort_by(.created_at)')
89 jq -r '.[] | "\(.target.name): \(.status) (attempt \(.attempt))\(if .error then ": \(.error)" else "" end)",
90 ([.steps[] | select(.status == "running" or .status == "failed")][] | " step \(.title): \(.status) \(.message // "") \(.count // "")"),
91 (if .git then " git \(.git.phase // "") \(.git.message // "") (\(.git.bytes) bytes, \(.git.chunks) chunks, \(.git.tries) tries)" else empty end),
92 (.warnings[]? | " warning: \(.)")' <<<"$imports"
93 # Runs on the imported repositories, with each failed job's reason and the end of its log.
94 for name in $(jq -r '.[] | select(.status == "succeeded") | .target.name' <<<"$imports"); do
95 runs=$(api GET "/repos/$owner/$name/actions/runs?per_page=30") || continue
96 jq -r --arg n "$name" '.workflow_runs[] | "\($n) run \(.name): \(.status) \(.conclusion // "")"' <<<"$runs"
97 for run in $(jq -r '.workflow_runs[] | select(.conclusion == "failure") | .id' <<<"$runs"); do
98 jobs=$(api GET "/repos/$owner/$name/actions/runs/$run/jobs?per_page=100") || continue
99 for job in $(jq -r '.jobs[] | select(.conclusion == "failure") | .id' <<<"$jobs"); do
100 echo "::group::$name: $(jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | "\(.name)\(if .failure then " (\(.failure.message? // .failure))" else "" end)"' <<<"$jobs")"
101 { api GET "/repos/$owner/$name/actions/jobs/$job/logs" || true; } | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z //' | tail -60
102 echo "::endgroup::"
103 done
104 done
105 done
106 exit 0
107 fi
108 if [ -n "$FOLLOW_RUN" ]; then
109 id=$FOLLOW_RUN
110 else
111 # A new campaign replaces one still running on Diffinery (its imports are cancelled by the new one).
112 for old in $(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=20" | jq -r '.workflow_runs[] | select(.status != "completed") | .id'); do
113 echo "cancelling the campaign still running on Diffinery: $old"
114 api POST "/repos/$R/actions/runs/$old/cancel" >/dev/null || true
115 done
116 before=$(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=1" | jq -r '.workflow_runs[0].id // 0')
117 api POST "/repos/$R/actions/workflows/import-campaign.yml/dispatches" "$(jq -nc --arg r "$REPOS" --arg m "$MINUTES" --arg w "$RUN_WORKFLOWS" '{ref: "main", inputs: {repos: $r, minutes: $m, run_workflows: $w}}')" >/dev/null
118 for i in $(seq 1 60); do
119 run=$(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=1" | jq -c '.workflow_runs[0] // empty')
120 [ -n "$run" ] && [ "$(jq -r .id <<<"$run")" != "$before" ] && break
121 sleep 5
122 done
123 id=$(jq -r .id <<<"$run")
124 fi
125 echo "Diffinery run: $(api GET "/repos/$R/actions/runs/$id" | jq -r .html_url)"
126 while [ "$(api GET "/repos/$R/actions/runs/$id" | jq -r .status)" != completed ]; do sleep 30; done
127 run=$(api GET "/repos/$R/actions/runs/$id")
128 jobs=$(api GET "/repos/$R/actions/runs/$id/jobs")
129 for job in $(jq -r '.jobs[].id' <<<"$jobs"); do
130 jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | "Job \(.name): \(.conclusion // .status)\(if .failure then " (\(.failure.message? // .failure))" else "" end)"' <<<"$jobs"
131 log=$(curl -fsS "$DIFFINERY/api/v3/repos/$R/actions/jobs/$job/logs" -H "Authorization: Bearer $TOKEN" || echo "(no log)")
132 # Diffinery's job logs start each line with a timestamp.
133 log=$(sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z //' <<<"$log")
134 if grep -q '^# Import campaign' <<<"$log"; then
135 sed -n '/^# Import campaign/,$p' <<<"$log" | tee -a "$GITHUB_STEP_SUMMARY"
136 else
137 # No report: the job stopped before writing one, so show how it ended.
138 echo "::group::Last 150 log lines of job $job"
139 tail -150 <<<"$log"
140 echo "::endgroup::"
141 fi
142 done
143 [ "$(jq -r .conclusion <<<"$run")" = success ] || { echo "::error::the campaign run on Diffinery ended $(jq -r .conclusion <<<"$run")"; exit 1; }
144
145 campaign:
146 name: Import, run and report
147 if: ${{ github.server_url != 'https://github.com' }}
148 runs-on: ubuntu-latest
149 timeout-minutes: 360
150 env:
151 DIFFINERY: ${{ github.server_url }}
152 TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }}
153 GITHUB_TOKEN_FOR_IMPORT: ${{ secrets.IMPORT_GITHUB_TOKEN }}
154 steps:
155 - uses: actions/checkout@v4
156 - name: Import and run
157 env:
158 REPOS: ${{ inputs.repos }}
159 MINUTES: ${{ inputs.minutes }}
160 RUN_WORKFLOWS: ${{ inputs.run_workflows }}
161 run: |
162 if [ -n "$GITHUB_TOKEN_FOR_IMPORT" ]; then
163 echo "::notice::Mode: imports use the IMPORT_GITHUB_TOKEN secret"
164 else
165 echo "::warning::Mode: token-free. IMPORT_GITHUB_TOKEN is not set, so imports use GitHub's anonymous rate limit"
166 fi
167 # shellcheck disable=SC2086
168 REPORT=report.md scripts/import-campaign.sh $REPOS
169 cat report.md >> "$GITHUB_STEP_SUMMARY"
170 - uses: actions/upload-artifact@v4
171 if: always()
172 with:
173 name: import-campaign-report
174 path: report.md