Skip to content

Commit a80d688

Browse files
committed
refactor(octokit): migrate from rest methods to direct use of octokit.request
1 parent 357751c commit a80d688

File tree

13 files changed

+272
-271
lines changed

13 files changed

+272
-271
lines changed

lib/plugins/collaborators.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ export default class Collaborators extends Diffable {
1212
}
1313
}
1414

15-
find () {
16-
return this.github.repos
17-
.listCollaborators({ repo: this.repo.repo, owner: this.repo.owner, affiliation: 'direct' })
18-
.then(res => {
19-
return res.data.map(user => {
20-
return {
21-
// Force all usernames to lowercase to avoid comparison issues.
22-
username: user.login.toLowerCase(),
23-
permission:
24-
(user.permissions.admin && 'admin') ||
25-
(user.permissions.push && 'push') ||
26-
(user.permissions.pull && 'pull')
27-
}
28-
})
29-
})
15+
async find () {
16+
const { data: collaborators } = await this.github.request('GET /repos/{owner}/{repo}/collaborators', {
17+
repo: this.repo.repo,
18+
owner: this.repo.owner,
19+
affiliation: 'direct'
20+
})
21+
22+
return collaborators.map(collaborator => ({
23+
// Force all usernames to lowercase to avoid comparison issues.
24+
username: collaborator.login.toLowerCase(),
25+
permission:
26+
(collaborator.permissions.admin && 'admin') ||
27+
(collaborator.permissions.push && 'push') ||
28+
(collaborator.permissions.pull && 'pull')
29+
}))
3030
}
3131

3232
comparator (existing, attrs) {
@@ -42,10 +42,13 @@ export default class Collaborators extends Diffable {
4242
}
4343

4444
add (attrs) {
45-
return this.github.repos.addCollaborator(Object.assign({}, attrs, this.repo))
45+
return this.github.request('PUT /repos/{owner}/{repo}/collaborators/{username}', { ...attrs, ...this.repo })
4646
}
4747

4848
remove (existing) {
49-
return this.github.repos.removeCollaborator(Object.assign({ username: existing.username }, this.repo))
49+
return this.github.request('DELETE /repos/{owner}/{repo}/collaborators/{username}', {
50+
username: existing.username,
51+
...this.repo
52+
})
5053
}
5154
}

lib/plugins/labels.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export default class Labels extends Diffable {
1919
}
2020

2121
find () {
22-
const options = this.github.issues.listLabelsForRepo.endpoint.merge(this.wrapAttrs({ per_page: 100 }))
23-
return this.github.paginate(options)
22+
return this.github.paginate('GET /repos/{owner}/{repo}/labels', this.wrapAttrs({ per_page: 100 }))
2423
}
2524

2625
comparator (existing, attrs) {
@@ -32,15 +31,15 @@ export default class Labels extends Diffable {
3231
}
3332

3433
update (existing, attrs) {
35-
return this.github.issues.updateLabel(this.wrapAttrs(attrs))
34+
return this.github.request('PATCH /repos/{owner}/{repo}/labels/{name}', this.wrapAttrs(attrs))
3635
}
3736

3837
add (attrs) {
39-
return this.github.issues.createLabel(this.wrapAttrs(attrs))
38+
return this.github.request('POST /repos/{owner}/{repo}/labels', this.wrapAttrs(attrs))
4039
}
4140

4241
remove (existing) {
43-
return this.github.issues.deleteLabel(this.wrapAttrs({ name: existing.name }))
42+
return this.github.request('DELETE /repos/{owner}/{repo}/labels/{name}', this.wrapAttrs({ name: existing.name }))
4443
}
4544

4645
wrapAttrs (attrs) {

lib/plugins/milestones.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ export default class Milestones extends Diffable {
1414
}
1515

1616
find () {
17-
const options = this.github.issues.listMilestones.endpoint.merge(
18-
Object.assign({ per_page: 100, state: 'all' }, this.repo)
19-
)
20-
return this.github.paginate(options)
17+
return this.github.paginate('GET /repos/{owner}/{repo}/milestones', { per_page: 100, state: 'all', ...this.repo })
2118
}
2219

2320
comparator (existing, attrs) {
@@ -31,20 +28,31 @@ export default class Milestones extends Diffable {
3128
update (existing, attrs) {
3229
const { owner, repo } = this.repo
3330

34-
return this.github.issues.updateMilestone(
35-
Object.assign({ milestone_number: existing.number }, attrs, { owner, repo })
36-
)
31+
return this.github.request('PATCH /repos/{owner}/{repo}/milestones/{milestone_number}', {
32+
milestone_number: existing.number,
33+
...attrs,
34+
owner,
35+
repo
36+
})
3737
}
3838

3939
add (attrs) {
4040
const { owner, repo } = this.repo
4141

42-
return this.github.issues.createMilestone(Object.assign({}, attrs, { owner, repo }))
42+
return this.github.request('POST /repos/{owner}/{repo}/milestones', {
43+
...attrs,
44+
owner,
45+
repo
46+
})
4347
}
4448

4549
remove (existing) {
4650
const { owner, repo } = this.repo
4751

48-
return this.github.issues.deleteMilestone(Object.assign({ milestone_number: existing.number }, { owner, repo }))
52+
return this.github.request('DELETE /repos/{owner}/{repo}/milestones/{milestone_number}', {
53+
milestone_number: existing.number,
54+
owner,
55+
repo
56+
})
4957
}
5058
}

lib/plugins/repository.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,31 @@ const enableAutomatedSecurityFixes = ({ github, settings, enabled }) => {
33
return Promise.resolve()
44
}
55

6-
const args = {
6+
const verb = enabled ? 'PUT' : 'DELETE'
7+
8+
return github.request(`${verb} /repos/{owner}/{repo}/automated-security-fixes`, {
79
owner: settings.owner,
810
repo: settings.repo,
911
mediaType: {
1012
previews: ['london']
1113
}
12-
}
13-
const methodName = enabled ? 'enableAutomatedSecurityFixes' : 'disableAutomatedSecurityFixes'
14-
15-
return github.repos[methodName](args)
14+
})
1615
}
1716

1817
const enableVulnerabilityAlerts = ({ github, settings, enabled }) => {
1918
if (enabled === undefined) {
2019
return Promise.resolve()
2120
}
2221

23-
const args = {
22+
const verb = enabled ? 'PUT' : 'DELETE'
23+
24+
return github.request(`${verb} /repos/{owner}/{repo}/vulnerability-alerts`, {
2425
owner: settings.owner,
2526
repo: settings.repo,
2627
mediaType: {
2728
previews: ['dorian']
2829
}
29-
}
30-
const methodName = enabled ? 'enableVulnerabilityAlerts' : 'disableVulnerabilityAlerts'
31-
32-
return github.repos[methodName](args)
30+
})
3331
}
3432

3533
export default class Repository {
@@ -46,23 +44,23 @@ export default class Repository {
4644
delete this.settings.enable_automated_security_fixes
4745
}
4846

49-
sync () {
47+
async sync () {
5048
this.settings.name = this.settings.name || this.settings.repo
51-
return this.github.repos
52-
.update(this.settings)
53-
.then(() => {
54-
if (this.topics) {
55-
return this.github.repos.replaceAllTopics({
56-
owner: this.settings.owner,
57-
repo: this.settings.repo,
58-
names: this.topics.split(/\s*,\s*/),
59-
mediaType: {
60-
previews: ['mercy']
61-
}
62-
})
49+
50+
await this.github.request('PATCH /repos/{owner}/{repo}', this.settings)
51+
52+
if (this.topics) {
53+
await this.github.request('PUT /repos/{owner}/{repo}/topics', {
54+
owner: this.settings.owner,
55+
repo: this.settings.repo,
56+
names: this.topics.split(/\s*,\s*/),
57+
mediaType: {
58+
previews: ['mercy']
6359
}
6460
})
65-
.then(() => enableVulnerabilityAlerts({ enabled: this.enableVulnerabilityAlerts, ...this }))
66-
.then(() => enableAutomatedSecurityFixes({ enabled: this.enableAutomatedSecurityFixes, ...this }))
61+
}
62+
63+
await enableVulnerabilityAlerts({ enabled: this.enableVulnerabilityAlerts, ...this })
64+
await enableAutomatedSecurityFixes({ enabled: this.enableAutomatedSecurityFixes, ...this })
6765
}
6866
}

lib/plugins/rulesets.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import Diffable from './diffable.js'
44

55
export default class Rulesets extends Diffable {
66
async find () {
7-
const { data: rulesets } = await this.github.repos.getRepoRulesets({ ...this.repo, includes_parents: false })
7+
const { data: rulesets } = await this.github.request('GET /repos/{owner}/{repo}/rulesets', {
8+
...this.repo,
9+
includes_parents: false
10+
})
811

912
const expandedRulesetsData = await Promise.all(
10-
rulesets.map(({ id }) => this.github.repos.getRepoRuleset({ ...this.repo, ruleset_id: id }))
13+
rulesets.map(({ id }) =>
14+
this.github.request('GET /repos/{owner}/{repo}/rulesets/{ruleset_id}', { ...this.repo, ruleset_id: id })
15+
)
1116
)
1217

1318
return expandedRulesetsData.map(({ data }) => data)
@@ -34,14 +39,21 @@ export default class Rulesets extends Diffable {
3439
}
3540

3641
update (existing, attrs) {
37-
return this.github.repos.updateRepoRuleset({ ...this.repo, ruleset_id: existing.id, ...attrs })
42+
return this.github.request('PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}', {
43+
...this.repo,
44+
ruleset_id: existing.id,
45+
...attrs
46+
})
3847
}
3948

4049
remove (existing) {
41-
return this.github.repos.deleteRepoRuleset({ ...this.repo, ruleset_id: existing.id })
50+
return this.github.request('DELETE /repos/{owner}/{repo}/rulesets/{ruleset_id}', {
51+
...this.repo,
52+
ruleset_id: existing.id
53+
})
4254
}
4355

4456
async add (attrs) {
45-
await this.github.repos.createRepoRuleset({ ...this.repo, ...attrs })
57+
await this.github.request('POST /repos/{owner}/{repo}/rulesets', { ...this.repo, ...attrs })
4658
}
4759
}

lib/plugins/teams.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import Diffable from './diffable.js'
22

33
// it is necessary to use this endpoint until GitHub Enterprise supports
44
// the modern version under /orgs
5-
const teamRepoEndpoint = '/teams/:team_id/repos/:owner/:repo'
5+
const teamRepoEndpoint = '/teams/{team_id}/repos/{owner}/{repo}'
66

77
export default class Teams extends Diffable {
88
find () {
9-
return this.github.repos.listTeams(this.repo).then(res => res.data)
9+
return this.github.request('GET /repos/{owner}/{repo}/teams', this.repo).then(res => res.data)
1010
}
1111

1212
comparator (existing, attrs) {
@@ -22,7 +22,7 @@ export default class Teams extends Diffable {
2222
}
2323

2424
async add (attrs) {
25-
const { data: existing } = await this.github.request('GET /orgs/:org/teams/:team_slug', {
25+
const { data: existing } = await this.github.request('GET /orgs/{org}/teams/{team_slug}', {
2626
org: this.repo.owner,
2727
team_slug: attrs.name
2828
})

test/unit/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('plugin', () => {
2828
}
2929
}
3030

31-
app = new Probot({ secret: any.string(), Octokit })
31+
app = new Probot({ secret: any.string(), Octokit, appId: any.string(), privateKey: any.string() })
3232

3333
event = {
3434
name: 'push',

test/unit/lib/plugins/collaborators.test.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import Collaborators from '../../../../lib/plugins/collaborators'
21
import { jest } from '@jest/globals'
2+
import { when } from 'jest-when'
3+
4+
import Collaborators from '../../../../lib/plugins/collaborators'
35

46
describe('Collaborators', () => {
57
let github
8+
const repoOwner = 'bkeepers'
9+
const repoName = 'test'
610

711
function configure (config) {
8-
return new Collaborators(github, { owner: 'bkeepers', repo: 'test' }, config)
12+
return new Collaborators(github, { owner: repoOwner, repo: repoName }, config)
913
}
1014

1115
beforeEach(() => {
12-
github = {
13-
repos: {
14-
listCollaborators: jest.fn().mockImplementation(() => Promise.resolve([])),
15-
removeCollaborator: jest.fn().mockImplementation(() => Promise.resolve()),
16-
addCollaborator: jest.fn().mockImplementation(() => Promise.resolve())
17-
}
18-
}
16+
github = { request: jest.fn().mockImplementation(() => Promise.resolve()) }
1917
})
2018

2119
describe('sync', () => {
@@ -27,41 +25,41 @@ describe('Collaborators', () => {
2725
{ username: 'DIFFERENTcase', permission: 'push' }
2826
])
2927

30-
github.repos.listCollaborators.mockReturnValueOnce(
31-
Promise.resolve({
28+
when(github.request)
29+
.calledWith('GET /repos/{owner}/{repo}/collaborators', {
30+
repo: repoName,
31+
owner: repoOwner,
32+
affiliation: 'direct'
33+
})
34+
.mockResolvedValue({
3235
data: [
3336
{ login: 'bkeepers', permissions: { admin: true, push: true, pull: true } },
3437
{ login: 'updated-permission', permissions: { admin: false, push: false, pull: true } },
3538
{ login: 'removed-user', permissions: { admin: false, push: true, pull: true } },
3639
{ login: 'differentCase', permissions: { admin: false, push: true, pull: true } }
3740
]
3841
})
39-
)
4042

4143
return plugin.sync().then(() => {
42-
expect(github.repos.addCollaborator).toHaveBeenCalledWith({
44+
expect(github.request).toHaveBeenCalledWith('PUT /repos/{owner}/{repo}/collaborators/{username}', {
4345
owner: 'bkeepers',
4446
repo: 'test',
4547
username: 'added-user',
4648
permission: 'push'
4749
})
4850

49-
expect(github.repos.addCollaborator).toHaveBeenCalledWith({
51+
expect(github.request).toHaveBeenCalledWith('PUT /repos/{owner}/{repo}/collaborators/{username}', {
5052
owner: 'bkeepers',
5153
repo: 'test',
5254
username: 'updated-permission',
5355
permission: 'push'
5456
})
5557

56-
expect(github.repos.addCollaborator).toHaveBeenCalledTimes(2)
57-
58-
expect(github.repos.removeCollaborator).toHaveBeenCalledWith({
58+
expect(github.request).toHaveBeenCalledWith('DELETE /repos/{owner}/{repo}/collaborators/{username}', {
5959
owner: 'bkeepers',
6060
repo: 'test',
6161
username: 'removed-user'
6262
})
63-
64-
expect(github.repos.removeCollaborator).toHaveBeenCalledTimes(1)
6563
})
6664
})
6765
})

0 commit comments

Comments
 (0)