Skip to content

Commit 0c50882

Browse files
committed
feat(updateDoc): dont repeat updateDoc
1 parent 32c5b6d commit 0c50882

File tree

5 files changed

+92
-44
lines changed

5 files changed

+92
-44
lines changed

jobs/github-event/push.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const Log = require('gk-log')
33

44
const dbs = require('../../lib/dbs')
55
const env = require('../../lib/env')
6-
const { updateRepoDoc } = require('../../lib/repository-docs')
7-
const updatedAt = require('../../lib/updated-at')
6+
const { updateRepoDoc, updateDoc } = require('../../lib/repository-docs')
87
const diff = require('../../lib/diff-package-json')
98
const diffGreenkeeperJson = require('../../lib/diff-greenkeeper-json')
109
const deleteBranches = require('../../lib/delete-branches')
@@ -205,19 +204,6 @@ module.exports = async function (data) {
205204
log.success('success')
206205
}
207206

208-
function updateDoc (repositories, repository, repoDoc) {
209-
return repositories.put(
210-
updatedAt(
211-
Object.assign(repoDoc, {
212-
private: repository.private,
213-
fullName: repository.full_name,
214-
fork: repository.fork,
215-
hasIssues: repository.has_issues
216-
})
217-
)
218-
)
219-
}
220-
221207
function hasRelevantChanges (commits, files) {
222208
return _.some(files, file => {
223209
return _.some(['added', 'removed', 'modified'], changeType => {

jobs/github-event/repository/archived.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Log = require('gk-log')
1414
const env = require('../../../lib/env')
1515
const dbs = require('../../../lib/dbs')
1616
const { maybeUpdatePaymentsJob } = require('../../../lib/payments')
17-
const updatedAt = require('../../../lib/updated-at')
17+
const { updateDoc } = require('../../../lib/repository-docs')
1818

1919
module.exports = async function ({ repository }) {
2020
const { repositories } = await dbs()
@@ -30,17 +30,4 @@ module.exports = async function ({ repository }) {
3030
if (!env.IS_ENTERPRISE) {
3131
return maybeUpdatePaymentsJob({ accountId: repoDoc.accountId, isPrivate: repoDoc.private })
3232
}
33-
34-
function updateDoc (repositories, repository, repoDoc) {
35-
return repositories.put(
36-
updatedAt(
37-
Object.assign(repoDoc, {
38-
private: repository.private,
39-
fullName: repository.full_name,
40-
fork: repository.fork,
41-
hasIssues: repository.has_issues
42-
})
43-
)
44-
)
45-
}
4633
}

jobs/github-event/repository/privatized.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Log = require('gk-log')
88
const env = require('../../../lib/env')
99
const dbs = require('../../../lib/dbs')
1010
const { maybeUpdatePaymentsJob } = require('../../../lib/payments')
11-
const updatedAt = require('../../../lib/updated-at')
11+
const { updateDoc } = require('../../../lib/repository-docs')
1212

1313
module.exports = async function ({ repository }) {
1414
const { repositories } = await dbs()
@@ -26,17 +26,4 @@ module.exports = async function ({ repository }) {
2626
log.warn('payment required')
2727
return maybeUpdatePaymentsJob({ accountId: repoDoc.accountId, isPrivate: repoDoc.private, repositoryId })
2828
}
29-
30-
function updateDoc (repositories, repository, repoDoc) {
31-
return repositories.put(
32-
updatedAt(
33-
Object.assign(repoDoc, {
34-
private: repository.private,
35-
fullName: repository.full_name,
36-
fork: repository.fork,
37-
hasIssues: repository.has_issues
38-
})
39-
)
40-
)
41-
}
4229
}

lib/repository-docs.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const { validate } = require('./validate-greenkeeper-json')
1212

1313
module.exports = {
1414
createDocs,
15-
updateRepoDoc
15+
updateRepoDoc,
16+
updateDoc
1617
}
1718

1819
// trigger (several) initial-subgroup-pr(s):
@@ -101,3 +102,16 @@ function createDocs ({ repositories, accountId }) {
101102
packages: {}
102103
}))
103104
}
105+
106+
function updateDoc (repositories, repository, repoDoc) {
107+
return repositories.put(
108+
updatedAt(
109+
Object.assign(repoDoc, {
110+
private: repository.private,
111+
fullName: repository.full_name,
112+
fork: repository.fork,
113+
hasIssues: repository.has_issues
114+
})
115+
)
116+
)
117+
}

test/jobs/payment-required.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const nock = require('nock')
2+
3+
const dbs = require('../../lib/dbs')
4+
const removeIfExists = require('../helpers/remove-if-exists')
5+
6+
describe('payment-required', async () => {
7+
beforeAll(async () => {
8+
const { repositories, installations } = await dbs()
9+
10+
await installations.put({
11+
_id: '111',
12+
installation: 11,
13+
plan: 'free'
14+
})
15+
16+
await repositories.put({
17+
_id: '1_payment-required',
18+
accountId: '111',
19+
fullName: 'jacoba/private',
20+
enabled: true,
21+
private: true
22+
})
23+
})
24+
25+
beforeEach(() => {
26+
jest.resetModules()
27+
jest.clearAllMocks()
28+
})
29+
30+
afterAll(async () => {
31+
const { repositories, installations } = await dbs()
32+
await Promise.all([
33+
removeIfExists(repositories, 'payment-required'),
34+
removeIfExists(installations, '111')
35+
])
36+
})
37+
38+
test('create payment-required issue', async () => {
39+
expect.assertions(9)
40+
const githubMock = nock('https://api.github.com')
41+
.post('/app/installations/11/access_tokens')
42+
.optionally()
43+
.reply(200, {
44+
token: 'secret'
45+
})
46+
.get('/rate_limit')
47+
.optionally()
48+
.reply(200)
49+
.post('/repos/jacoba/private/issues', ({ title, body, labels }) => {
50+
expect(title).toEqual('Payment required')
51+
expect(body).toMatch(/🚨 You privatized your repo. 🚨/)
52+
expect(body).toMatch(/Please enter your payment information at/)
53+
expect(labels[0]).toEqual('greenkeeper')
54+
return true
55+
})
56+
.reply(201, () => {
57+
return {
58+
number: 10
59+
}
60+
})
61+
62+
const paymentRequired = require('../../jobs/payment-required')
63+
const newJob = await paymentRequired({ accountId: '111', repositoryId: '1_payment-required' })
64+
expect(newJob).toBeFalsy()
65+
66+
const { repositories } = await dbs()
67+
const issue = await repositories.get('1_payment-required:issue:10')
68+
expect(issue.initial).toBeFalsy()
69+
expect(issue.type).toEqual('issue')
70+
expect(issue.number).toBe(10)
71+
expect(issue.repositoryId).toBe('1_payment-required')
72+
githubMock.done()
73+
})
74+
})

0 commit comments

Comments
 (0)