|
| 1 | +--- |
| 2 | +name: Run TeamCity Tests on Comment |
| 3 | + |
| 4 | +on: |
| 5 | + issue_comment: |
| 6 | + types: [created] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-team-membership: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + # Only run on pull request comments that starts with /test |
| 17 | + if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/test') |
| 18 | + outputs: |
| 19 | + is-team-member: ${{ steps.check-membership.outputs.is-member }} |
| 20 | + env: |
| 21 | + AUTHORIZED_TEAM: terraform-azure |
| 22 | + steps: |
| 23 | + - name: Check team membership |
| 24 | + id: check-membership |
| 25 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 26 | + with: |
| 27 | + github-token: ${{ secrets.GH_MEMEBERSHIP_CHECK_TOKEN }} |
| 28 | + script: | |
| 29 | + const teamSlug = "${{ env.AUTHORIZED_TEAM }}"; |
| 30 | + const org = context.repo.owner; |
| 31 | + const username = context.actor; |
| 32 | +
|
| 33 | + try { |
| 34 | + const response = await github.rest.teams.getMembershipForUserInOrg({ |
| 35 | + org: org, |
| 36 | + team_slug: teamSlug, |
| 37 | + username: username, |
| 38 | + }); |
| 39 | +
|
| 40 | + const isMember = response.data.state === 'active'; |
| 41 | + core.setOutput('is-member', isMember); |
| 42 | +
|
| 43 | + if (isMember) { |
| 44 | + core.info(`User ${username} is a member of team ${teamSlug}`); |
| 45 | + } else { |
| 46 | + core.warning(`User ${username} is not an active member of team ${teamSlug}`); |
| 47 | + } |
| 48 | +
|
| 49 | + return isMember; |
| 50 | + } catch (error) { |
| 51 | + if (error.status === 404) { |
| 52 | + core.warning(`User ${username} is not a member of team ${teamSlug}`); |
| 53 | + core.setOutput('is-member', false); |
| 54 | + return false; |
| 55 | + } |
| 56 | + throw error; |
| 57 | + } |
| 58 | +
|
| 59 | + run-tests: |
| 60 | + runs-on: ubuntu-latest |
| 61 | + needs: check-team-membership |
| 62 | + if: needs.check-team-membership.outputs.is-team-member == 'true' |
| 63 | + env: |
| 64 | + TCTEST_SERVER: ${{ secrets.TCTEST_SERVER }} |
| 65 | + TCTEST_FILEREGEX: 'internal/services/[a-z]*/[_a-zA-Z]*(resource|data_source)' |
| 66 | + TCTEST_SKIP_QUEUE: 'true' |
| 67 | + TCTEST_TOKEN_TC: ${{ secrets.TCTEST_TOKEN_TC }} |
| 68 | + TCTEST_BUILDTYPEID: TF_AzureRM_AZURERM_SERVICE_PUBLIC |
| 69 | + TCTEST_REPO: terraform-providers/terraform-provider-azurerm |
| 70 | + steps: |
| 71 | + - name: Setup Go |
| 72 | + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.2.0 |
| 73 | + with: |
| 74 | + go-version: '1.25' |
| 75 | + |
| 76 | + - name: Get tctest version |
| 77 | + id: tctest-version |
| 78 | + run: | |
| 79 | + LATEST_RELEASE=$(curl -s https://api.github.com/repos/katbyte/tctest/releases/latest | grep "tag_name" | cut -d '"' -f 4) |
| 80 | + echo "version=${LATEST_RELEASE}" >> $GITHUB_OUTPUT |
| 81 | + echo "Latest tctest release: $LATEST_RELEASE" |
| 82 | +
|
| 83 | + - name: Cache tctest binary |
| 84 | + id: cache-tctest |
| 85 | + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 |
| 86 | + with: |
| 87 | + path: ~/go/bin/tctest |
| 88 | + key: tctest-${{ runner.os }}-${{ steps.tctest-version.outputs.version }} |
| 89 | + |
| 90 | + - name: Install tctest |
| 91 | + if: steps.cache-tctest.outputs.cache-hit != 'true' |
| 92 | + run: | |
| 93 | + go install github.com/katbyte/tctest@latest |
| 94 | +
|
| 95 | + - name: Run tctest |
| 96 | + run: | |
| 97 | + PR_NUMBER=${{ github.event.issue.number }} |
| 98 | + COMMENT_BODY="${{ github.event.comment.body }}" |
| 99 | + echo "Running tctest for PR #${PR_NUMBER}" |
| 100 | + tctest pr ${PR_NUMBER} --properties "POST_GITHUB_COMMENT=true" |
| 101 | +
|
| 102 | + - name: Comment on success |
| 103 | + if: success() |
| 104 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 105 | + with: |
| 106 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + script: | |
| 108 | + await github.rest.reactions.createForIssueComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + comment_id: context.payload.comment.id, |
| 112 | + content: 'rocket' |
| 113 | + }); |
| 114 | +
|
| 115 | + - name: Comment on failure |
| 116 | + if: failure() |
| 117 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 118 | + with: |
| 119 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 120 | + script: | |
| 121 | + await github.rest.issues.createComment({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + issue_number: context.issue.number, |
| 125 | + body: '❌ Failed to trigger TeamCity tests. Please check the workflow logs for details.' |
| 126 | + }); |
| 127 | +
|
| 128 | + unauthorized-comment: |
| 129 | + runs-on: ubuntu-latest |
| 130 | + needs: check-team-membership |
| 131 | + if: needs.check-team-membership.outputs.is-team-member == 'false' |
| 132 | + steps: |
| 133 | + - name: Comment unauthorized |
| 134 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 135 | + with: |
| 136 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + script: | |
| 138 | + await github.rest.issues.createComment({ |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + issue_number: context.issue.number, |
| 142 | + body: '⚠️ You are not authorized to run tests. Only members of the designated team can trigger test runs.' |
| 143 | + }); |
0 commit comments