-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (168 loc) · 5.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const { InMemoryCache } = require( 'apollo-cache-inmemory' );
const { ApolloClient }= require( 'apollo-client' );
const { onError } = require( 'apollo-link-error' );
const { createHttpLink } = require( 'apollo-link-http' );
const { gql } = require( 'apollo-server' );
const fetch = require( 'node-fetch' );
const { program } = require('commander');
const settings = require('dotenv').config().parsed;
async function getGraphQLClient() {
const GITHUB_GRAPHQL_API_URI = 'https://api.github.com/graphql';
// TODO could fail authenticating, so we need to add a custom error to ApolloClient
// We can pass a error handler to the constructor of ApolloClient
const httpLink = createHttpLink( {
uri: GITHUB_GRAPHQL_API_URI,
headers: {
Authorization: `Bearer ${ settings.GITHUB_API_TOKEN }`,
// Fallback UA intentionally uses a really old version number to distinguish
// it from the real values set in the env vars so we know where it's coming from
//'User-Agent': process.env.HTTP_USER_AGENT || 'wpcomvip-parker/0.0.1',
},
fetch,
} );
const errorLink = onError( ( { networkError, graphQLErrors } ) => {
if ( graphQLErrors ) {
graphQLErrors.forEach( ( { message, locations, path } ) =>
console.log(
`[GraphQL error]: Message: ${ message }, Location: ${ locations }, Path: ${ path }`,
),
);
}
if ( networkError ) {
console.log( "Network Error Response:" );
console.log( networkError.name );
console.log( networkError.result );
console.log( networkError.response.url );
console.log( networkError.response.status );
console.log( networkError.response.statusText );
const errorDetails = {
status: networkError.statusCode,
statusText: networkError.statusText || '',
};
console.log( "Error details:" );
console.log ( errorDetails );
}
} );
const link = errorLink.concat( httpLink );
return new ApolloClient( {
link,
// Apollo 2.x requires one...need to look further if it causes data consistency issues
cache: new InMemoryCache(),
} );
}
async function getReleaseDate( owner, repo, releaseTag ) {
const index = gql`
query GetReleaseDate($releaseTag: String,$owner: String!, $repo: String!)
{
repository(owner: $owner, name: $repo) {
object(expression: $releaseTag) {
... on Commit {
oid
messageHeadline
committedDate
author {
user {
login
}
}
}
}
}
}`;
const client = await getGraphQLClient();
try {
const result = await client.query( {
query: index,
variables: {
owner,
repo,
releaseTag,
},
} );
if( ! result.data.repository.object.committedDate ) {
return console.log( 'Release date not found' );
}
return result.data.repository.object.committedDate;
} catch ( e ) {
console.log( `Error querying GitHub GraphQL API ${ JSON.stringify( e ) }` );
}
}
async function listMergedPRs( owner, repo, startDate, endDate ) {
const queryString = `repo:${ owner }/${ repo } is:pr is:merged merged:${ startDate }..${ endDate }`;
const index = gql`query ListMergedSince($queryString: String!){
search(first: 100, query: $queryString, type: ISSUE) {
nodes {
... on PullRequest {
title
url
author {
login
}
}
}
}
}`;
const client = await getGraphQLClient();
try {
const result = await client.query( {
query: index,
variables: {
queryString,
},
} );
if( ! result.data.search.nodes.length ) {
return;
}
return result.data.search.nodes;
} catch ( e ) {
console.log( `Error querying GitHub GraphQL API ${ JSON.stringify( e ) }` );
}
}
program
.requiredOption( '-o, --owner <repoOwner>', 'Owner of the repo to extract PRs from' )
.requiredOption( '-r, --repo <repo>', 'Name of the repository' )
.requiredOption( '-p, --previous-release <previousTag>', 'Release tag of the previous release')
.option( '-c, --current-release <currentTag>', 'Release tag of the release to compare to. Leave empty to retrieve up to the latest merged PR' );
program.parse( process.argv );
const options = program.opts();
async function main() {
if (settings.error) {
return console.error( settings.error );
}
if ( ! settings.GITHUB_API_TOKEN ) {
return console.error( 'Please provide a valid GitHub API token through the .env file. See .env.example.' );
}
const startDate = await getReleaseDate( options.owner, options.repo, options.previousRelease );
let endDate;
if ( options.currentRelease ) {
endDate = await getReleaseDate( options.currentRelease );
} else {
endDate = new Date().toISOString().split('T')[0];
}
const mergedPRs = await listMergedPRs( options.owner, options.repo, startDate, endDate );
if ( ! mergedPRs || ! mergedPRs.length ) {
return console.error( `No merged PRs found between ${ startDate } and ${ endDate }` );
}
console.log();
const updatesList = [];
const contributionsList = [];
for ( const pr of mergedPRs ) {
const id = pr.url.substring( pr.url.lastIndexOf( '/' ) + 1 );
const PRString = `- ${ pr.title } ( #${ id } ) by ${ pr.author.login }`;
if ( settings.DEPENDENCY_AUTHORS && settings.DEPENDENCY_AUTHORS.includes( pr.author.login ) ) {
updatesList.push( PRString );
} else {
contributionsList.push( PRString );
}
}
// Print contributions
console.log( '## New and Fixes' );
console.log();
contributionsList.map( c => { return console.log( c ); } );
console.log();
// Print updates
console.log( '## Updates' );
console.log();
updatesList.map( c => { return console.log( c ); } );
}
main();