Skip to content

Commit

Permalink
Fix generator to work on repos without releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Americas authored and satazor committed Mar 18, 2022
1 parent 4f32be1 commit d8c4d5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/changelog-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ class ChangelogFetcher {
}
tagName
}
createdAt
}
}`;
const result = await this.client(query, { owner: this.owner, repo: this.repo });

// Extract release from nested result.
const release = result.repository.latestRelease;

// For shiny new repositories without releases, use the repository creation date instead.
if (!release) {
return { tagCommit: { committedDate: moment.utc(result.repository.createdAt) } };
}

// Transform string timestamp into moment.
release.tagCommit.committedDate = moment.utc(release.tagCommit.committedDate);

Expand Down
27 changes: 27 additions & 0 deletions test/changelog-fetcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('ChangelogFetcher', () => {
return {
data: {
repository: {
createdAt: moment('2017-10-22T12').toISOString(),
latestRelease: {
tagCommit: {
committedDate: moment('2018-10-22T12').toISOString()
Expand Down Expand Up @@ -653,6 +654,32 @@ describe('ChangelogFetcher', () => {
tagName: 'latestRelease'
});
});

it('should return a mocked latest release if the repository has no releases', async () => {
const fetcher = new ChangelogFetcher({
owner: 'biz',
repo: 'buz',
token: 'qux'
});

nock('https://api.github.com')
.post('/graphql')
.reply(200, (_, requestBody) => {
const mockData = getDataForRequest(requestBody);

mockData.data.repository.latestRelease = undefined;

return mockData;
});

const result = await fetcher.getLatestRelease();

expect(result).toEqual({
tagCommit: {
committedDate: moment.utc(moment('2017-10-22T12').toISOString())
}
});
});
});

describe('getRepositoryCreatedAt()', () => {
Expand Down

0 comments on commit d8c4d5c

Please sign in to comment.