Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Go through all the workflows and clean them up ahead of the next major release #462

Merged
merged 8 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .github/actions/copy-workflow-go/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ runs:
# As of Go 1.19 io/ioutil is deprecated
# We automate its upgrade here because it is quite a widely used package
while read file; do
sed -i 's/ioutil.NopCloser/io.NopCloser/' "${file}";
sed -i 's/ioutil.ReadAll/io.ReadAll/' "${file}";
sed -i 's/ioutil.NopCloser/io.NopCloser/' "$file";
sed -i 's/ioutil.ReadAll/io.ReadAll/' "$file";
# ReadDir replacement might require manual intervention (https://pkg.go.dev/io/ioutil#ReadDir)
sed -i 's/ioutil.ReadDir/os.ReadDir/' "${file}";
sed -i 's/ioutil.ReadFile/os.ReadFile/' "${file}";
sed -i 's/ioutil.TempDir/os.MkdirTemp/' "${file}";
sed -i 's/ioutil.TempFile/os.CreateTemp/' "${file}";
sed -i 's/ioutil.WriteFile/os.WriteFile/' "${file}";
sed -i 's/ioutil.Discard/io.Discard/' "${file}";
sed -i 's/ioutil.ReadDir/os.ReadDir/' "$file";
sed -i 's/ioutil.ReadFile/os.ReadFile/' "$file";
sed -i 's/ioutil.TempDir/os.MkdirTemp/' "$file";
sed -i 's/ioutil.TempFile/os.CreateTemp/' "$file";
sed -i 's/ioutil.WriteFile/os.WriteFile/' "$file";
sed -i 's/ioutil.Discard/io.Discard/' "$file";
done <<< "$(find . -type f -name '*.go')"

