Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate pr-labels to changes code into an analyser #237

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/forges/github.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
36 changes: 5 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ 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';
import { PRLabelAnalyser } from './utils/analyser/pr_labels';

export async function run({ git, forge, config }: { git: SimpleGit; forge: Forge; config: Config }) {
if (config.ci.debug) {
Expand Down Expand Up @@ -143,37 +144,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);
Expand Down
51 changes: 51 additions & 0 deletions src/utils/analyser/pr_labels.ts
Original file line number Diff line number Diff line change
@@ -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<Change[]> {
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;
}
}
8 changes: 7 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -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<T> = Promise<T> | T;

Expand Down Expand Up @@ -111,3 +111,9 @@ export type UserConfig = Partial<{
}>;

export const defineConfig = (config: UserConfig) => config;

export type Commit = LogResult<DefaultLogFields>['all'][0];

export type Analyser = {
getChangesFromCommits(commits: Commit[]): Promise<Change[]>;
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"moduleResolution": "Bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"noUnusedLocals": true
}
}