|
| 1 | +import { Octokit } from '@octokit/rest' |
| 2 | +import { parseHostedGitUrl } from '@simple-libs/hosted-git-info' |
| 3 | +import { |
| 4 | + type GenericReleaseCreatorOptions, |
| 5 | + GenericReleaseCreator |
| 6 | +} from '@simple-release/core' |
| 7 | + |
| 8 | +export interface GithubReleaseCreatorOptions { |
| 9 | + /** |
| 10 | + * The GitHub personal access token to authenticate with the GitHub API. |
| 11 | + */ |
| 12 | + token: string |
| 13 | + /** |
| 14 | + * The GitHub owner (username or organization) of the repository. |
| 15 | + * If not provided, it will be inferred from the remote URL. |
| 16 | + */ |
| 17 | + owner?: string |
| 18 | + /** |
| 19 | + * The GitHub project (repository name) to create releases in. |
| 20 | + * If not provided, it will be inferred from the remote URL. |
| 21 | + */ |
| 22 | + project?: string |
| 23 | +} |
| 24 | + |
| 25 | +const OK = 200 |
| 26 | +const noop = () => { /* no-op */ } |
| 27 | + |
| 28 | +/** |
| 29 | + * A class that creates releases on GitHub using the GitHub REST API. |
| 30 | + */ |
| 31 | +export class GithubReleaseCreator extends GenericReleaseCreator { |
| 32 | + readonly octokit: Octokit |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new instance of the GitHub release creator. |
| 36 | + * @param options - The options for the GitHub release creator. |
| 37 | + */ |
| 38 | + constructor( |
| 39 | + private readonly options: GithubReleaseCreatorOptions |
| 40 | + ) { |
| 41 | + super() |
| 42 | + |
| 43 | + this.octokit = new Octokit({ |
| 44 | + auth: options.token, |
| 45 | + log: { |
| 46 | + debug: noop, |
| 47 | + info: noop, |
| 48 | + warn: noop, |
| 49 | + error: noop |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + private async hasRelease( |
| 55 | + owner: string, |
| 56 | + project: string, |
| 57 | + tag: string |
| 58 | + ): Promise<boolean> { |
| 59 | + try { |
| 60 | + const { status } = await this.octokit.rest.repos.getReleaseByTag({ |
| 61 | + owner, |
| 62 | + repo: project, |
| 63 | + tag |
| 64 | + }) |
| 65 | + |
| 66 | + return status === OK |
| 67 | + } catch { |
| 68 | + return false |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + async create(options: GenericReleaseCreatorOptions): Promise<void> { |
| 73 | + const { |
| 74 | + project, |
| 75 | + dryRun, |
| 76 | + logger |
| 77 | + } = options |
| 78 | + let { |
| 79 | + owner: githubOwner, |
| 80 | + project: githubProject |
| 81 | + } = this.options |
| 82 | + const { octokit } = this |
| 83 | + const data = await project.getReleaseData() |
| 84 | + |
| 85 | + if (!githubOwner || !githubProject) { |
| 86 | + const remote = await project.gitClient.getConfig('remote.origin.url') |
| 87 | + const repo = parseHostedGitUrl(remote) |
| 88 | + |
| 89 | + if (repo) { |
| 90 | + if (!githubOwner) { |
| 91 | + githubOwner = repo.owner |
| 92 | + } |
| 93 | + |
| 94 | + if (!githubProject) { |
| 95 | + githubProject = repo.project |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + for (const releaseData of data) { |
| 101 | + if (await this.hasRelease(githubOwner!, githubProject!, releaseData.nextTag)) { |
| 102 | + logger?.verbose(`Release ${releaseData.nextTag} already exists.`) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + const params = { |
| 107 | + owner: githubOwner!, |
| 108 | + repo: githubProject!, |
| 109 | + tag_name: releaseData.nextTag, |
| 110 | + name: releaseData.title, |
| 111 | + body: releaseData.notes, |
| 112 | + prerelease: releaseData.isPrerelease |
| 113 | + } |
| 114 | + |
| 115 | + logger?.verbose(`Creating release with params:\n${JSON.stringify(params, null, 2)}`) |
| 116 | + |
| 117 | + if (!dryRun) { |
| 118 | + await octokit.rest.repos.createRelease(params) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments