Skip to content

Instantly share code, notes, and snippets.

@tichopad
Created March 21, 2023 12:46
Show Gist options
  • Save tichopad/b45bfe8a0d3e7254ce646af73466166e to your computer and use it in GitHub Desktop.
Save tichopad/b45bfe8a0d3e7254ce646af73466166e to your computer and use it in GitHub Desktop.
Bulk create Sentry Projects
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