Skip to content

Instantly share code, notes, and snippets.

@ijsnow
Created December 11, 2018 19:41
Show Gist options
  • Save ijsnow/0cbd968168d869ee3dd13216e9236571 to your computer and use it in GitHub Desktop.
Save ijsnow/0cbd968168d869ee3dd13216e9236571 to your computer and use it in GitHub Desktop.
import axios from 'axios';
import * as JSONC from '@sqs/jsonc-parser'
const getUsersQuery = `query AllUsers {
users {
nodes {
id
username
latestSettings {
id
contents
}
}
}
}`;
const api = axios.create({
baseURL: 'https://sourcegraph.com/.api/',
timeout: 1000,
headers: {'Authorization': 'token <redacted>'}
});
api.defaults.timeout = 10000
interface Res<T> {
data: T
}
interface User {
id: string
latestSettings: {
id: number
contents: string
}
}
async function enableCodeCovForUsersWhoUseIt(): Promise<void> {
const res = await api.post<Res<{users: {nodes: User[]}}>>('/graphql', {
query: getUsersQuery
})
const users = res.data.data.users.nodes
for (const user of users) {
if (user.latestSettings) {
const parsed = JSONC.parse(user.latestSettings.contents)
if (!parsed || !parsed.extensions || typeof parsed.extensions['sourcegraph/codecov'] !== 'undefined') {
continue
}
const hasCodecovKeys = Object.keys(parsed).find(key => !!key.match(/^codecov/))
if (!hasCodecovKeys) {
continue
}
// Do the settings
await enableCodecovForSubject(user.id, user.latestSettings.id)
}
}
}
const updateSettingsForUserMutation = `mutation UpdateSettings($input: SettingsMutationGroupInput!, $edit: SettingsEdit!) {
settingsMutation(input: $input) {
editSettings(edit: $edit) {
empty {
alwaysNil
}
}
}
}`;
/**
* Input shape for the above
*
* {
* "input": {
* "subject": "VXNlcjoxNDM0",
* "lastID": 6105
* },
* "edit": {
* "keyPath": [{"property": "extensions"}, {"property": "sourcegraph/codecov"}],
* "value": false
* }
* }
*/
async function enableCodecovForSubject(id: string, settingsID: number): Promise<void> {
await api.post('/graphql', {
query: updateSettingsForUserMutation,
variables: {
input: {
subject: id,
lastID: settingsID
},
edit: {
keyPath: [{"property": "extensions"}, {"property": "sourcegraph/codecov"}],
value: true
}
}
})
}
enableCodeCovForUsersWhoUseIt().then(() => console.log('done.')).catch(err => {
console.error('Error during execution:', err)
})
{
"name": "settings-update",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"run": "ts-node index.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "^1.15.3",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.2.1"
},
"dependencies": {
"@sqs/jsonc-parser": "^1.0.3",
"axios": "^0.18.0"
}
}
{
"compilerOptions": {
"lib": ["es2017", "dom"],
"moduleResolution": "node"
},
"include": ["src"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment