Skip to content

🔂 Reset Test Repo #65

🔂 Reset Test Repo

🔂 Reset Test Repo #65

name: 🔂 Reset Test Repo
on:
workflow_dispatch:
env:
TARGET_REPO: "ynput/ayon-addon-action-testing"
TARGET_REPO_PROJECT_NAME: "action-testing"
DEFAULT_BRANCH: "develop"
MAIN_BRANCH: main
TEMPLATE_REPO: "ynput/ayon-addon-template"
GH_TOKEN: ${{ secrets.REPO_CTRL_TOKEN }}
REQUIRED_SECRECTS: CI_EMAIL,CI_USER,REPO_CTRL_TOKEN,YNPUT_BOT_TOKEN
jobs:
verify_repo_secrets:
runs-on: ubuntu-latest
steps:
- name: 🔎 Verify repo secrets for ${{ github.repository }}
id: verify_var
run: |
missing_vars=$(echo ${{ env.REQUIRED_SECRECTS }} | tr ',' '\n' | while read var; do
gh secret list --repo ${{ github.repository }} --json name --jq '.[].name' | grep -qx "$var" || echo "$var"
done)
if [ -n "$missing_vars" ]; then
echo "::error::The following required variables are missing: $missing_vars for repository ${{ github.repository }}"
exit 1
else
echo "::notice::All required variables are present."
fi
reset-test-repo:
runs-on: ubuntu-latest
needs: verify_repo_secrets
steps:
# TODO Implement early return to check if all required repo variables exist
# TODO Implement early return to check against list of testing repos
- name: Check for existing repo
id: repo-check
run: |
if ! gh repo view "${{ env.TARGET_REPO }}" > /dev/null 2>&1; then
echo "::error::Repository `${{ env.TARGET_REPO }}` doesn't exists - can't reset. Please create the repository first."
echo "repo_present=false" >> $GITHUB_OUTPUT
exit 1
fi
echo "repo_present=true" >> $GITHUB_OUTPUT
echo "::notice::Found Repository ${{ env.TARGET_REPO }}."
exit 0
- name: Delete repository if exists
if: steps.repo-check.outputs.repo_present
run: |
echo "::notice:: Deleting repository `${{ env.TARGET_REPO }}`"
gh repo delete "${{ env.TARGET_REPO }}" --yes
- name: Create new Repo `${{ env.TARGET_REPO }}` from template `${{ env.TEMPLATE_REPO }}`
run: |
gh repo create ${{ env.TARGET_REPO }} --template ${{ env.TEMPLATE_REPO }} --public
echo "::notice::Recreated repository `${{ env.TARGET_REPO }}` from template `${{ env.TEMPLATE_REPO }}`"
- name: Show branches
run: |
gh api repos/${{ env.TARGET_REPO }}/branches --jq '.[].name'
prepare-repo:
runs-on: ubuntu-latest
needs: reset-test-repo
env:
DEFAULT_BRANCH: "develop"
steps:
- name: Add ${{ github.actor }} as repo collaborator
# WARNING critical step - user will loose admin-permission in repo if step is removed or altered incorrect
if: always()
run: |
gh api --method PUT \
-H "Accept: application/vnd.github.v3+json" \
"/repos/${{ env.TARGET_REPO }}/collaborators/${{ github.actor }}" \
-f permission=admin
- name: Set repo secrets
# INFO deprecated - should be set from organization
run: |
# gh secret set YNPUT_BOT_TOKEN --body "${{ secrets.YNPUT_BOT_TOKEN }}" --repo ${{ env.TARGET_REPO }}
# gh secret set CI_USER --body "${{ secrets.CI_USER }}" --repo ${{ env.TARGET_REPO }}
# gh secret set CI_EMAIL --body "${{ secrets.CI_EMAIL }}" --repo ${{ env.TARGET_REPO }}
echo "$(gh secret list --repo ${{ env.TARGET_REPO }})"
- name: Set repo variables
run: |
gh variable set PATCH_BUMP_LABEL --body "bugfix" --repo ${{ env.TARGET_REPO }}
gh variable set MINOR_BUMP_LABEL --body "feature, enhancement" --repo ${{ env.TARGET_REPO }}
gh variable set MAIN_BRANCH --body "main" --repo ${{ env.TARGET_REPO }}
gh variable set PROJECT_NAME --body ${{ env.TARGET_REPO_PROJECT_NAME }} --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_ORDER --body "feature,enhancement,bugfix,refactor,docs,test,pr" --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_FEATURE --body "feature" --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_ENHANCEMENT --body "enhancement" --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_BUGFIX --body "bugfix" --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_DOCS --body "documentation" --repo ${{ env.TARGET_REPO }}
gh variable set CHANGELOG_TEST --body "test" --repo ${{ env.TARGET_REPO }}
echo "$(gh variable list --repo ${{ env.TARGET_REPO }})"
- name: Add repo labels
run: |
gh label create "feature" --color "#008672" --description "New functionality which is not present so far" --repo ${{ env.TARGET_REPO }}
gh label create "bugfix" --color "#f96713" --description "Something got fixed" --repo ${{ env.TARGET_REPO }}
echo "$(gh label list --repo ${{ env.TARGET_REPO }})"
local-changes:
runs-on: ubuntu-latest
needs: prepare-repo
steps:
- name: Checkout ${{ env.TARGET_REPO }} repo
uses: actions/checkout@v4
with:
token: ${{ env.GH_TOKEN }}
repository: ${{ env.TARGET_REPO }}
ref: ${{ env.DEFAULT_BRANCH }}
- name: Add PR Template
# TODO change develop to main
run: |
curl -O https://raw.githubusercontent.com/${{ github.repository }}/develop/.github/pull_request_template.md
mv pull_request_template.md ./.github/pull_request_template.md
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
git add ./.github/pull_request_template.md
git commit -m "Add pr template"
git push origin ${{ env.DEFAULT_BRANCH }}
- name: Add initial-release workflow
# TODO change develop to main
run: |
curl -O https://raw.githubusercontent.com/${{ github.repository }}/develop/caller_workflows/addon_repo_initial_release.yml
mv addon_repo_initial_release.yml ./.github/workflows/initial_release.yml
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
git add ./.github/workflows/initial_release.yml
git commit -m "Add initial release trigger workflow"
git push origin ${{ env.DEFAULT_BRANCH }}
- name: Add release workflow
run: |
curl -O https://raw.githubusercontent.com/${{ github.repository }}/develop/caller_workflows/addon_repo_release_trigger.yml
mv addon_repo_release_trigger.yml ./.github/workflows/release_trigger.yml
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
git add ./.github/workflows/release_trigger.yml
git commit -m "Add release trigger workflow"
git push origin ${{ env.DEFAULT_BRANCH }}
- name: 🗒️ Adjust package.py
run: |
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
# INFO capitalize all letters after a "-" and the first one
repo_name_cap=$(echo "${{ env.TARGET_REPO_PROJECT_NAME }}" | sed -E 's/(^|-)(.)/\1\U\2/g')
sed -i "s/^version = \".*\"/version = \"0.1.0\"/" package.py
sed -i "s/^name = \".*\"/name = \"${{ env.TARGET_REPO_PROJECT_NAME }}\"/" package.py
sed -i "s/^title = \".*\"/title = \"$repo_name_cap\"/" package.py
git add package.py
git commit -m "Add warning information to readme."
git push origin ${{ env.DEFAULT_BRANCH }}
- name: 🗒️ Adjust readme
run: |
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
echo -e "# Not intended for Development\nThis is a testing repository for github actions.<br>Changes in this repository are likely to be reset by automanted testing procedures.<br>Don't use this repository for any development - use [ynput/ci-testing](https://github.com/ynput/ci-testing) instead." > README.md
git add README.md
git commit -m "Add warning information to readme"
git push origin ${{ env.DEFAULT_BRANCH }}
update-repo:
runs-on: ubuntu-latest
needs: local-changes
steps:
- name: Get latest sha from ${{ env.DEFAULT_BRANCH }}
id: latest-sha
run: |
commit_sha=$(gh api /repos/${{ env.TARGET_REPO }}/branches/develop -q '.commit.sha')
echo "sha=$commit_sha" >> $GITHUB_OUTPUT
- name: Add main branch
run: |
gh api \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
/repos/${{ env.TARGET_REPO }}/git/refs \
-f ref="refs/heads/${{ env.MAIN_BRANCH }}" \
-f sha="${{ steps.latest-sha.outputs.sha }}"
# TODO renable as soon as Teama ctor ID works again
# - name: Update protect_main.json file
# # TODO change develop to main
# run: |
# curl -O https://raw.githubusercontent.com/${{ github.repository }}/develop/branch_rules/protect_main.json
# cat protect_main.json | jq '.bypass_actors[0].actor_id = ${{ secrets.ACTOR_ID_TEAM }}' > protect_main_insert.json
# - name: Apply ruleset for '${{ env.MAIN_BRANCH }}'
# run: |
# gh api \
# --method POST \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# /repos/${{ env.TARGET_REPO }}/rulesets \
# --input protect_main_insert.json
# - name: Update protect_develop.json file
# # TODO change develop to main
# run: |
# curl -O https://raw.githubusercontent.com/${{ github.repository }}/develop/branch_rules/protect_develop.json
# cat protect_develop.json | jq '.bypass_actors[0].actor_id = ${{ secrets.ACTOR_ID_REPO_ROLE }} | .bypass_actors[1].actor_id = ${{ secrets.ACTOR_ID_TEAM }} | .bypass_actors[2].actor_id = ${{ secrets.ACTOR_ID_ORG_ADMIN }}' > protect_develop_insert.json
# - name: Apply ruleset for 'develop'
# run: |
# gh api \
# --method POST \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# /repos/${{ env.TARGET_REPO }}/rulesets \
# --input protect_develop_insert.json
prepare-release:
runs-on: ubuntu-latest
needs: update-repo
steps:
- name: Create GitHub Release
run: |
gh workflow run initial_release.yml --repo ${{ env.TARGET_REPO }}
- name: Poll for Draft Release
# TODO check if draft release creation emmits a signal or something to avoid this kind of pulling
id: poll-release
run: |
for i in {1..20}; do
DRAFT_RELEASE=$(gh release list --repo ${{ env.TARGET_REPO }} --limit 1 --json isDraft --jq '.[0].isDraft')
if [[ "$DRAFT_RELEASE" == "true" ]]; then
echo "Draft release found."
release_tag=$(gh release list --repo ${{ env.TARGET_REPO }} --limit 1 --json tagName --jq '.[0].tagName')
echo "release_tag=$release_tag" >> $GITHUB_OUTPUT
break
else
echo "Draft release not found yet, retrying in 15 seconds..."
sleep 15
fi
done
- name: Publish Release
if: steps.poll-release.outputs.release_tag
run: |
gh release edit ${{ steps.poll-release.outputs.release_tag }} --draft=false --repo ${{ env.TARGET_REPO }}
create-dummy-prs:
runs-on: ubuntu-latest
needs: prepare-release
steps:
- name: Checkout ${{ env.TARGET_REPO }} repo
uses: actions/checkout@v4
with:
token: ${{ env.GH_TOKEN }}
repository: ${{ env.TARGET_REPO }}
ref: ${{ env.DEFAULT_BRANCH }}
- name: Create first feature branch
run: |
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
git checkout -b test_feature01
date >> date.txt
git add date.txt
git commit -m "Add date file"
git push -u origin test_feature01
pr_body="
# Date file added
## Summary
Got a date file included
## Root Cause Analysis
[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)
## Changelog
* Add date file
* Commit it
* Fix no date file
## Testing Strategy
Tested all the way.
## Checklist
* [x] The fix has been locally tested
* [x] New unit tests have been added to prevent future regressions
* [x] The documentation has been updated if necessary
"
gh pr create --base ${{ env.DEFAULT_BRANCH }} --head test_feature01 --title "Add date file" --body "$pr_body" --label "bugfix" --assignee "${{ github.actor }}"
- name: Create second feature branch
run: |
git config --global user.name "${{ secrets.CI_USER }}"
git config --global user.email "${{ secrets.CI_EMAIL }}"
git checkout -b test_feature02
free -h >> memory_usage.txt
git add memory_usage.txt
git commit -m "Add memory usage file"
git push -u origin test_feature02
pr_body="
# Memory usage check added
## Summary
Got a check for memory usage implemented
## Root Cause Analysis
[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)
## Changelog
* Add text file
* Commit it the memory check
* Fix no memory usage file
## Testing Strategy
Tested all the way.
## Checklist
* [x] The fix has been locally tested
* [x] New unit tests have been added to prevent future regressions
* [x] The documentation has been updated if necessary
"
gh pr create --base ${{ env.DEFAULT_BRANCH }} --head test_feature02 --title "Add mem file" --body "$pr_body" --label "enhancement" --assignee "${{ github.actor }}"