go install golang.org/x/tools/cmd/[email protected]
Expand All @@ -79,7 +79,7 @@ runs:
fi
fi
- name: go mod tidy (on initial workflow deployment)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 }}
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
uses: protocol/[email protected]
with:
working-directory: ${{ env.TARGET_REPO_DIR }}
Expand All @@ -90,7 +90,7 @@ runs:
git commit -m "run go mod tidy"
fi
- name: gofmt -s (on initial workflow deployment and on new Go version)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 || env.GO_VERSION_BUMP == 1}}
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1 || env.GO_VERSION_BUMP == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/add-label-by-query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
steps:
- name: Add label by query
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
QUERY: ${{ github.event.inputs.query }}
LABEL: ${{ github.event.inputs.label }}
DRY_RUN: ${{ github.event.inputs.dry-run }}
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ jobs:
fetch-depth: 0
- name: Check if we should automerge
id: should-automerge
env:
BASE_REF: ${{ github.event.pull_request.base.ref }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
for commit in $(git rev-list --first-parent origin/${{ github.event.pull_request.base.ref }}..${{ github.event.pull_request.head.sha }}); do
for commit in $(git rev-list --first-parent origin/$BASE_REF..$HEAD_SHA); do
committer=$(git show --format=$'%ce' -s $commit)
echo "Committer: $committer"
if [[ "$committer" != "[email protected]" ]]; then
echo "Commit $commit wasn't committed by web3-bot, but by $committer."
echo "::set-output name=status::false"
echo "status=false" >> $GITHUB_OUTPUT
exit
fi
done
echo "::set-output name=status::true"
echo "status=true" >> $GITHUB_OUTPUT
automerge:
needs: automerge-check
runs-on: ubuntu-latest
Expand All @@ -43,13 +46,13 @@ jobs:
uses: lewagon/wait-on-check-action@3a563271c3f8d1611ed7352809303617ee7e54ac # v1.2.0
with:
ref: ${{ github.event.pull_request.head.sha }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token: ${{ github.token }}
wait-interval: 10
running-workflow-name: '${{ inputs.job }} / ${{ github.job }}' # the name of the check for this job
- name: Merge PR
uses: pascalgn/automerge-action@eb68b061739cb9d81564f8e812d0b3c45f0fb09a # v0.15.5
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
GITHUB_TOKEN: "${{ github.token }}"
MERGE_LABELS: ""
MERGE_METHOD: "squash"
MERGE_DELETE_BRANCH: true
8 changes: 5 additions & 3 deletions .github/workflows/check-3rd-party.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ jobs:
- id: set-matrix
run: |
TARGETS=$(find . -type f -name "*.yml" | sed "s|^\./||" | grep -v workflow-templates/header.yml | jq -R -s -c 'split("\n")[:-1]')
echo "::set-output name=targets::$TARGETS"
echo "targets=$TARGETS" >> $GITHUB_OUTPUT
check:
needs: [ matrix ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
file: ${{ fromJson(needs.matrix.outputs.targets) }}
file: ${{ fromJSON(needs.matrix.outputs.targets) }}
name: ${{ matrix.file }}
steps:
- uses: actions/checkout@v3
- name: Run check
run: .github/workflows/check-3rd-party.sh ${{ matrix.file }}
env:
FILE: ${{ matrix.file }}
run: .github/workflows/check-3rd-party.sh $FILE
24 changes: 12 additions & 12 deletions .github/workflows/copy-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
cfg: ${{ fromJson(github.event.inputs.targets) }}
cfg: ${{ fromJSON(github.event.inputs.targets) }}
max-parallel: 10
env:
TARGET_REPO_DIR: "target-repo"
Expand Down Expand Up @@ -53,40 +53,40 @@ jobs:
- name: determine files to add
# By setting the environment variable, it's possible to programmatically add / modify this list.
# See https://github.com/protocol/.github/blob/38135c75e47839623bf9b2748275d8c6167a8fa8/.github/workflows/copy-workflow.yml#L163-L168 for an example, how we used to make use of this.
run: |
files=${{ toJson(toJson(matrix.cfg.files)) }}
extra_files=${{ toJson(toJson(matrix.cfg.extra_files)) }}
files=$(echo -e "$files" "$extra_files" | jq -nc '[inputs] | add')
echo "FILES=$files" >> $GITHUB_ENV
env:
FILES: |
${{ toJSON(matrix.cfg.files) }}
${{ toJSON(matrix.cfg.extra_files) }}
run: echo "FILES=$(jq -nc '[inputs] | add' <<< "$FILES")" >> $GITHUB_ENV
- name: is initial workflow deployment
# INITIAL_WORKFLOW_DEPLOYMENT=1 iff none of the files in the target repository exist yet
run: |
initial_workflow_deployment=1
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
for f in $(jq -r '.[]' <<< "$FILES"); do
if [[ -f $TARGET_REPO_DIR/$f ]]; then
initial_workflow_deployment=0
break
fi
done
echo "INITIAL_WORKFLOW_DEPLOYMENT=$initial_workflow_deployment" >> $GITHUB_ENV
- name: remove Travis (on initial workflow deployment)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 }}
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -f .travis.yml ]]; then
git rm .travis.yml
git commit -m "disable Travis"
fi
- name: remove CircleCI (on initial workflow deployment)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 }}
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -d .circleci ]]; then
git rm -r .circleci
git commit -m "disable CircleCI"
fi
- name: remove gx (on initial workflow deployment)
if: ${{ env.INITIAL_WORKFLOW_DEPLOYMENT == 1 }}
if: env.INITIAL_WORKFLOW_DEPLOYMENT == 1
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
if [[ -d .gx ]]; then
Expand All @@ -111,7 +111,7 @@ jobs:
}
TEMPLATE_ENGINE: s#\$\{\{\{\s*(.*?)\s*\}\}\}#`jq -cjn 'env.CONTEXT | fromjson.$1'`#ge
run: |
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
for f in $(jq -r '.[]' <<< "$FILES"); do
echo -e "\nProcessing $f."
# add DO NOT EDIT header
tmp=$(mktemp)
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:
working-directory: ${{ env.TARGET_REPO_DIR }}
run: echo "NEEDS_UPDATE=$(git rev-list HEAD...origin/$(git rev-parse --abbrev-ref HEAD) --ignore-submodules --count 2> /dev/null || echo 1)" >> $GITHUB_ENV
- name: Force push web3-bot/sync branch
if: ${{ env.NEEDS_UPDATE != 0 }}
if: env.NEEDS_UPDATE != 0
working-directory: ${{ env.TARGET_REPO_DIR }}
run: |
git checkout -B web3-bot/sync
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/create-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ jobs:
done
failed=()
for target in ${targets[@]}; do
echo "Processing ${target}"
base="$(gh api "/repos/${target}" --jq '.default_branch')"
echo "Processing $target"
base="$(gh api "/repos/$target" --jq '.default_branch')"
# checks if a PR needs to be created
if [[ "$(gh api -X GET "/repos/${target}/compare/${base}...${{ env.PR_BRANCH }}" --jq '.status')" == 'ahead' ]]; then
if [[ "$(gh api -X GET "/repos/${target}/pulls" -f head="$(echo "${target}" | cut -d/ -f1):${{ env.PR_BRANCH }}" -f base="$base" --jq 'length')" != '0' ]] ; then
if [[ "$(gh api -X GET "/repos/$target/compare/$base...$PR_BRANCH" --jq '.status')" == 'ahead' ]]; then
if [[ "$(gh api -X GET "/repos/$target/pulls" -f head="$(echo "$target" | cut -d/ -f1):$PR_BRANCH" -f base="$base" --jq 'length')" != '0' ]] ; then
echo "The PR already exists. Skipping."
continue
fi
else
echo "The branch does not exist or has diverged from ${base}. Skipping."
echo "The branch does not exist or has diverged from $base. Skipping."
continue
fi
# tries to create a PR in target
Expand All @@ -47,7 +47,7 @@ jobs:
pr_create_cooldown_in_seconds=1
# max cumulative sleep time - 68.25 minutes
while true; do
if result="$(gh api "/repos/$target/pulls" -f title="${{ env.PR_TITLE }}" -f head="${{ env.PR_BRANCH }}" -f base="$base")"; then
if result="$(gh api "/repos/$target/pulls" -f title="$PR_TITLE" -f head="$PR_BRANCH" -f base="$base")"; then
echo "Successfully created a PR for '$target' ($pr_create_attempt/$pr_create_max_attempts)"
echo "Sleeping for $pr_create_cooldown_in_seconds seconds before creating a next one"
sleep $pr_create_cooldown_in_seconds
Expand Down
31 changes: 18 additions & 13 deletions .github/workflows/dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ on:
push:
branches: [ master, testing ]
workflow_dispatch:
concurrency:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

