Skip to content

Commit

Permalink
init implementation, adds rover subgraph check command (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Golang Lemonade <147884153+golanglemonade@users.noreply.github.com>
  • Loading branch information
golanglemonade committed Mar 1, 2024
1 parent 4d27929 commit a8b4732
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Expand Up @@ -15,4 +15,4 @@ steps:
- label: ":sparkles: Lint"
plugins:
plugin-linter#v3.3.0:
id: a-github-user/template
id: datumforge/apollo
37 changes: 23 additions & 14 deletions README.md
@@ -1,28 +1,29 @@
[![Build status](https://badge.buildkite.com/df549c652f77fa3660f4d7f975fd3bb5744e12fb4066719553.svg?branch=main)](https://buildkite.com/datum/template-buildkite-plugin)
[![Build status](https://badge.buildkite.com/0e16a8e8b81104b7059cafc364375695c55427ae8328a68b5f.svg)](https://buildkite.com/datum/apollo-buildkite-plugin)

# Template Buildkite Plugin
# Apollo Buildkite Plugin

Check the [buildkite organization](https://github.com/buildkite-plugins) or [website](https://buildkite.com/plugins) to see if your plugin already exists or we can contribute to it !

Be sure to update this readme with your plugin information after using the template repository - for more info checkout Buildkite's documentation [here](https://buildkite.com/docs/plugins)
Buildkite plugin that will check apollo schemas. The current implementation only runs the `rover subgraph check` command. Future releases will add the ability to run the `graph` and `supergraph` commands

## Example

Provide an example of using this plugin, like so:

Add the following to your `pipeline.yml`:

```yml
steps:
- command: ls
plugins:
- a-github-user/template#v1.0.0:
pattern: '*.md'
- plugins:
- datumforge/apollo#v0.0.1:
graph-ref: datum@current
subgraph: datum
schema: schema.graphql
```

## Developing
To ignore existing lint violations set `ignore-existing: true` in the plugin definition

Provide examples on how to modify and test, e.g.:
## Environment Variables

1. `APOLLO_KEY` is required to be set in the environment before the plugin can run

## Developing

To run the linter:
```shell
Expand All @@ -33,4 +34,12 @@ To run the tests:

```shell
task test
```
```

## Contributing

1. Fork the repo
2. Make the changes
3. Run the tests
4. Commit and push your changes
5. Send a pull request
2 changes: 1 addition & 1 deletion Taskfile.yaml
Expand Up @@ -26,4 +26,4 @@ tasks:
cmds:
- task: shellcheck
- task: lint
- task: test
- task: test
3 changes: 1 addition & 2 deletions docker-compose.yml
@@ -1,8 +1,7 @@
services:
# You need to update this for your plugin name / location
lint:
image: buildkite/plugin-linter
command: [ '--id', 'a-github-user/template' ]
command: [ '--id', 'datumforge/apollo' ]
volumes:
- ".:/plugin:ro"
tests:
Expand Down
81 changes: 81 additions & 0 deletions hooks/command
@@ -0,0 +1,81 @@
#!/bin/bash

set -o nounset # script exits when tries to use undeclared variables == set -u
set -o pipefail # causes pipelines to retain / set the last non-zero status

# set path
export PATH="$PATH:/var/lib/buildkite-agent/.rover/bin"

graph_ref=${BUILDKITE_PLUGIN_APOLLO_GRAPH_REF}
subgraph=${BUILDKITE_PLUGIN_APOLLO_SUBGRAPH}
schema=${BUILDKITE_PLUGIN_APOLLO_SCHEMA:-"schema.graphql"}
ignore_existing=${BUILDKITE_PLUGIN_APOLLO_IGNORE_EXISTING:-false}

debug=${BUILDKITE_PLUGIN_APOLLO_DEBUG:-false}

if [[ "$debug" == "true" ]]; then
set -o xtrace # trace what's executed == set -x (useful for debugging)
fi

function main() {
echo +++ :graphql: schema check

# Run the check command with the rover client
# Example: rover subgraph check golanglemonades-Team@current --name datum --schema schema.graphql
append_command=""
if [[ "$ignore_existing" == "true" ]]; then
append_command="--ignore-existing-lint-violations"
fi

check_result=$(rover subgraph check "${graph_ref}" --name "${subgraph}" --schema "${schema}" ${append_command} --format=json)

success=$(echo "$check_result" | jq -r '.data.success')
errors=$(echo "$check_result" | jq -r '.error')

# Parse the results to display on the top of the buildkite job
lint_result="$check_result" parse_lint_result

# Start the annotation
echo -e "\`apollo schema check\` results: " | buildkite-agent annotate --context apollo

# Check for error results, warning results, otherwise it was successful
if [[ "$error_results" != "[]" && "$error_results" != "null" ]]; then
echo -e ":graphql: errors found\n" | buildkite-agent annotate --context "apollo" --style "error" --append
echo -e "**Errors**:\n" | buildkite-agent annotate --context "apollo" --append
echo -e "$(echo "$error_results" |jq -r '"Level | Field | Line | Message", "---| ---| --- |----", (.[] | "\(.level) | \(.coordinate) | \(.start_line)| \(.message)" )')\n\n" | buildkite-agent annotate --context "apollo" --append
if [[ "$warning_results" != ""&& "$warning_results" != "null" ]]; then
echo -e "**Warnings**:\n" | buildkite-agent annotate --context "apollo" --append
echo -e "$(echo "$warning_results" |jq -r '"Level | Field | Line | Message", "---| ---| --- |----", (.[] | "\(.level) | \(.coordinate) | \(.start_line)| \(.message)" )')\n\n" | buildkite-agent annotate --context "apollo" --append
fi
elif [[ "$warning_results" != "[]" && "$warning_results" != "null" ]]; then
# do not error on warnings
echo -e ":graphql: warnings found\n" | buildkite-agent annotate --context "apollo" --style "warning" --append
echo -e "**Warnings**:\n" | buildkite-agent annotate --context "apollo" --append
echo -e "$(echo "$warning_results" |jq -r '"Level | Field | Line | Message", "---| ---| --- |----", (.[] | "\(.level) | \(.coordinate) | \(.start_line)| \(.message)" )')\n\n" | buildkite-agent annotate --context "apollo" --append
else
echo -e ":graphql: no errors found, congrats!\n"| buildkite-agent annotate --context "apollo" --style "success" --append
fi

if [ "${success}" == "false" ]; then
echo "Error: ${errors}"
exit 1
fi

exit 0
}

function parse_lint_result () {
warning_results=$(echo "$check_result" | jq '.data.tasks.lint.diagnostics
| map(select(.level == "WARNING" ))')

error_results=$(echo "$check_result" | jq '.data.tasks.lint.diagnostics
| map(select(.level == "ERROR" ))')


# Display these in the logs
echo "$warning_results" |jq
echo "$error_results" |jq
}


main
3 changes: 0 additions & 3 deletions hooks/post-command

This file was deleted.

10 changes: 8 additions & 2 deletions hooks/post-checkout → hooks/pre-checkout 100644 → 100755
Expand Up @@ -2,5 +2,11 @@

set -o errexit # script exits when a command fails == set -e
set -o nounset # script exits when tries to use undeclared variables == set -u
set -o xtrace # trace what's executed == set -x (useful for debugging)
set -o pipefail # causes pipelines to retain / set the last non-zero status
set -o pipefail # causes pipelines to retain / set the last non-zero status

# Install rover, if not available
which rover|| (curl -sSL https://rover.apollo.dev/nix/latest | sh -s -- --force)

export PATH="$PATH:/var/lib/buildkite-agent/.rover/bin"

exit 0
25 changes: 17 additions & 8 deletions plugin.yml
@@ -1,14 +1,23 @@
---
name: template-buildkite-plugin
description: what your plugin does
name: Apollo
description: Apollo schema checks with rover
author: Datum
requirements:
- if you have requirements
- bash
- curl
configuration:
properties:
propertyname:
graph-ref: # The name of the apollo graph to check
type: string
required: []
# - if any properties are required
dependencies:
dependencyname: [dep]
subgraph: # The name of the subgraph to check
type: string
schema: # The schema to check, defaults to schema.graphql
type: string
ignore-existing: # Ignore existing lint errors and only return new errors
type: boolean
default: false
required:
- graph-ref
- subgraph
- schema
additionalProperties: false
9 changes: 5 additions & 4 deletions renovate.json
@@ -1,5 +1,6 @@
{
"extends": [
"config:base"
]
}
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
}
60 changes: 60 additions & 0 deletions tests/command.bats
@@ -0,0 +1,60 @@
#!/usr/bin/env bats

load "$BATS_PLUGIN_PATH/load.bash"

# Uncomment the following line to debug stub failures
# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty
# export WHICH_STUB_DEBUG=/dev/tty

@test "default step" {
export BUILDKITE_PIPELINE_DEFAULT_BRANCH="main"
export BUILDKITE_PLUGIN_APOLLO_GRAPH_REF="pets@current"
export BUILDKITE_PLUGIN_APOLLO_SUBGRAPH="cats"

stub rover \
'subgraph check $BUILDKITE_PLUGIN_APOLLO_GRAPH_REF --name BUILDKITE_PLUGIN_APOLLO_SUBGRAPH --schema "schema.graphql" --format=json'\

run "$PWD/hooks/command"

assert_success
assert_output --partial "+++ :graphql: schema check"
assert_output --partial "check"

unstub rover
}

@test "default step with schema" {
export BUILDKITE_PIPELINE_DEFAULT_BRANCH="main"
export BUILDKITE_PLUGIN_APOLLO_GRAPH_REF="pets@current"
export BUILDKITE_PLUGIN_APOLLO_SUBGRAPH="cats"
export BUILDKITE_PLUGIN_APOLLO_SCHEMA="graphql/schema.graphql"

stub rover \
'subgraph check $BUILDKITE_PLUGIN_APOLLO_GRAPH_REF --name BUILDKITE_PLUGIN_APOLLO_SUBGRAPH --schema $BUILDKITE_PLUGIN_APOLLO_SCHEMA --format=json'\

run "$PWD/hooks/command"

assert_success
assert_output --partial "+++ :graphql: schema check"
assert_output --partial "check"

unstub rover
}

@test "ignore existing" {
export BUILDKITE_PIPELINE_DEFAULT_BRANCH="main"
export BUILDKITE_PLUGIN_APOLLO_GRAPH_REF="pets@current"
export BUILDKITE_PLUGIN_APOLLO_SUBGRAPH="cats"
export BUILDKITE_PLUGIN_APOLLO_IGNORE_EXISTING="true"

stub rover \
'subgraph check $BUILDKITE_PLUGIN_APOLLO_GRAPH_REF --name BUILDKITE_PLUGIN_APOLLO_SUBGRAPH --schema "schema.graphql" --ignore-existing-lint-violations --format=json'\

run "$PWD/hooks/command"

assert_success
assert_output --partial "+++ :graphql: schema check"
assert_output --partial "check"

unstub rover
}
3 changes: 0 additions & 3 deletions tests/post-checkout.bats

This file was deleted.

0 comments on commit a8b4732

Please sign in to comment.