-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(internal): refactor changelogs (#6878)
- Loading branch information
Showing
5 changed files
with
214 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import changelogFilenameRegex from 'changelog-filename-regex'; | ||
import { logger } from '../../../../logger'; | ||
import { GithubGitBlob } from '../../../../types/platform/github'; | ||
import { GithubHttp } from '../../../../util/http/github'; | ||
import { ensureTrailingSlash } from '../../../../util/url'; | ||
import { ChangeLogFile, ChangeLogNotes } from '../common'; | ||
|
||
const http = new GithubHttp(); | ||
|
||
export async function getTags( | ||
endpoint: string, | ||
repository: string | ||
): Promise<string[]> { | ||
logger.trace('github.getTags()'); | ||
const url = `${endpoint}repos/${repository}/tags?per_page=100`; | ||
try { | ||
const res = await http.getJson<{ name: string }[]>(url, { | ||
paginate: true, | ||
}); | ||
|
||
const tags = res.body; | ||
|
||
if (!tags.length) { | ||
logger.debug({ repository }, 'repository has no Github tags'); | ||
} | ||
|
||
return tags.map((tag) => tag.name).filter(Boolean); | ||
} catch (err) { | ||
logger.debug({ sourceRepo: repository }, 'Failed to fetch Github tags'); | ||
logger.debug({ err }); | ||
// istanbul ignore if | ||
if (err.message && err.message.includes('Bad credentials')) { | ||
logger.warn('Bad credentials triggering tag fail lookup in changelog'); | ||
throw err; | ||
} | ||
return []; | ||
} | ||
} | ||
|
||
export async function getReleaseNotesMd( | ||
repository: string, | ||
apiBaseUrl: string | ||
): Promise<ChangeLogFile> | null { | ||
logger.trace('github.getReleaseNotesMd()'); | ||
const apiPrefix = `${ensureTrailingSlash(apiBaseUrl)}repos/${repository}`; | ||
|
||
const res = await http.getJson<{ name: string }[]>(`${apiPrefix}/contents/`); | ||
|
||
const files = res.body.filter((f) => changelogFilenameRegex.test(f.name)); | ||
|
||
if (!files.length) { | ||
logger.trace('no changelog file found'); | ||
return null; | ||
} | ||
const { name: changelogFile } = files.shift(); | ||
/* istanbul ignore if */ | ||
if (files.length > 1) { | ||
logger.debug( | ||
`Multiple candidates for changelog file, using ${changelogFile}` | ||
); | ||
} | ||
|
||
const fileRes = await http.getJson<GithubGitBlob>( | ||
`${apiPrefix}/contents/${changelogFile}` | ||
); | ||
|
||
const changelogMd = | ||
Buffer.from(fileRes.body.content, 'base64').toString() + '\n#\n##'; | ||
return { changelogFile, changelogMd }; | ||
} | ||
|
||
export async function getReleaseList( | ||
apiBaseUrl: string, | ||
repository: string | ||
): Promise<ChangeLogNotes[]> { | ||
logger.trace('github.getReleaseList()'); | ||
const url = `${ensureTrailingSlash( | ||
apiBaseUrl | ||
)}repos/${repository}/releases?per_page=100`; | ||
const res = await http.getJson< | ||
{ | ||
html_url: string; | ||
id: number; | ||
tag_name: string; | ||
name: string; | ||
body: string; | ||
}[] | ||
>(url); | ||
return res.body.map((release) => ({ | ||
url: release.html_url, | ||
id: release.id, | ||
tag: release.tag_name, | ||
name: release.name, | ||
body: release.body, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import changelogFilenameRegex from 'changelog-filename-regex'; | ||
import { logger } from '../../../../logger'; | ||
import { GitlabTreeNode } from '../../../../types/platform/gitlab'; | ||
import { GitlabHttp } from '../../../../util/http/gitlab'; | ||
import { ensureTrailingSlash } from '../../../../util/url'; | ||
import { ChangeLogFile, ChangeLogNotes } from '../common'; | ||
|
||
const http = new GitlabHttp(); | ||
|
||
function getRepoId(repository: string): string { | ||
return repository.replace(/\//g, '%2f'); | ||
} | ||
|
||
export async function getTags( | ||
endpoint: string, | ||
repository: string | ||
): Promise<string[]> { | ||
logger.trace('gitlab.getTags()'); | ||
const url = `${ensureTrailingSlash(endpoint)}projects/${getRepoId( | ||
repository | ||
)}/repository/tags`; | ||
try { | ||
const res = await http.getJson<{ name: string }[]>(url); | ||
|
||
const tags = res.body; | ||
|
||
if (!tags.length) { | ||
logger.debug({ sourceRepo: repository }, 'repository has no Gitlab tags'); | ||
} | ||
|
||
return tags.map((tag) => tag.name).filter(Boolean); | ||
} catch (err) { | ||
logger.info({ sourceRepo: repository }, 'Failed to fetch Gitlab tags'); | ||
// istanbul ignore if | ||
if (err.message && err.message.includes('Bad credentials')) { | ||
logger.warn('Bad credentials triggering tag fail lookup in changelog'); | ||
throw err; | ||
} | ||
return []; | ||
} | ||
} | ||
|
||
export async function getReleaseNotesMd( | ||
repository: string, | ||
apiBaseUrl: string | ||
): Promise<ChangeLogFile> | null { | ||
logger.trace('gitlab.getReleaseNotesMd()'); | ||
const apiPrefix = `${ensureTrailingSlash( | ||
apiBaseUrl | ||
)}projects/${repository}/repository/`; | ||
|
||
// https://docs.gitlab.com/13.2/ee/api/repositories.html#list-repository-tree | ||
let files = (await http.getJson<GitlabTreeNode[]>(`${apiPrefix}tree/`)).body; | ||
|
||
files = files.filter((f) => changelogFilenameRegex.test(f.name)); | ||
if (!files.length) { | ||
logger.trace('no changelog file found'); | ||
return null; | ||
} | ||
const { name: changelogFile } = files.shift(); | ||
/* istanbul ignore if */ | ||
if (files.length > 1) { | ||
logger.debug( | ||
`Multiple candidates for changelog file, using ${changelogFile}` | ||
); | ||
} | ||
|
||
const fileRes = await http.getJson<{ content: string }>( | ||
`${apiPrefix}files/${changelogFile}?ref=master` | ||
); | ||
const changelogMd = | ||
Buffer.from(fileRes.body.content, 'base64').toString() + '\n#\n##'; | ||
return { changelogFile, changelogMd }; | ||
} | ||
|
||
export async function getReleaseList( | ||
apiBaseUrl: string, | ||
repository: string | ||
): Promise<ChangeLogNotes[]> { | ||
logger.trace('gitlab.getReleaseNotesMd()'); | ||
|
||
const repoId = getRepoId(repository); | ||
const apiUrl = `${ensureTrailingSlash( | ||
apiBaseUrl | ||
)}projects/${repoId}/releases`; | ||
const res = await http.getJson< | ||
{ | ||
name: string; | ||
release: string; | ||
description: string; | ||
tag_name: string; | ||
}[] | ||
>(`${apiUrl}?per_page=100`, { | ||
paginate: true, | ||
}); | ||
return res.body.map((release) => ({ | ||
url: `${apiUrl}/${release.tag_name}`, | ||
name: release.name, | ||
body: release.description, | ||
tag: release.tag_name, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.