diff --git a/.github/workflows/daily-dev-bump.yaml b/.github/workflows/daily-dev-bump.yaml index 3122beb13e2..afecb653cb8 100644 --- a/.github/workflows/daily-dev-bump.yaml +++ b/.github/workflows/daily-dev-bump.yaml @@ -17,8 +17,6 @@ on: required: false type: boolean default: false - pull_request: - types: [closed] schedule: # * is a special character in YAML so you have to quote this string - cron: "0 8 * * *" # Run every day at midnight Pacific Time @@ -31,7 +29,7 @@ env: jobs: bump-version: - if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }} + if: ${{ github.repository == 'flutter/devtools' }} name: Bump Version runs-on: ubuntu-latest steps: @@ -121,22 +119,4 @@ jobs: GH_TOKEN: ${{ secrets.DEVTOOLS_WORKFLOW_BOT_TOKEN }} ORIGINAL_GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} IS_DRAFT: ${{ inputs.draft == true }} - clean-up-branches: - # If a pr is closed on a workflow bot PR, then clean up workflow bot branches. - if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.user.login == 'DartDevtoolWorkflowBot'}} - name: Clean up Dev Bump Branches - runs-on: ubuntu-latest - steps: - - name: Clean up branches - run: | - # Get 5 most recent branches of closed DartDevtoolWorkflowBot PRs. - CLOSED_BRANCH_NAMES=$(gh pr list -A DartDevtoolWorkflowBot -s closed -L 5 --search sort:created-desc | grep auto-bump- | sed 's|.*\(auto-bump-[[:digit:]]*\).*|\1|') - - # Get list of refs(branches) that exist on the remote - EXISTING_REFS=$(git ls-remote --heads | grep refs/heads/auto-bump-) - for CLOSED_BRANCH in $CLOSED_BRANCH_NAMES; do - if echo "$EXISTING_REFS" | grep -q "$CLOSED_BRANCH" ; then - # If the branch still exists then we will delete it - gh api /repos/flutter/devtools/git/refs/heads/$CLOSED_BRANCH -X DELETE - fi - done + diff --git a/.github/workflows/flutter-candidate-update.yaml b/.github/workflows/flutter-candidate-update.yaml index 98bec314955..756fa81abbf 100644 --- a/.github/workflows/flutter-candidate-update.yaml +++ b/.github/workflows/flutter-candidate-update.yaml @@ -14,6 +14,7 @@ env: jobs: update-candidate: + if: ${{ github.repository == 'flutter/devtools' }} name: Update Flutter Candidate Version runs-on: ubuntu-latest steps: diff --git a/.github/workflows/workflow-bot-cleanup.yaml b/.github/workflows/workflow-bot-cleanup.yaml new file mode 100644 index 00000000000..faf30fbf27e --- /dev/null +++ b/.github/workflows/workflow-bot-cleanup.yaml @@ -0,0 +1,53 @@ +name: Workflow Bot Cleanup +on: + workflow_dispatch: # Allows for manual triggering if needed + schedule: + # * is a special character in YAML so you have to quote this string + - cron: "0 9 * * *" # Run every day +permissions: + contents: write + pull-requests: write + + +jobs: + clean-up-branches: + if: ${{ github.repository == 'flutter/devtools' }} + name: Clean up closed DartDevtoolWorkflowBot Branches + runs-on: ubuntu-latest + steps: + - name: Sparse checkout of the repository + uses: actions/checkout@v3 + with: + sparse-checkout: | + README.md + - name: Clean up closed DartDevtoolWorkflowBot branches + run: | + set -e + + # Get list of branches that exist on the remote, then filter for the workflow bot branches + EXISTING_BRANCHES=$(git ls-remote --heads | grep refs/heads | awk '{print $2}' |sed 's|refs/heads/\(.*\)$|\1|') + for EXISTING_BRANCH in $EXISTING_BRANCHES; do + set +e # Turn off exit on error, since "gh pr view" may fail if branch doesn't exist + PR_INFO=$(gh pr view --json closed,author "$EXISTING_BRANCH") + if [[ $? -ne 0 ]]; then + # If getting PR_INFO fails assume the PR does not exist + echo "SKIP: No PR exists for $EXISTING_BRANCH" + continue + fi + set -e # Turn exit on error back on + + PR_IS_CLOSED=$(echo $PR_INFO | jq -r '.closed') + PR_AUTHOR=$(echo $PR_INFO | jq -r '.author.login') + if [[ "$PR_IS_CLOSED" == "true" ]] && [[ "$PR_AUTHOR" == "DartDevtoolWorkflowBot" ]]; then + # Delete branches where: + # - DartDevtoolWorkflowBot is the author + # - the PR is closed + echo "Deleting $EXISTING_BRANCH" + gh api /repos/flutter/devtools/git/refs/heads/$EXISTING_BRANCH -X DELETE + else + echo "SKIP: Avoiding $EXISTING_BRANCH { is_closed:$PR_IS_CLOSED, author:$PR_AUTHOR }" + fi + done + + env: + GH_TOKEN: ${{ secrets.DEVTOOLS_WORKFLOW_BOT_TOKEN }}