|
| 1 | +import { gql, GraphQLClient } from 'graphql-request' |
| 2 | + |
| 3 | +/** |
| 4 | + * Get inputs from environment variables |
| 5 | + */ |
| 6 | +function getInputs() { |
| 7 | + return { |
| 8 | + entityGuid: process.env.ENTITY_GUID, |
| 9 | + apiKey: process.env.API_KEY, |
| 10 | + application: process.env.APPLICATION, |
| 11 | + version: process.env.VERSION, |
| 12 | + category: process.env.CATEGORY || 'Deployment', |
| 13 | + type: process.env.TYPE || 'Basic', |
| 14 | + featureFlagId: process.env.FEATURE_FLAG_ID, |
| 15 | + description: process.env.DESCRIPTION, |
| 16 | + changelog: process.env.CHANGELOG, |
| 17 | + commit: process.env.COMMIT, |
| 18 | + deepLink: process.env.DEEP_LINK, |
| 19 | + user: process.env.USER, |
| 20 | + groupId: process.env.GROUP_ID, |
| 21 | + shortDescription: process.env.SHORT_DESCRIPTION, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Safely escape and quote a string for GraphQL |
| 27 | + */ |
| 28 | +function gqlString(str) { |
| 29 | + if (!str) return null |
| 30 | + // Let GraphQL handle the escaping by using JSON.stringify |
| 31 | + return JSON.stringify(str) |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Build the categoryFields GraphQL string based on category |
| 36 | + */ |
| 37 | +function buildCategoryFieldsGQL(category, inputs) { |
| 38 | + if (category === 'Deployment') { |
| 39 | + const fields = [`version: ${gqlString(inputs.version)}`] |
| 40 | + |
| 41 | + if (inputs.changelog) { |
| 42 | + fields.push(`changelog: ${gqlString(inputs.changelog)}`) |
| 43 | + } |
| 44 | + if (inputs.commit) { |
| 45 | + fields.push(`commit: ${gqlString(inputs.commit)}`) |
| 46 | + } |
| 47 | + if (inputs.deepLink) { |
| 48 | + fields.push(`deepLink: ${gqlString(inputs.deepLink)}`) |
| 49 | + } |
| 50 | + |
| 51 | + return `categoryFields: { deployment: { ${fields.join(', ')} } }` |
| 52 | + } else if (category === 'Feature Flag') { |
| 53 | + return `categoryFields: { featureFlag: { featureFlagId: ${gqlString(inputs.featureFlagId)} } }` |
| 54 | + } |
| 55 | + |
| 56 | + return '' |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Build optional fields GraphQL string |
| 61 | + */ |
| 62 | +function buildOptionalFieldsGQL(inputs) { |
| 63 | + const fields = [] |
| 64 | + |
| 65 | + if (inputs.user) { |
| 66 | + fields.push(`user: ${gqlString(inputs.user)}`) |
| 67 | + } |
| 68 | + if (inputs.description) { |
| 69 | + fields.push(`description: ${gqlString(inputs.description)}`) |
| 70 | + } |
| 71 | + if (inputs.shortDescription) { |
| 72 | + fields.push(`shortDescription: ${gqlString(inputs.shortDescription)}`) |
| 73 | + } |
| 74 | + if (inputs.groupId) { |
| 75 | + fields.push(`groupId: ${gqlString(inputs.groupId)}`) |
| 76 | + } |
| 77 | + |
| 78 | + return fields.join('\n ') |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Create change tracking event |
| 83 | + */ |
| 84 | +async function createChangeTrackingEvent() { |
| 85 | + try { |
| 86 | + // All NerdGraph API calls go to staging |
| 87 | + const apiEndpoint = 'https://staging-api.newrelic.com/graphql' |
| 88 | + |
| 89 | + const inputs = getInputs() |
| 90 | + |
| 91 | + console.log('Creating change tracking event via NerdGraph...') |
| 92 | + console.log(` Category: ${inputs.category}`) |
| 93 | + console.log(` Type: ${inputs.type}`) |
| 94 | + console.log(` Entity GUID: ${inputs.entityGuid}`) |
| 95 | + console.log(` Application: ${inputs.application}`) |
| 96 | + console.log(` API Endpoint: ${apiEndpoint}`) |
| 97 | + |
| 98 | + if (inputs.category === 'Deployment') { |
| 99 | + console.log(` Version: ${inputs.version}`) |
| 100 | + } else if (inputs.category === 'Feature Flag') { |
| 101 | + console.log(` Feature Flag ID: ${inputs.featureFlagId}`) |
| 102 | + } |
| 103 | + |
| 104 | + // Build category fields |
| 105 | + const categoryFieldsGQL = buildCategoryFieldsGQL(inputs.category, inputs) |
| 106 | + |
| 107 | + // Build optional fields |
| 108 | + const optionalFieldsGQL = buildOptionalFieldsGQL(inputs) |
| 109 | + |
| 110 | + // Build the GraphQL mutation inline (matching the documented examples) |
| 111 | + // Note: We use inline values instead of variables to match New Relic's API expectations |
| 112 | + const mutation = gql` |
| 113 | + mutation { |
| 114 | + changeTrackingCreateEvent( |
| 115 | + changeTrackingEvent: { |
| 116 | + categoryAndTypeData: { |
| 117 | + ${categoryFieldsGQL} |
| 118 | + kind: { category: ${gqlString(inputs.category)}, type: ${gqlString(inputs.type)} } |
| 119 | + } |
| 120 | + entitySearch: { query: "id = '${inputs.entityGuid}'" } |
| 121 | + ${optionalFieldsGQL} |
| 122 | + } |
| 123 | + ) { |
| 124 | + changeTrackingEvent { |
| 125 | + changeTrackingId |
| 126 | + timestamp |
| 127 | + category |
| 128 | + type |
| 129 | + user |
| 130 | + shortDescription |
| 131 | + description |
| 132 | + groupId |
| 133 | + } |
| 134 | + messages |
| 135 | + } |
| 136 | + } |
| 137 | + ` |
| 138 | + |
| 139 | + // Create GraphQL client |
| 140 | + const client = new GraphQLClient(apiEndpoint, { |
| 141 | + headers: { |
| 142 | + 'API-Key': inputs.apiKey, |
| 143 | + }, |
| 144 | + }) |
| 145 | + |
| 146 | + // Execute the mutation |
| 147 | + const response = await client.request(mutation) |
| 148 | + |
| 149 | + console.log('') |
| 150 | + console.log('Response:') |
| 151 | + console.log(JSON.stringify(response, null, 2)) |
| 152 | + |
| 153 | + const trackingId = response.changeTrackingCreateEvent.changeTrackingEvent.changeTrackingId |
| 154 | + |
| 155 | + if (trackingId) { |
| 156 | + console.log('') |
| 157 | + console.log('✓ Change tracking event created successfully!') |
| 158 | + console.log(` Tracking ID: ${trackingId}`) |
| 159 | + |
| 160 | + // Display any messages from the API |
| 161 | + const messages = response.changeTrackingCreateEvent.messages |
| 162 | + if (messages && messages.length > 0) { |
| 163 | + console.log(' Messages:', messages) |
| 164 | + } |
| 165 | + } else { |
| 166 | + throw new Error('Tracking ID not found in response') |
| 167 | + } |
| 168 | + |
| 169 | + } catch (error) { |
| 170 | + console.error('') |
| 171 | + console.error('Error: Change tracking event creation failed!') |
| 172 | + |
| 173 | + if (error.response) { |
| 174 | + console.error('GraphQL Errors:') |
| 175 | + console.error(JSON.stringify(error.response.errors, null, 2)) |
| 176 | + console.error('') |
| 177 | + if (error.response.status) { |
| 178 | + console.error('HTTP Status:', error.response.status) |
| 179 | + } |
| 180 | + } else { |
| 181 | + console.error(error.message) |
| 182 | + if (error.stack) { |
| 183 | + console.error(error.stack) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + process.exit(1) |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// Run the function |
| 192 | +createChangeTrackingEvent() |
| 193 | + |
0 commit comments