-
-
Save sub314xxl/09a8c06cd742ae640fed63b2c505dd6a to your computer and use it in GitHub Desktop.
Bulk create Sentry Projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs/promises'; | |
const SENTRY_API_KEY = 'enterapikeyhere'; | |
const ORG_SLUG = 'enterorgslughere'; | |
const TEAM_SLUG = 'enterteamnamehere'; | |
const fetchSentry = ( | |
path: string, | |
method: 'GET' | 'POST' | 'PUT' = 'GET', | |
body?: Record<string, any>, | |
) => { | |
return fetch(`https://sentry.io/api/0/${path}`, { | |
method, | |
headers: { | |
Authorization: `Bearer ${SENTRY_API_KEY}`, | |
'Content-Type': 'application/json', | |
}, | |
body: body ? JSON.stringify(body) : undefined, | |
}).then((response) => response.json()); | |
}; | |
const createProject = async ({ name, platform }: { name: string; platform: string }) => { | |
// Create project | |
await fetchSentry(`teams/${ORG_SLUG}/${TEAM_SLUG}/projects/`, 'POST', { | |
name, | |
}); | |
// Add platform to it | |
await fetchSentry(`projects/${ORG_SLUG}/${name}/`, 'PUT', { | |
platform, | |
}); | |
// Get its DSN client key | |
const keyData = await fetchSentry(`projects/${ORG_SLUG}/${name}/keys/`); | |
const dsn: string = keyData[0].dsn.public; | |
return { | |
name, | |
dsn, | |
}; | |
}; | |
const projectsToCreate = [ | |
{ name: 'service-location', platform: 'node-awslambda' }, | |
{ name: 'mobile-app', platform: 'react-native' }, | |
]; | |
const resultsPromises = projectsToCreate.map(createProject); | |
Promise.all(resultsPromises) | |
.then((result) => { | |
console.log(result); | |
return result; | |
}) | |
.then((result) => { | |
return result.map((project) => [project.name, project.dsn] as const); | |
}) | |
.then((entries) => { | |
return Object.fromEntries(entries); | |
}) | |
.then((namesToDSNs) => { | |
return fs.writeFile('sentry-dsn-values.json', JSON.stringify(namesToDSNs, null, 2), { | |
encoding: 'utf-8', | |
}); | |
}) | |
.then(() => { | |
console.log('File saved.'); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment