Skip to content

Commit aee82bd

Browse files
authored
[TT-1725] add go doc generating pipeline (#1447)
1 parent 1449c13 commit aee82bd

File tree

1 file changed

+134
-43
lines changed

1 file changed

+134
-43
lines changed

.github/workflows/generate-go-docs.yaml

Lines changed: 134 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,161 @@ on:
99

1010
jobs:
1111
generate_docs_new_pr:
12-
if: ${{ contains(github.event.issue.labels.*.name, 'generate_go_docs') }}
12+
if: ${{ contains(github.event.pull_request.labels.*.name, 'generate_go_docs') }}
1313
runs-on: ubuntu-latest
1414
permissions:
15-
contents: read
16-
pull-requests: write
17-
repository-projects: read
15+
id-token: write
16+
contents: read
1817

1918
steps:
19+
- name: Setup GitHub Token for reading Generate Go Doc Repo
20+
id: setup-github-token-read
21+
uses: smartcontractkit/.github/actions/setup-github-token@9e7cc0779934cae4a9028b8588c9adb64d8ce68c # [email protected]
22+
with:
23+
aws-role-arn: ${{ secrets.AWS_ROLE_ARN_READ_GENERATE_GO_DOC_REPO }}
24+
aws-lambda-url: ${{ secrets.GATI_LAMBDA_TT_URL }}
25+
aws-region: ${{ secrets.AWS_REGION }}
26+
27+
- name: Configure git for private repository and install go tools
28+
env:
29+
GOPRIVATE: github.com/smartcontractkit/generate-go-function-docs
30+
run: |
31+
git config --global url."https://x-access-token:${{ steps.setup-github-token-read.outputs.access-token }}@github.com/".insteadOf "https://github.com/"
32+
go install github.com/smartcontractkit/[email protected]
33+
go install github.com/jmank88/[email protected]
34+
go install golang.org/x/tools/gopls@latest
35+
2036
- name: Checkout current branch
2137
uses: actions/checkout@v3
2238
with:
2339
fetch-depth: 0
2440

25-
- name: Setup Git
26-
run: |
27-
git config user.name "github-actions[bot]"
28-
git config user.email "github-actions[bot]@users.noreply.github.com"
29-
30-
- name: Get PR details
31-
id: pr-details
32-
run: |
33-
echo "pr_number=$(jq -r .pull_request.number <<< "$GITHUB_EVENT_PATH")" >> $GITHUB_OUTPUT
34-
echo "pr_branch=$(jq -r .pull_request.head.ref <<< "$GITHUB_EVENT_PATH")" >> $GITHUB_OUTPUT
35-
echo "base_commit=${{ github.event.pull_request.base.sha }}" >> $GITHUB_OUTPUT
36-
echo "head_commit=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
41+
- name: Detect changes related to current PR
42+
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
43+
id: changes
44+
with:
45+
filters: |
46+
seth:
47+
- 'seth/**/*.go'
48+
wasp:
49+
- 'wasp/**/*.go'
50+
havoc:
51+
- 'havoc/**/*.go'
52+
lib:
53+
- 'lib/**/*.go'
54+
k8s-test-runner:
55+
- 'k8s-test-runner/**/*.go'
56+
framework:
57+
- 'framework/**/*.go'
58+
# later add tools here or, if possible, make this filter dynamic so that it's created based on the go modules in the repository
3759
38-
- name: Install go doc generator
60+
- name: Find all go modules in the repository and filter the ones that changed
3961
shell: bash
62+
id: go-modules
63+
env:
64+
FILTERS: ${{ steps.changes.outputs.changes }}
4065
run: |
41-
go install github.com/smartcontractkit/generate-go-function-docs@latest
66+
PATH=$PATH:$(go env GOPATH)/bin
67+
export PATH
4268
43-
- name: Install gopls
44-
shell: bash
45-
run: |
46-
go install golang.org/x/tools/gopls@latest
69+
# Find all go projects
70+
gomods_output=$(gomods 2>&1)
4771
48-
- name: Create a new branch
49-
run: |
50-
git checkout -b ${{ steps.pr-details.outputs.pr_branch }}-docs
72+
# Extract the parent directories of go.mod files
73+
parent_folders=$(echo "$gomods_output" | grep 'go\.mod$' | sed 's/\/go\.mod//' | sed 's/^[ \t]*//;s/[ \t]*$//' | xargs -n 1)
74+
75+
# Convert parent directories into a JSON matrix
76+
echo "$parent_folders" | jq -R -s 'split("\n") | map(select(length > 0)) | map({folder: .})' > all_folders.json
77+
78+
# Filter the directories that did not changeß
79+
jq --argjson filters "$FILTERS" 'map(select(.folder as $folder | $filters | index($folder)))' all_folders.json > filtered_folders.json
80+
81+
echo "Filtered folder List JSON"
82+
cat filtered_folders.json
5183
52-
- name: Generate go docs
84+
rm all_folders.json
85+
86+
- name: Generate go docs for changed projects
5387
shell: bash
5488
env:
5589
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
5690
run: |
57-
generate-go-function-docs diff -b ${{ steps.pr-details.outputs.base_commit }} -c ${{ steps.pr-details.outputs.head_commit }} -g fake_slash
91+
# Add go binary to PATH
92+
PATH=$PATH:$(go env GOPATH)/bin
93+
export PATH
94+
cat filtered_folders.json | jq -c '.[]' | while read -r item; do
95+
folder=$(echo "$item" | jq -r '.folder')
96+
echo "Processing folder: $folder"
97+
generate-go-function-docs diff -b ${{ github.event.pull_request.base.sha }} -c ${{ github.event.pull_request.head.sha }} --saveCosts --generator chatgpt --folder "$folder"
98+
cd "$folder"
99+
cd -
100+
done
101+
rm filtered_folders.json
58102
59-
- name: Make sure code still compiles
60-
run: |
61-
go mod tidy
62-
go mod vendor
63-
go build ./...
103+
- name: Upload costs as artifact
104+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
105+
with:
106+
name: generation-costs
107+
path: ./costs
64108

65-
- name: Commit changes
66-
run: |
67-
git add .
68-
git commit -m "Add automatically generated go documentation"
69-
git push origin ${{ steps.pr-details.outputs.pr_branch }}-docs
109+
- name: Remove costs before committing
110+
shell: bash
111+
run: rm -rf costs
112+
113+
- name: Setup GitHub Token for creating a new PR
114+
id: setup-github-token-write
115+
uses: smartcontractkit/.github/actions/setup-github-token@9e7cc0779934cae4a9028b8588c9adb64d8ce68c # [email protected]
116+
with:
117+
aws-role-arn: ${{ secrets.AWS_ROLE_ARN_CREATE_PR }}
118+
aws-lambda-url: ${{ secrets.GATI_LAMBDA_TT_URL }}
119+
aws-region: ${{ secrets.AWS_REGION }}
70120

71121
- name: Create a new PR targeting current PR
72-
uses: peter-evans/create-pull-request@v5
122+
uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5
123+
with:
124+
token: ${{ steps.setup-github-token-write.outputs.access-token }}
125+
branch: ${{ github.head_ref }}-docs
126+
base: ${{ github.head_ref }}
127+
title: "Go docs for PR#${{ github.event.pull_request.number }}"
128+
body: "This PR contains automatically generated go documentation for the PR#${{ github.event.pull_request.number }}. Please review the changes."
129+
commit-message: "[Bot] Add automatically generated go documentation"
130+
131+
- name: Send Slack notification
132+
uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001 # v1.25.0
133+
if: failure()
134+
id: slack
135+
env:
136+
SLACK_BOT_TOKEN: ${{ secrets.QA_SLACK_API_KEY }}
73137
with:
74-
token: ${{ secrets.GITHUB_TOKEN }}
75-
base: ${{ env.pr_branch }}
76-
branch: ${{ steps.pr-details.outputs.pr_branch }}-docs
77-
title: "Go docs for PR#${{ env.pr_number }}"
78-
body: "This PR contains automatically generated go documentation for the PR#${{ env.pr_number }}. Please review the changes."
138+
channel-id: 'C049X3353K2'
139+
payload: |
140+
{
141+
"attachments": [
142+
{
143+
"color": "#C62828",
144+
"blocks": [
145+
{
146+
"type": "section",
147+
"text": {
148+
"type": "mrkdwn",
149+
"text": "Go doc generation - Failed :x:"
150+
}
151+
},
152+
{
153+
"type": "section",
154+
"text": {
155+
"type": "mrkdwn",
156+
"text": "<@U060CGGPY8H>, please have a look."
157+
}
158+
},
159+
{
160+
"type": "section",
161+
"text": {
162+
"type": "mrkdwn",
163+
"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>"
164+
}
165+
}
166+
]
167+
}
168+
]
169+
}

0 commit comments

Comments
 (0)