|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +LINEAR_REQUEST="$SCRIPT_DIR/linear-graphql-request.sh" |
| 6 | +FILTER="$SCRIPT_DIR/filter-unlinked-cve-issues.jq" |
| 7 | + |
| 8 | +if [[ -z "${LINEAR_API_KEY:-}" ]]; then |
| 9 | + echo "LINEAR_API_KEY is required" >&2 |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [[ -z "${LINEAR_TEAM_ID:-}" ]]; then |
| 14 | + echo "LINEAR_TEAM_ID is required" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [[ -z "${REPOSITORY:-}" ]]; then |
| 19 | + echo "REPOSITORY is required" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +TEAM_QUERY='query ResolveTeam($teamId: String!) { |
| 24 | + team(id: $teamId) { id } |
| 25 | +}' |
| 26 | +team_payload=$(jq -n \ |
| 27 | + --arg query "$TEAM_QUERY" \ |
| 28 | + --arg teamId "$LINEAR_TEAM_ID" \ |
| 29 | + '{query: $query, variables: {teamId: $teamId}}') |
| 30 | +team_response=$(LINEAR_API_KEY="$LINEAR_API_KEY" "$LINEAR_REQUEST" <<<"$team_payload") |
| 31 | + |
| 32 | +if jq -e 'has("errors") or (.data.team.id == null)' >/dev/null <<<"$team_response"; then |
| 33 | + echo "Could not resolve LINEAR_TEAM_ID: $(jq -c '.errors // .' <<<"$team_response")" >&2 |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +team_uuid=$(jq -r '.data.team.id' <<<"$team_response") |
| 38 | + |
| 39 | +QUERY='query OpenRepositoryCves($teamId: ID!, $titlePrefix: String!, $after: String) { |
| 40 | + issues( |
| 41 | + first: 100 |
| 42 | + after: $after |
| 43 | + filter: { |
| 44 | + team: { id: { eq: $teamId } } |
| 45 | + title: { startsWith: $titlePrefix } |
| 46 | + state: { type: { nin: ["completed", "canceled", "duplicate"] } } |
| 47 | + } |
| 48 | + ) { |
| 49 | + nodes { |
| 50 | + id |
| 51 | + identifier |
| 52 | + title |
| 53 | + url |
| 54 | + priority |
| 55 | + state { name type } |
| 56 | + labels { nodes { name } } |
| 57 | + attachments { nodes { id title url } } |
| 58 | + } |
| 59 | + pageInfo { hasNextPage endCursor } |
| 60 | + } |
| 61 | +}' |
| 62 | + |
| 63 | +title_prefix="[$REPOSITORY]" |
| 64 | +after="" |
| 65 | +all_issues='[]' |
| 66 | + |
| 67 | +while true; do |
| 68 | + variables=$(jq -n \ |
| 69 | + --arg teamId "$team_uuid" \ |
| 70 | + --arg titlePrefix "$title_prefix" \ |
| 71 | + --arg after "$after" \ |
| 72 | + '{ |
| 73 | + teamId: $teamId, |
| 74 | + titlePrefix: $titlePrefix, |
| 75 | + after: (if $after == "" then null else $after end) |
| 76 | + }') |
| 77 | + payload=$(jq -n \ |
| 78 | + --arg query "$QUERY" \ |
| 79 | + --argjson variables "$variables" \ |
| 80 | + '{query: $query, variables: $variables}') |
| 81 | + response=$(LINEAR_API_KEY="$LINEAR_API_KEY" "$LINEAR_REQUEST" <<<"$payload") |
| 82 | + |
| 83 | + if jq -e 'has("errors") or (.data.issues == null)' >/dev/null <<<"$response"; then |
| 84 | + echo "Could not fetch open CVEs from Linear: $(jq -c '.errors // .' <<<"$response")" >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + page=$(jq -c '.data.issues.nodes' <<<"$response") |
| 89 | + all_issues=$(jq -cn \ |
| 90 | + --argjson accumulated "$all_issues" \ |
| 91 | + --argjson page "$page" \ |
| 92 | + '$accumulated + $page') |
| 93 | + |
| 94 | + has_next_page=$(jq -r '.data.issues.pageInfo.hasNextPage' <<<"$response") |
| 95 | + if [[ "$has_next_page" != "true" ]]; then |
| 96 | + break |
| 97 | + fi |
| 98 | + |
| 99 | + after=$(jq -r '.data.issues.pageInfo.endCursor // empty' <<<"$response") |
| 100 | + if [[ -z "$after" ]]; then |
| 101 | + echo "Linear reported another page without an end cursor" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | +done |
| 105 | + |
| 106 | +jq -c -f "$FILTER" <<<"$all_issues" |
0 commit comments