From 7f726ebf503ee1a9c8b76b891f795205e0537ca2 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Mon, 14 Oct 2024 10:55:05 +0200 Subject: [PATCH 1/3] Extract analysing logic --- src/index.ts | 34 +++------------------- src/utils/analyser/pr_labels.ts | 51 +++++++++++++++++++++++++++++++++ src/utils/types.ts | 8 +++++- 3 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 src/utils/analyser/pr_labels.ts diff --git a/src/index.ts b/src/index.ts index 497b4b4..b6b9228 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import type { Change, CommandContext, HookContext } from './utils/types'; import { extractVersionFromCommitMessage, getNextVersionFromLabels } from './utils/change'; import { getReleaseOptions } from './utils/pr'; import { Forge } from './forges/forge'; +import { PRLabelAnalyser } from './utils/analyser/pr_labels'; export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge; config: Config }) { if (config.ci.debug) { @@ -138,37 +139,10 @@ export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge const useVersionPrefixV = config.user.useVersionPrefixV === undefined ? latestTag.startsWith('v') : config.user.useVersionPrefixV; const latestVersion = latestTag.replace(/^v/, ''); - const changes: Change[] = []; - for await (const commit of unTaggedCommits.all) { - if (commit.message.startsWith(config.ci.releasePrefix)) { - continue; - } - - const pr = await forge.getPullRequestFromCommit({ - owner: config.ci.repoOwner!, - repo: config.ci.repoName!, - commitHash: commit.hash, - }); - - if (config.user.skipCommitsWithoutPullRequest && !pr) { - console.log(c.yellow('# No pull-request found for commit, skipping.'), `${commit.hash}: "${commit.message}"`); - continue; - } - - if (pr?.labels.some((l) => config.user.skipLabels?.includes(l))) { - console.log(c.yellow('# Skipping commit / PR by label:'), `${commit.hash}: "${commit.message}"`); - continue; - } - - changes.push({ - commitHash: commit.hash, - author: pr?.author || commit.author_name, - title: pr?.title || commit.message, - labels: pr?.labels || [], - pullRequestNumber: pr?.number, - }); - } + // TODO: support additional analysers + const analyser = new PRLabelAnalyser(forge, config); + const changes = await analyser.getChangesFromCommits([...unTaggedCommits.all]); if (config.ci.debug) { console.log(c.yellow('changes'), changes); diff --git a/src/utils/analyser/pr_labels.ts b/src/utils/analyser/pr_labels.ts new file mode 100644 index 0000000..3022551 --- /dev/null +++ b/src/utils/analyser/pr_labels.ts @@ -0,0 +1,51 @@ +import c from 'picocolors'; + +import { Forge } from '../../forges/forge'; +import { Config } from '../config'; +import { Analyser, Change, Commit } from '../types'; + +export class PRLabelAnalyser implements Analyser { + forge: Forge; + config: Config; + + constructor(forge: Forge, config: Config) { + this.forge = forge; + this.config = config; + } + + async getChangesFromCommits(commits: Commit[]): Promise { + const changes: Change[] = []; + + for await (const commit of commits) { + if (commit.message.startsWith(this.config.ci.releasePrefix)) { + continue; + } + + const pr = await this.forge.getPullRequestFromCommit({ + owner: this.config.ci.repoOwner!, + repo: this.config.ci.repoName!, + commitHash: commit.hash, + }); + + if (this.config.user.skipCommitsWithoutPullRequest && !pr) { + console.log(c.yellow('# No pull-request found for commit, skipping.'), `${commit.hash}: "${commit.message}"`); + continue; + } + + if (pr?.labels.some((l) => this.config.user.skipLabels?.includes(l))) { + console.log(c.yellow('# Skipping commit / PR by label:'), `${commit.hash}: "${commit.message}"`); + continue; + } + + changes.push({ + commitHash: commit.hash, + author: pr?.author || commit.author_name, + title: pr?.title || commit.message, + labels: pr?.labels || [], + pullRequestNumber: pr?.number, + }); + } + + return changes; + } +} diff --git a/src/utils/types.ts b/src/utils/types.ts index 7018288..292c495 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,7 +1,7 @@ import type { ExecFunction } from 'shelljs'; import type { Forge } from '../forges/forge'; import type { Config } from './config'; -import type { SimpleGit } from 'simple-git'; +import type { DefaultLogFields, LogResult, SimpleGit } from 'simple-git'; export type PromiseOrValue = Promise | T; @@ -111,3 +111,9 @@ export type UserConfig = Partial<{ }>; export const defineConfig = (config: UserConfig) => config; + +export type Commit = LogResult['all'][0]; + +export type Analyser = { + getChangesFromCommits(commits: Commit[]): Promise; +}; From 2036240c15d321490eda354131e6c61ee0ad2a55 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Fri, 22 Nov 2024 12:25:12 +0100 Subject: [PATCH 2/3] rm unused imports --- src/forges/github.ts | 2 +- src/index.ts | 2 +- tsconfig.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/forges/github.ts b/src/forges/github.ts index 1f1ecee..73a7385 100644 --- a/src/forges/github.ts +++ b/src/forges/github.ts @@ -1,4 +1,4 @@ -import { Comment, Forge, PullRequest } from './forge'; +import { Forge, PullRequest } from './forge'; import { Octokit } from '@octokit/rest'; export class GithubForge extends Forge { diff --git a/src/index.ts b/src/index.ts index 9e289e1..212a8dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import semver from 'semver'; import { prepare } from './cmd/prepare'; import { release } from './cmd/release'; import type { Config } from './utils/config'; -import type { Change, CommandContext, HookContext } from './utils/types'; +import type { CommandContext, HookContext } from './utils/types'; import { extractVersionFromCommitMessage, getNextVersionFromLabels } from './utils/change'; import { getReleaseOptions } from './utils/pr'; import { Forge } from './forges/forge'; diff --git a/tsconfig.json b/tsconfig.json index 106c5b2..03f85b0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "moduleResolution": "Bundler", "strict": true, "esModuleInterop": true, - "skipLibCheck": true + "skipLibCheck": true, + "noUnusedLocals": true } } From d22cbab1a19fcb8b8fb772e2b6f0399de69e3cd2 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Sun, 24 Nov 2024 13:00:58 +0100 Subject: [PATCH 3/3] fix import --- src/index.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 0b12c8d..907167b 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -5,7 +5,6 @@ import { GithubForge } from './forges/github'; import { SimpleGit, simpleGit } from 'simple-git'; import { prepare } from './cmd/prepare'; -import { release } from './cmd/release'; const config: Config = { ci: {