From 8e0047b62c5eeb865a73d25d5a85c7106e53cf9a Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Mon, 20 Nov 2023 13:57:35 +1100 Subject: [PATCH 1/5] Add rake task for generating GraphQL docs Keep GraphQL tasks co-located. --- .buildkite/update_graphql_docs | 2 +- README.md | 2 +- lib/tasks/graphql.rake | 5 +++++ scripts/generate-graphql-api-content.sh | 5 ----- 4 files changed, 7 insertions(+), 7 deletions(-) delete mode 100755 scripts/generate-graphql-api-content.sh diff --git a/.buildkite/update_graphql_docs b/.buildkite/update_graphql_docs index 29b659c926..d65c51d774 100755 --- a/.buildkite/update_graphql_docs +++ b/.buildkite/update_graphql_docs @@ -26,7 +26,7 @@ if [ -n "$PULL_REQUEST_NUMBER" ]; then fi echo "+++ Generate GraphQL docs" -./scripts/generate-graphql-api-content.sh +rake graphql:generate echo "--- Commit and push changes" git add . diff --git a/README.md b/README.md index 90bfbb386f..3afb8e8ed1 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ If you need to fetch the latest schema you can either: API_ACCESS_TOKEN=xxx rake graphql:fetch_schema >| data/graphql/schema.graphql # Generate docs based on latest schema -./scripts/generate-graphql-api-content.sh +rake graphql:generate ``` diff --git a/lib/tasks/graphql.rake b/lib/tasks/graphql.rake index 89e81d109a..76309e9e4c 100644 --- a/lib/tasks/graphql.rake +++ b/lib/tasks/graphql.rake @@ -16,4 +16,9 @@ namespace :graphql do puts GraphQL::Client.load_schema(HTTP).to_definition end + + desc "Generate GraphQL docs and navigation from GraphQL schema" + task :generate do + ruby "scripts/generate_graphql_api_content.rb" + end end diff --git a/scripts/generate-graphql-api-content.sh b/scripts/generate-graphql-api-content.sh deleted file mode 100755 index 355b8523bd..0000000000 --- a/scripts/generate-graphql-api-content.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -scripts_dir="$(dirname "${BASH_SOURCE[0]}")" - -rm -rf ${scripts_dir}/../pages/apis/graphql/schemas/* -ruby "${scripts_dir}/generate_graphql_api_content.rb" From 509f011c2700159bd60b2be921c568d535cd791e Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Mon, 20 Nov 2023 14:00:00 +1100 Subject: [PATCH 2/5] Run GraphQL CI step outside of docker Running each GraphQL sub-command in a separate docker container means we don't need to have git installed in the app docker image. --- .buildkite/pipeline.graphql.yml | 10 ---------- .buildkite/update_graphql_docs | 10 +++++++--- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/.buildkite/pipeline.graphql.yml b/.buildkite/pipeline.graphql.yml index 1d34fd45ae..d20355fff8 100644 --- a/.buildkite/pipeline.graphql.yml +++ b/.buildkite/pipeline.graphql.yml @@ -1,13 +1,3 @@ steps: - label: Update GraphQL docs command: .buildkite/update_graphql_docs - plugins: - - docker-compose#v4.9.0: - run: app - mount-ssh-agent: true - mount-buildkite-agent: true - env: - - API_ACCESS_TOKEN - - GIT_NAME - - GIT_EMAIL - - GH_TOKEN diff --git a/.buildkite/update_graphql_docs b/.buildkite/update_graphql_docs index d65c51d774..550830eb01 100755 --- a/.buildkite/update_graphql_docs +++ b/.buildkite/update_graphql_docs @@ -4,7 +4,11 @@ set -euo pipefail . bin/utils.sh echo "+++ Fetch latest schema (via GraphQL introspection)" -rake graphql:fetch_schema >| data/graphql/schema.graphql +docker compose run \ + --no-deps \ + -e API_ACCESS_TOKEN \ + app \ + bash -c "rake graphql:fetch_schema >| data/graphql/schema.graphql" echo "+++ Check for schema changes" if git diff --exit-code --quiet data/graphql/schema.graphql; then @@ -21,12 +25,12 @@ BRANCH=buildkite-docs-bot/graphql/$(git rev-parse --short :data/graphql/schema.g PULL_REQUEST_NUMBER=$(get_branch_pull_request_number $BRANCH) if [ -n "$PULL_REQUEST_NUMBER" ]; then - buildkite-agent annotate "Nothing to change, pull request [#$PULL_REQUEST_NUMBER](https://github.com/buildkite/docs/pull/$PULL_REQUEST_NUMBER) awaiting approval." --style "info" + buildkite-agent annotate "Nothing to change, pull request [#$PULL_REQUEST_NUMBER](https://github.com/buildkite/docs/pull/$PULL_REQUEST_NUMBER) awaiting approval." --style "info" exit 0 fi echo "+++ Generate GraphQL docs" -rake graphql:generate +docker compose run --no-deps app rake graphql:generate echo "--- Commit and push changes" git add . From cb9bcb2171c9da285ff25d7f134555cad6abd041 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Mon, 20 Nov 2023 14:02:23 +1100 Subject: [PATCH 3/5] Replace `File.exists?` with `File.exist?` `File.exists?` was removed in Ruby 3.2 --- scripts/generate_graphql_api_content.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/generate_graphql_api_content.rb b/scripts/generate_graphql_api_content.rb index 6d65611959..3162ae7c89 100644 --- a/scripts/generate_graphql_api_content.rb +++ b/scripts/generate_graphql_api_content.rb @@ -33,7 +33,7 @@ schema_type_data["kind"].to_s.downcase end - Dir.mkdir("#{schemas_dir}/#{sub_dir}") unless File.exists?("#{schemas_dir}/#{sub_dir}") + Dir.mkdir("#{schemas_dir}/#{sub_dir}") unless File.exist?("#{schemas_dir}/#{sub_dir}") File.write("#{schemas_dir}/#{sub_dir}/#{name.downcase}.md", render_page(schema_type_data, sub_dir.capitalize.pluralize)) end end From 7d54a33ca69035cb8b5e7502b6f6db4a7b84e220 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Mon, 20 Nov 2023 15:40:45 +1100 Subject: [PATCH 4/5] Remove auto-merge --- .buildkite/update_graphql_docs | 3 --- 1 file changed, 3 deletions(-) diff --git a/.buildkite/update_graphql_docs b/.buildkite/update_graphql_docs index 550830eb01..e4688ba6b9 100755 --- a/.buildkite/update_graphql_docs +++ b/.buildkite/update_graphql_docs @@ -45,6 +45,3 @@ gh pr create \ --title "Update GraphQL docs" \ --body "This is an automated PR based on the current GraphQL schema" \ | buildkite-agent annotate --style "success" - -# Auto-merge PR once checks have passed -gh pr merge -m --auto From d1a7f6ae72916dfe082e2dd6e844bbc879614411 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Mon, 20 Nov 2023 15:50:31 +1100 Subject: [PATCH 5/5] Replace gh cli command with curl --- .buildkite/update_graphql_docs | 6 ++---- bin/utils.sh | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.buildkite/update_graphql_docs b/.buildkite/update_graphql_docs index e4688ba6b9..a1314fff7d 100755 --- a/.buildkite/update_graphql_docs +++ b/.buildkite/update_graphql_docs @@ -41,7 +41,5 @@ git commit -m "Update GraphQL docs" git push -u origin $BRANCH echo "+++ Create pull request" -gh pr create \ - --title "Update GraphQL docs" \ - --body "This is an automated PR based on the current GraphQL schema" \ - | buildkite-agent annotate --style "success" +create_pull_request "Update GraphQL docs" "This is an automated PR based on the current GraphQL schema" \ + | jq ".url" | buildkite-agent annotate --style "success" diff --git a/bin/utils.sh b/bin/utils.sh index 8ff1c858b1..22753756c9 100644 --- a/bin/utils.sh +++ b/bin/utils.sh @@ -33,3 +33,13 @@ function post_github_comment() { --data "{\"body\":\"$2\"}" \ https://api.github.com/repos/buildkite/docs/issues/$1/comments } + +function create_pull_request() { + curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GH_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + --data "{\"title\":\"$1\", "body\":\"$2\"}" \ + https://api.github.com/repos/buildkite/docs/pulls" +}