2020-01-13 15:06:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const requestPromise = require( 'request-promise' );
|
|
|
|
const { graphql } = require( '@octokit/graphql' );
|
|
|
|
const { pkg, REPO } = require( '../config' );
|
|
|
|
|
|
|
|
/* eslint no-console: 0 */
|
|
|
|
|
|
|
|
const headers = {
|
2023-08-23 13:30:35 +00:00
|
|
|
authorization: `token ${ pkg.changelog.githubToken }`,
|
2020-01-13 15:06:32 +00:00
|
|
|
'user-agent': 'changelog-tool',
|
|
|
|
};
|
|
|
|
|
|
|
|
const authedGraphql = graphql.defaults( { headers } );
|
|
|
|
|
|
|
|
const getPullRequestType = ( labels ) => {
|
|
|
|
const typeLabel = labels.find( ( label ) =>
|
|
|
|
label.name.includes( pkg.changelog.labelPrefix )
|
|
|
|
);
|
|
|
|
if ( ! typeLabel ) {
|
|
|
|
return pkg.changelog.defaultPrefix;
|
|
|
|
}
|
|
|
|
return typeLabel.name.replace( `${ pkg.changelog.labelPrefix } `, '' );
|
|
|
|
};
|
|
|
|
|
2020-01-28 13:47:26 +00:00
|
|
|
const devNoteSuffix = ( labels ) => {
|
|
|
|
const noteLabel = labels.find( ( label ) =>
|
|
|
|
label.name.includes( pkg.changelog.devNoteLabel )
|
|
|
|
);
|
|
|
|
return noteLabel ? ' [DN]' : '';
|
|
|
|
};
|
|
|
|
|
2020-01-13 15:06:32 +00:00
|
|
|
const isCollaborator = async ( username ) => {
|
|
|
|
return requestPromise( {
|
|
|
|
url: `https://api.github.com/orgs/${
|
|
|
|
REPO.split( '/' )[ 0 ]
|
|
|
|
}/members/${ username }`,
|
|
|
|
headers,
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
} )
|
|
|
|
.then( ( response ) => {
|
|
|
|
return response.statusCode === 204;
|
|
|
|
} )
|
|
|
|
.catch( ( err ) => {
|
|
|
|
if ( err.statusCode !== 404 ) {
|
|
|
|
console.log( '🤯' );
|
|
|
|
console.log( err.message );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const getEntry = async ( pullRequest ) => {
|
|
|
|
if (
|
|
|
|
pullRequest.labels.nodes.some(
|
|
|
|
( label ) => label.name === pkg.changelog.skipLabel
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const collaborator = await isCollaborator( pullRequest.author.login );
|
|
|
|
const type = getPullRequestType( pullRequest.labels.nodes );
|
|
|
|
const authorTag = collaborator ? '' : `👏 @${ pullRequest.author.login }`;
|
2020-01-28 13:47:26 +00:00
|
|
|
const devNote = devNoteSuffix( pullRequest.labels.nodes );
|
2020-01-13 15:06:32 +00:00
|
|
|
let title;
|
|
|
|
if ( /### Changelog\r\n\r\n> /.test( pullRequest.body ) ) {
|
|
|
|
const bodyParts = pullRequest.body.split( '### Changelog\r\n\r\n> ' );
|
|
|
|
const note = bodyParts[ bodyParts.length - 1 ];
|
|
|
|
title = note
|
|
|
|
// Remove comment prompt
|
|
|
|
.replace( /<!---(.*)--->/gm, '' )
|
|
|
|
// Remove new lines and whitespace
|
|
|
|
.trim();
|
|
|
|
if ( ! title.length ) {
|
|
|
|
title = `${ type }: ${ pullRequest.title }`;
|
|
|
|
} else {
|
|
|
|
title = `${ type }: ${ title }`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
title = `${ type }: ${ pullRequest.title }`;
|
|
|
|
}
|
2020-01-28 13:47:26 +00:00
|
|
|
return `- ${ title } [#${ pullRequest.number }](${ pullRequest.url })${ devNote } ${ authorTag }`;
|
2020-01-13 15:06:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
authedGraphql,
|
|
|
|
getEntry,
|
|
|
|
};
|