diff --git a/Tasks/GitHubReleaseV0/Strings/resources.resjson/en-US/resources.resjson b/Tasks/GitHubReleaseV0/Strings/resources.resjson/en-US/resources.resjson index bc0130fc3508..a1c2fa72acd0 100644 --- a/Tasks/GitHubReleaseV0/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/GitHubReleaseV0/Strings/resources.resjson/en-US/resources.resjson @@ -13,6 +13,8 @@ "loc.input.help.target": "Specify the commit SHA for which the GitHub release will be created. E.g. `48b11d8d6e92a22e3e9563a3f643699c16fd6e27`. You can also use a variable here. E.g. `$(myCommitSHA)`.", "loc.input.label.tagSource": "Tag source", "loc.input.help.tagSource": "Specify the tag to be used for release creation. The 'Git tag' option automatically takes the tag which is associated with the Git commit. Use the 'User specified tag' option to manually provide a tag.", + "loc.input.label.tagPattern": "Tag Pattern", + "loc.input.help.tagPattern": "Specify the regex for tag to be used for release creation.", "loc.input.label.tag": "Tag", "loc.input.help.tag": "Specify the tag for which to create, edit, or delete a release. You can also use a variable here. E.g. `$(myTagName)`.", "loc.input.label.title": "Release title", diff --git a/Tasks/GitHubReleaseV0/main.ts b/Tasks/GitHubReleaseV0/main.ts index 394dce5165e2..127706a3befe 100644 --- a/Tasks/GitHubReleaseV0/main.ts +++ b/Tasks/GitHubReleaseV0/main.ts @@ -46,9 +46,12 @@ class Main { const githubReleaseAssetInputPatterns = tl.getDelimitedInput(Inputs.assets, Delimiters.newLine); if (action === ActionType.create) { + //Get task inputs specific to create release + const tagPattern = tl.getInput(Inputs.tagPattern) || undefined; + // Get tag to create release if tag source is gitTag/auto if (Utility.isTagSourceAuto(tagSource)) { - tag = await helper.getTagForCommitTarget(githubEndpointToken, repositoryName, target); + tag = await helper.getTagForCommitTarget(githubEndpointToken, repositoryName, target, tagPattern); } if (!!tag) { diff --git a/Tasks/GitHubReleaseV0/operations/Constants.ts b/Tasks/GitHubReleaseV0/operations/Constants.ts index 00b0028d9a40..43a876eb3492 100644 --- a/Tasks/GitHubReleaseV0/operations/Constants.ts +++ b/Tasks/GitHubReleaseV0/operations/Constants.ts @@ -16,4 +16,5 @@ export class Inputs { public static readonly releaseNotes = "releaseNotes"; public static readonly addChangeLog = "addChangeLog"; public static readonly deleteExistingAssets = "deleteExistingAssets"; + public static readonly tagPattern = "tagPattern"; } \ No newline at end of file diff --git a/Tasks/GitHubReleaseV0/operations/Helper.ts b/Tasks/GitHubReleaseV0/operations/Helper.ts index 8a31ac529a2b..e9ea627f8bc9 100644 --- a/Tasks/GitHubReleaseV0/operations/Helper.ts +++ b/Tasks/GitHubReleaseV0/operations/Helper.ts @@ -24,6 +24,7 @@ export class Helper { /** * Returns tag name to be used for creating a release. + * If tagPattern is specified, returns tag matching the given pattern * If user has specified tag, then use it * else if $(Build.SourceBranch) is referencing to a tag, then parse tag name and use it * else fetch tag from the target specified by user @@ -34,8 +35,9 @@ export class Helper { * @param repositoryName * @param target * @param tag + * @param tagPattern */ - public async getTagForCommitTarget(githubEndpointToken: string, repositoryName: string, target: string): Promise { + public async getTagForCommitTarget(githubEndpointToken: string, repositoryName: string, target: string, tagPattern: string = null): Promise { console.log(tl.loc("FetchTagForTarget", target)); let tag = undefined; @@ -45,7 +47,7 @@ export class Helper { // If the buildSourceVersion and user specified target does not match, then prefer user specified target if (commit_sha !== buildSourceVersion) { - tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha); + tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha, tagPattern); } else { let buildSourceBranch = tl.getVariable(AzureDevOpsVariables.buildSourceBranch); @@ -55,9 +57,12 @@ export class Helper { // Else fetch tag from commit if (!!normalizedBranch) { tag = normalizedBranch; + if (!!tagPattern && !Utility.isTagMatching(tag, tagPattern)) { + tag = null; + } } else { - tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha); + tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha, tagPattern); } } @@ -238,6 +243,7 @@ export class Helper { /** * Returns tag name associated with the commit. + * If tagPattern is specified returns tag name * If 0 tag found return undefined * else if 1 tag found return tag name * else throw error @@ -245,8 +251,17 @@ export class Helper { * @param repositoryName * @param commit_sha */ - private async _getTagForCommit(githubEndpointToken: string, repositoryName: string, commit_sha: string): Promise { - let filteredTag: any = await this.filterTag(githubEndpointToken, repositoryName, commit_sha, this._filterTagsByCommitSha); + private async _getTagForCommit(githubEndpointToken: string, repositoryName: string, commit_sha: string, tagPattern: string = null): Promise { + let filteredTag: any; + let filterTagsCallback = (tagsList: any[], commit_sha: string): any[] => { + tagsList = this._filterTagsByCommitSha(tagsList, commit_sha); + if (!tagPattern) { + return tagsList; + } + return tagsList.filter((tag: any) => Utility.isTagMatching(tag[GitHubAttributes.nameAttribute], tagPattern)); + } + + filteredTag = await this.filterTag(githubEndpointToken, repositoryName, commit_sha, filterTagsCallback); return filteredTag && filteredTag[GitHubAttributes.nameAttribute]; } diff --git a/Tasks/GitHubReleaseV0/operations/Utility.ts b/Tasks/GitHubReleaseV0/operations/Utility.ts index 8b56bae67d37..ea22c348adc4 100644 --- a/Tasks/GitHubReleaseV0/operations/Utility.ts +++ b/Tasks/GitHubReleaseV0/operations/Utility.ts @@ -239,6 +239,11 @@ export class Utility { } + public static isTagMatching(tag: string, tagPattern: string): boolean { + let tagPatternRegex = new RegExp("^" + tagPattern + "$"); + return tagPatternRegex.test(tag); + } + private static readonly _onlyFirstLine = new RegExp("^.*$", "m"); private static readonly _githubPaginatedLinkRegex = new RegExp("^<(.*)>$"); private static readonly _githubPaginatedRelRegex = new RegExp('^rel="(.*)"$'); diff --git a/Tasks/GitHubReleaseV0/task.json b/Tasks/GitHubReleaseV0/task.json index febe3408ac7e..8c5fb6e46652 100644 --- a/Tasks/GitHubReleaseV0/task.json +++ b/Tasks/GitHubReleaseV0/task.json @@ -14,7 +14,7 @@ "preview": true, "version": { "Major": 0, - "Minor": 153, + "Minor": 155, "Patch": 0 }, "demands": [], @@ -75,6 +75,14 @@ "manual": "User specified tag" } }, + { + "name": "tagPattern", + "type": "string", + "label": "Tag Pattern", + "required": false, + "visibleRule": "tagSource = auto", + "helpMarkDown": "Specify the regex for tag to be used for release creation." + }, { "name": "tag", "type": "string", diff --git a/Tasks/GitHubReleaseV0/task.loc.json b/Tasks/GitHubReleaseV0/task.loc.json index f143c7c104dd..a77b1df81fd9 100644 --- a/Tasks/GitHubReleaseV0/task.loc.json +++ b/Tasks/GitHubReleaseV0/task.loc.json @@ -14,7 +14,7 @@ "preview": true, "version": { "Major": 0, - "Minor": 153, + "Minor": 155, "Patch": 0 }, "demands": [], @@ -75,6 +75,14 @@ "manual": "User specified tag" } }, + { + "name": "tagPattern", + "type": "string", + "label": "ms-resource:loc.input.label.tagPattern", + "required": false, + "visibleRule": "tagSource = auto", + "helpMarkDown": "ms-resource:loc.input.help.tagPattern" + }, { "name": "tag", "type": "string",