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

from-issue: improve parseIssue #27945

Merged
merged 1 commit into from
Nov 20, 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
12 changes: 7 additions & 5 deletions .blueprint/from-issue/command.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ const command: JHipsterCommandDefinition = {
},
configure(gen: any) {
// Gets the owner, repo and issue_number from a string such as, "jhipster/generator-jhipster#12345"
const parsedIssue = parseIssue(gen.issue);
if (parsedIssue) {
gen.owner = parsedIssue.owner;
gen.repository = parsedIssue.repository;
gen.issueNumber = parsedIssue.issue;
const { owner, repository, issue } = parseIssue(gen.issue);
if (owner) {
gen.owner = owner;
}
if (repository) {
gen.repository = repository;
}
gen.issueNumber = issue;
},
scope: 'generator',
},
Expand Down
27 changes: 27 additions & 0 deletions lib/testing/github.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'esmocha';
import { parseIssue } from './github.js';

describe('github', () => {
it('should parse full issue spec', () => {
expect(parseIssue('jhipster/generator-jhipster#12345')).toEqual({
owner: 'jhipster',
repository: 'generator-jhipster',
issue: '12345',
});
});
it('should parse repo with issue', () => {
expect(parseIssue('generator-jhipster-entity-audit#12345')).toEqual({
repository: 'generator-jhipster-entity-audit',
issue: '12345',
});
});
it('should parse issue', () => {
expect(parseIssue('12345')).toEqual({ issue: '12345' });
});
it('should not parse with missing repo', () => {
expect(() => parseIssue('jhipster/12345')).toThrowError('Invalid issue format: jhipster/12345');
});
it('should not parse with invalid issue number', () => {
expect(() => parseIssue('a')).toThrowError('Invalid issue format: a');
});
});
14 changes: 8 additions & 6 deletions lib/testing/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { EOL } from 'node:os';
import process from 'node:process';
import axios from 'axios';

export const parseIssue = (issue: string): undefined | { owner: string; repository: string; issue: string } => {
if (issue.includes('#')) {
const split = issue.split('/');
const split2 = split[1].split('#');
return { owner: split[0], repository: split2[0], issue: split2[1] };
type GithubIssue = { owner: string; repository: string; issue: string };

export const parseIssue = (issue: string): GithubIssue => {
const result = /^(((?<owner>[^/]*)\/)?(?<repository>[^#]+)#)?(?<issue>\d+)$/.exec(issue);
const groups = result?.groups;
if (groups) {
return { ...groups } as GithubIssue;
}
return undefined;
throw new Error(`Invalid issue format: ${issue}`);
};

export const getGithubOutputFile = (): string | undefined => {
Expand Down
Loading