Expand Down Expand Up @@ -52,8 +52,8 @@ jobs:
targets+=($(jq -c ".repositories[] | $defaults + ." $config))
echo "::endgroup::"
done
batches=$(jq -sc '[. | _nwise(${{ env.MAX_REPOS_PER_WORKFLOW }})] | to_entries' <<< "${targets[@]}")
echo "::set-output name=batches::$batches"
batches=$(jq -sc "[. | _nwise($MAX_REPOS_PER_WORKFLOW)] | to_entries" <<< "${targets[@]}")
echo "batches=$batches" >> $GITHUB_OUTPUT
dispatch:
needs: [ matrix ]
name: Dispatch copy workflow(batch ${{ matrix.cfg.key }})
Expand All @@ -73,7 +73,7 @@ jobs:
# The triggered copy workflow runs use that final array as their matrix.
# Since max-parallel here is 1, we'll end up with at most max-parallel from copy workflow + 1 parallel jobs.
# 20 is the upper limit on parallel jobs on a free plan.
cfg: ${{ fromJson(needs.matrix.outputs.batches) }}
cfg: ${{ fromJSON(needs.matrix.outputs.batches) }}
max-parallel: 1
env:
GITHUB_TOKEN: ${{ secrets.WEB3BOT_GITHUB_TOKEN }}
Expand All @@ -82,31 +82,36 @@ jobs:
steps:
- id: dispatch
name: Dispatch copy workflow
env:
TARGETS: ${{ toJSON(matrix.cfg.value) }}
run: |
start_date="$(date +%s)"
# 2 toJson calls are needed to turn the array of targets into a string
echo '{"targets":${{ toJson(toJson(matrix.cfg.value)) }}}' | jq -c '.' | gh workflow run "${{ env.WORKFLOW_YML }}" --ref "${{ github.ref }}" --repo "${{ env.WORKFLOW_REPO }}" --json
echo "::set-output name=start_date::$start_date"
gh workflow run "$WORKFLOW_YML" --ref "$GITHUB_REF" --repo "$WORKFLOW_REPO" --field "targets=$TARGETS"
echo "start_date=$start_date" >> $GITHUB_OUTPUT
- id: run
name: Wait for copy workflow run to start
env:
START_DATE: ${{ steps.dispatch.outputs.start_date }}
run: |
# checks every 3 seconds until the most recent copy workflow run's created_at is later than this job's start_date
while sleep 3; do
run="$(gh api "/repos/${{ env.WORKFLOW_REPO }}/actions/workflows/${{ env.WORKFLOW_YML }}/runs?per_page=1" --jq '.workflow_runs[0]')"
run="$(gh api "/repos/$WORKFLOW_REPO/actions/workflows/$WORKFLOW_YML/runs?per_page=1" --jq '.workflow_runs[0]')"
# nothing to check if no copy workflow run was returned
if [[ ! -z "$run" ]]; then
run_start_date="$(date --date="$(jq -r '.created_at' <<< "$run")" +%s)"
if [[ "$run_start_date" > "${{ steps.dispatch.outputs.start_date }}" ]]; then
echo "::set-output name=id::$(jq -r '.id' <<< "$run")"
if [[ "$run_start_date" > "$START_DATE" ]]; then
echo "id=$(jq -r '.id' <<< "$run")" >> $GITHUB_OUTPUT
break
fi
fi
done
- name: Wait for copy workflow run to complete
env:
RUN_ID: ${{ steps.run.outputs.id }}
run: |
# delays checking copy workflow's run status to save on GH API requests
sleep ${{ env.WORKFLOW_COMPLETION_CHECK_DELAY }}
sleep $WORKFLOW_COMPLETION_CHECK_DELAY

