Skip to content

Commit

Permalink
Merge pull request #21 from Spacetown/add_token_for_base_branch
Browse files Browse the repository at this point in the history
Add tokens for base branch and tail of base branch
  • Loading branch information
BlueBasher authored May 9, 2024
2 parents 632b090 + c792a72 commit c0a3957
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/branch-creator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class BranchCreator {

const repository = await gitRestClient.getRepository(repositoryId, project.name);

const branchName = await this.getBranchName(workItemTrackingRestClient, settingsDocument, workItemId, project.name);
const branchName = await this.getBranchName(workItemTrackingRestClient, settingsDocument, workItemId, project.name, sourceBranchName);
const branchUrl = `${gitBaseUrl}/${repository.name}?version=GB${encodeURI(branchName)}`;

if (await this.branchExists(gitRestClient, repositoryId, project.name, branchName)) {
Expand Down Expand Up @@ -55,7 +55,7 @@ export class BranchCreator {
navigationService.openNewWindow(branchUrl, "");
}

public async getBranchName(workItemTrackingRestClient: WorkItemTrackingRestClient, settingsDocument: SettingsDocument, workItemId: number, project: string): Promise<string> {
public async getBranchName(workItemTrackingRestClient: WorkItemTrackingRestClient, settingsDocument: SettingsDocument, workItemId: number, project: string, sourceBranchName: string): Promise<string> {
const workItem = await workItemTrackingRestClient.getWorkItem(workItemId, project, undefined, undefined, WorkItemExpand.Fields);
const workItemType = workItem.fields["System.WorkItemType"];

Expand All @@ -69,7 +69,18 @@ export class BranchCreator {

let branchName = branchNameTemplate;
tokens.forEach((token) => {
let workItemFieldValue = workItem.fields[token.replace('${', '').replace('}', '')];
let workItemFieldName = token.replace('${', '').replace('}', '');
let workItemFieldValue = ""
if (workItemFieldName == "SourceBranchName") {
workItemFieldValue = sourceBranchName
}
else if (workItemFieldName == "SourceBranchNameTail") {
workItemFieldValue = sourceBranchName.replace(/.+\//, "")
}
else {
workItemFieldValue = workItem.fields[workItemFieldName];
}

if (workItemFieldValue) {
if (typeof workItemFieldValue.replace === 'function') {
workItemFieldValue = workItemFieldValue.replace(/[^a-zA-Z0-9]/g, settingsDocument.nonAlphanumericCharactersReplacement);
Expand Down
2 changes: 1 addition & 1 deletion src/branch-details-form/branch-details-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class BranchDetailsForm extends React.Component<{}, ISelectBranchDetailsState> {
const branchCreator = new BranchCreator();
let branchNames: string[] = [];
for await (const workItemId of this.state.workItems) {
const branchName = await branchCreator.getBranchName(workItemTrackingRestClient, settingsDocument, workItemId, this.state.projectName!);
const branchName = await branchCreator.getBranchName(workItemTrackingRestClient, settingsDocument, workItemId, this.state.projectName!, this.state.sourceBranchName!);
branchNames.push(branchName);
}

Expand Down
3 changes: 3 additions & 0 deletions src/branchNameTemplateValidator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class BranchNameTemplateValidator {
}

private getUnknownFields(tokens: string[], workItemFieldNames: string[]): string[] {
const allFieldNames = Object.assign([], workItemFieldNames)
allFieldNames.push("SourceBranchName")
allFieldNames.push("SourceBranchNameTail")
const fieldNames = tokens.map(token => token.replace('${', '').replace('}', ''));
return fieldNames.filter(x => workItemFieldNames.indexOf(x) === -1);
}
Expand Down

0 comments on commit c0a3957

Please sign in to comment.