Skip to content

Commit

Permalink
Project management automation: Fix 'add milestone' (#17157)
Browse files Browse the repository at this point in the history
Octokit returns all resources wrapped in a `data` key, e.g.
`data.milestone` instead of `milestone`. Also,
`octokit.repos.getContents()` returns a base64 encoded string.
  • Loading branch information
noisysocks authored and gziolo committed Aug 29, 2019
1 parent 14e5a74 commit 5a8fb39
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
8 changes: 4 additions & 4 deletions packages/project-management-automation/lib/add-milestone.js
Expand Up @@ -31,7 +31,7 @@ async function addMilestone( payload, octokit ) {

debug( 'add-milestone: Fetching current milestone' );

const { milestone } = await octokit.issues.get( {
const { data: { milestone } } = await octokit.issues.get( {
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
Expand All @@ -44,13 +44,13 @@ async function addMilestone( payload, octokit ) {

debug( 'add-milestone: Fetching `package.json` contents' );

const { content } = await octokit.repos.getContents( {
const { data: { content, encoding } } = await octokit.repos.getContents( {
owner: payload.repository.owner.login,
repo: payload.repository.name,
path: 'package.json',
} );

const { version } = JSON.parse( content );
const { version } = JSON.parse( Buffer.from( content, encoding ).toString() );

let [ major, minor ] = version.split( '.' ).map( Number );

Expand Down Expand Up @@ -81,7 +81,7 @@ async function addMilestone( payload, octokit ) {

debug( 'add-milestone: Fetching all milestones' );

const milestones = await octokit.issues.listMilestonesForRepo( {
const { data: milestones } = await octokit.issues.listMilestonesForRepo( {
owner: payload.repository.owner.login,
repo: payload.repository.name,
} );
Expand Down
54 changes: 35 additions & 19 deletions packages/project-management-automation/lib/test/add-milestone.js
Expand Up @@ -80,7 +80,9 @@ describe( 'addFirstTimeContributorLabel', () => {
const octokit = {
issues: {
get: jest.fn( () => Promise.resolve( {
milestone: 'Gutenberg 6.4',
data: {
milestone: 'Gutenberg 6.4',
},
} ) ),
createMilestone: jest.fn(),
listMilestonesForRepo: jest.fn(),
Expand Down Expand Up @@ -123,21 +125,28 @@ describe( 'addFirstTimeContributorLabel', () => {
const octokit = {
issues: {
get: jest.fn( () => Promise.resolve( {
milestone: null,
data: {
milestone: null,
},
} ) ),
createMilestone: jest.fn(),
listMilestonesForRepo: jest.fn( () => Promise.resolve( [
{ title: 'Gutenberg 6.2', number: 10 },
{ title: 'Gutenberg 6.3', number: 11 },
{ title: 'Gutenberg 6.4', number: 12 },
] ) ),
listMilestonesForRepo: jest.fn( () => Promise.resolve( {
data: [
{ title: 'Gutenberg 6.2', number: 10 },
{ title: 'Gutenberg 6.3', number: 11 },
{ title: 'Gutenberg 6.4', number: 12 },
],
} ) ),
update: jest.fn(),
},
repos: {
getContents: jest.fn( () => Promise.resolve( {
content: JSON.stringify( {
version: '6.3.0',
} ),
data: {
content: Buffer.from( JSON.stringify( {
version: '6.3.0',
} ) ).toString( 'base64' ),
encoding: 'base64',
},
} ) ),
},
};
Expand Down Expand Up @@ -191,21 +200,28 @@ describe( 'addFirstTimeContributorLabel', () => {
const octokit = {
issues: {
get: jest.fn( () => Promise.resolve( {
milestone: null,
data: {
milestone: null,
},
} ) ),
createMilestone: jest.fn(),
listMilestonesForRepo: jest.fn( () => Promise.resolve( [
{ title: 'Gutenberg 6.8', number: 10 },
{ title: 'Gutenberg 6.9', number: 11 },
{ title: 'Gutenberg 7.0', number: 12 },
] ) ),
listMilestonesForRepo: jest.fn( () => Promise.resolve( {
data: [
{ title: 'Gutenberg 6.8', number: 10 },
{ title: 'Gutenberg 6.9', number: 11 },
{ title: 'Gutenberg 7.0', number: 12 },
],
} ) ),
update: jest.fn(),
},
repos: {
getContents: jest.fn( () => Promise.resolve( {
content: JSON.stringify( {
version: '6.9.0',
} ),
data: {
content: Buffer.from( JSON.stringify( {
version: '6.9.0',
} ) ).toString( 'base64' ),
encoding: 'base64',
},
} ) ),
},
};
Expand Down

0 comments on commit 5a8fb39

Please sign in to comment.