# checks every 3 seconds until the copy workflow run's status is completed
# redirects the stdout to /dev/null because it is very chatty
gh run watch "${{ steps.run.outputs.id }}" --repo "${{ env.WORKFLOW_REPO }}" > /dev/null
gh run watch "$RUN_ID" --repo "$WORKFLOW_REPO" > /dev/null
14 changes: 9 additions & 5 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:
- name: Determine version
run: echo "VERSION=$(jq -r .version version.json)" >> $GITHUB_ENV
- name: Determine branch
run: echo "DEFAULT_BRANCH=refs/heads/${{ github.event.repository.default_branch }}" >> $GITHUB_ENV
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: echo "DEFAULT_BRANCH=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
- name: Create a release, if we're on the default branch
run: echo "CREATETAG=true" >> $GITHUB_ENV
if: env.DEFAULT_BRANCH == github.ref
Expand All @@ -22,21 +24,23 @@ jobs:
id: getmergedpr
uses: actions-ecosystem/action-get-merged-pull-request@59afe90821bb0b555082ce8ff1e36b03f91553d9 # v1.0.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ github.token }}
- name: Check if the "release" label was set on the PR
if: steps.getmergedpr.outputs.number != '' && env.DEFAULT_BRANCH != github.ref
env:
LABELS: ${{ steps.getmergedpr.outputs.labels }}
run: |
while IFS= read -r label; do
if [[ "$label" == "release" ]]; then
echo "CREATETAG=true" >> $GITHUB_ENV
break
fi
done <<< "${{ steps.getmergedpr.outputs.labels }}"
done <<< "$LABELS"
- name: Create release
if: env.CREATETAG == 'true'
run: |
git fetch origin --tags
if ! $(git rev-list ${{ env.VERSION}}.. &> /dev/null); then
git tag ${{ env.VERSION }}
if ! $(git rev-list $VERSION.. &> /dev/null); then
git tag $VERSION
git push --tags
fi
21 changes: 13 additions & 8 deletions .github/workflows/stale-repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ jobs:
- uses: actions/checkout@v3
- id: repos
name: find repositories to check
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
# returns a JSON array of repository objects with the following schema:
# [ {"config": "$JSON_CONFIG_FILE_PATH", "repo": "$GITHUB_REPOSITORY_FULL_NAME"} ]
repos="$(jq -nc '[inputs | .repositories[] | {"config":input_filename,"repo":.target}]' configs/*.json)"

if [[ '${{ github.event_name }}' == 'pull_request' ]]; then
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
# resets the checkout to PR target commit
git fetch origin ${{ github.event.pull_request.base.sha }}
git checkout ${{ github.event.pull_request.base.sha }}
git fetch origin $BASE_SHA
git checkout $BASE_SHA

# returns a JSON array of repository objects as they exist on pull request target
base="$(jq -nc '[inputs | .repositories[] | {"config":input_filename,"repo":.target}]' configs/*.json)"
Expand All @@ -33,16 +35,19 @@ jobs:
repos="$(jq -nc '$head - $base' --argjson head "$repos" --argjson base "$base")"
fi

echo "::set-output name=repos::$repos"
echo "repos=$repos" >> $GITHUB_OUTPUT
- name: find deleted / archived repositories
if: ${{ steps.repos.outputs.repos != '[]' }}
if: steps.repos.outputs.repos != '[]'
env:
GITHUB_TOKEN: ${{ github.token }}
REPOS: ${{ steps.repos.outputs.repos }}
run: |
status=0
while read config; do
echo "::group::$config"
while read repo; do
exists=true
output=$(curl -s -f -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/$repo") || exists=false
output=$(curl -s -f -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/repos/$repo") || exists=false
if ! $exists; then
echo "Repository $repo does not exist."
status=1
Expand All @@ -52,7 +57,7 @@ jobs:
echo "Repository $repo is archived."
status=1
fi
done <<< "$(jq -r '.[] | select(.config == $key) | .repo' --arg key "$config" <<< '${{ steps.repos.outputs.repos }}')"
done <<< "$(jq -r '.[] | select(.config == $key) | .repo' --arg key "$config" <<< "$REPOS")"
echo "::endgroup::"
done <<< "$(jq -r 'map(.config) | unique | .[]' <<< '${{ steps.repos.outputs.repos }}')"
done <<< "$(jq -r 'map(.config) | unique | .[]' <<< "$REPOS")"
exit $status
Loading