From aee82bd22a7574605c983f5eac6e6a50eb7d93b6 Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Mon, 9 Dec 2024 11:21:12 +0100 Subject: [PATCH] [TT-1725] add go doc generating pipeline (#1447) --- .github/workflows/generate-go-docs.yaml | 177 ++++++++++++++++++------ 1 file changed, 134 insertions(+), 43 deletions(-) diff --git a/.github/workflows/generate-go-docs.yaml b/.github/workflows/generate-go-docs.yaml index 3ab2cd056..f70eb0fac 100644 --- a/.github/workflows/generate-go-docs.yaml +++ b/.github/workflows/generate-go-docs.yaml @@ -9,70 +9,161 @@ on: jobs: generate_docs_new_pr: - if: ${{ contains(github.event.issue.labels.*.name, 'generate_go_docs') }} + if: ${{ contains(github.event.pull_request.labels.*.name, 'generate_go_docs') }} runs-on: ubuntu-latest permissions: - contents: read - pull-requests: write - repository-projects: read + id-token: write + contents: read steps: + - name: Setup GitHub Token for reading Generate Go Doc Repo + id: setup-github-token-read + uses: smartcontractkit/.github/actions/setup-github-token@9e7cc0779934cae4a9028b8588c9adb64d8ce68c # setup-github-token@0.1.2 + with: + aws-role-arn: ${{ secrets.AWS_ROLE_ARN_READ_GENERATE_GO_DOC_REPO }} + aws-lambda-url: ${{ secrets.GATI_LAMBDA_TT_URL }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Configure git for private repository and install go tools + env: + GOPRIVATE: github.com/smartcontractkit/generate-go-function-docs + run: | + git config --global url."https://x-access-token:${{ steps.setup-github-token-read.outputs.access-token }}@github.com/".insteadOf "https://github.com/" + go install github.com/smartcontractkit/generate-go-function-docs@v0.0.1 + go install github.com/jmank88/gomods@v0.1.3 + go install golang.org/x/tools/gopls@latest + - name: Checkout current branch uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup Git - run: | - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - - - name: Get PR details - id: pr-details - run: | - echo "pr_number=$(jq -r .pull_request.number <<< "$GITHUB_EVENT_PATH")" >> $GITHUB_OUTPUT - echo "pr_branch=$(jq -r .pull_request.head.ref <<< "$GITHUB_EVENT_PATH")" >> $GITHUB_OUTPUT - echo "base_commit=${{ github.event.pull_request.base.sha }}" >> $GITHUB_OUTPUT - echo "head_commit=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT + - name: Detect changes related to current PR + uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2 + id: changes + with: + filters: | + seth: + - 'seth/**/*.go' + wasp: + - 'wasp/**/*.go' + havoc: + - 'havoc/**/*.go' + lib: + - 'lib/**/*.go' + k8s-test-runner: + - 'k8s-test-runner/**/*.go' + framework: + - 'framework/**/*.go' + # later add tools here or, if possible, make this filter dynamic so that it's created based on the go modules in the repository - - name: Install go doc generator + - name: Find all go modules in the repository and filter the ones that changed shell: bash + id: go-modules + env: + FILTERS: ${{ steps.changes.outputs.changes }} run: | - go install github.com/smartcontractkit/generate-go-function-docs@latest + PATH=$PATH:$(go env GOPATH)/bin + export PATH - - name: Install gopls - shell: bash - run: | - go install golang.org/x/tools/gopls@latest + # Find all go projects + gomods_output=$(gomods 2>&1) - - name: Create a new branch - run: | - git checkout -b ${{ steps.pr-details.outputs.pr_branch }}-docs + # Extract the parent directories of go.mod files + parent_folders=$(echo "$gomods_output" | grep 'go\.mod$' | sed 's/\/go\.mod//' | sed 's/^[ \t]*//;s/[ \t]*$//' | xargs -n 1) + + # Convert parent directories into a JSON matrix + echo "$parent_folders" | jq -R -s 'split("\n") | map(select(length > 0)) | map({folder: .})' > all_folders.json + + # Filter the directories that did not changeß + jq --argjson filters "$FILTERS" 'map(select(.folder as $folder | $filters | index($folder)))' all_folders.json > filtered_folders.json + + echo "Filtered folder List JSON" + cat filtered_folders.json - - name: Generate go docs + rm all_folders.json + + - name: Generate go docs for changed projects shell: bash env: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} run: | - generate-go-function-docs diff -b ${{ steps.pr-details.outputs.base_commit }} -c ${{ steps.pr-details.outputs.head_commit }} -g fake_slash + # Add go binary to PATH + PATH=$PATH:$(go env GOPATH)/bin + export PATH + cat filtered_folders.json | jq -c '.[]' | while read -r item; do + folder=$(echo "$item" | jq -r '.folder') + echo "Processing folder: $folder" + generate-go-function-docs diff -b ${{ github.event.pull_request.base.sha }} -c ${{ github.event.pull_request.head.sha }} --saveCosts --generator chatgpt --folder "$folder" + cd "$folder" + cd - + done + rm filtered_folders.json - - name: Make sure code still compiles - run: | - go mod tidy - go mod vendor - go build ./... + - name: Upload costs as artifact + uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 + with: + name: generation-costs + path: ./costs - - name: Commit changes - run: | - git add . - git commit -m "Add automatically generated go documentation" - git push origin ${{ steps.pr-details.outputs.pr_branch }}-docs + - name: Remove costs before committing + shell: bash + run: rm -rf costs + + - name: Setup GitHub Token for creating a new PR + id: setup-github-token-write + uses: smartcontractkit/.github/actions/setup-github-token@9e7cc0779934cae4a9028b8588c9adb64d8ce68c # setup-github-token@0.1.2 + with: + aws-role-arn: ${{ secrets.AWS_ROLE_ARN_CREATE_PR }} + aws-lambda-url: ${{ secrets.GATI_LAMBDA_TT_URL }} + aws-region: ${{ secrets.AWS_REGION }} - name: Create a new PR targeting current PR - uses: peter-evans/create-pull-request@v5 + uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5 + with: + token: ${{ steps.setup-github-token-write.outputs.access-token }} + branch: ${{ github.head_ref }}-docs + base: ${{ github.head_ref }} + title: "Go docs for PR#${{ github.event.pull_request.number }}" + body: "This PR contains automatically generated go documentation for the PR#${{ github.event.pull_request.number }}. Please review the changes." + commit-message: "[Bot] Add automatically generated go documentation" + + - name: Send Slack notification + uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001 # v1.25.0 + if: failure() + id: slack + env: + SLACK_BOT_TOKEN: ${{ secrets.QA_SLACK_API_KEY }} with: - token: ${{ secrets.GITHUB_TOKEN }} - base: ${{ env.pr_branch }} - branch: ${{ steps.pr-details.outputs.pr_branch }}-docs - title: "Go docs for PR#${{ env.pr_number }}" - body: "This PR contains automatically generated go documentation for the PR#${{ env.pr_number }}. Please review the changes." + channel-id: 'C049X3353K2' + payload: | + { + "attachments": [ + { + "color": "#C62828", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "Go doc generation - Failed :x:" + } + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "<@U060CGGPY8H>, please have a look." + } + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "<${{ github.server_url }}/${{ github.repository }}/pull/${{ github.event.pull_request.number }}|PR#${{ github.event.pull_request.number }}> | <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Run Details>" + } + } + ] + } + ] + } \ No newline at end of file