Skip to content

Commit

Permalink
Tag Filters: GitHub Release Task (#10703)
Browse files Browse the repository at this point in the history
Enhancements for tags: User defines the tag pattern for which the release should be created.

When tagSource is auto and tagPattern is specified,

In case of manual trigger, Find a tag matching the tagPattern at the target. If there are multiple matching tags or no matching tags, Do not create a release.
If the trigger is a commit push, Find a tag on that commit matching the tag Patter, If there are multiple matching tags or no matching tags, Do not create a release.
If the trigger is a tag push, Check if that tag matches the given pattern. If yes, create a release, else do not.
Adding a new optional parameter tagPattern to getTagForCommitTarget and _getTagForCommit.
If the tagPattern is specified, tags are filtered based on the tagPattern too in addition to the existing filters.
  • Loading branch information
TheLayman authored Jun 24, 2019
1 parent b9fb9c8 commit afaa36d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion Tasks/GitHubReleaseV0/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Tasks/GitHubReleaseV0/operations/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
25 changes: 20 additions & 5 deletions Tasks/GitHubReleaseV0/operations/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string> {
public async getTagForCommitTarget(githubEndpointToken: string, repositoryName: string, target: string, tagPattern: string = null): Promise<string> {
console.log(tl.loc("FetchTagForTarget", target));
let tag = undefined;

Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -238,15 +243,25 @@ 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
* @param githubEndpointToken
* @param repositoryName
* @param commit_sha
*/
private async _getTagForCommit(githubEndpointToken: string, repositoryName: string, commit_sha: string): Promise<string> {
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<string> {
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];
}
Expand Down
5 changes: 5 additions & 0 deletions Tasks/GitHubReleaseV0/operations/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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="(.*)"$');
Expand Down
10 changes: 9 additions & 1 deletion Tasks/GitHubReleaseV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"preview": true,
"version": {
"Major": 0,
"Minor": 153,
"Minor": 155,
"Patch": 0
},
"demands": [],
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion Tasks/GitHubReleaseV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"preview": true,
"version": {
"Major": 0,
"Minor": 153,
"Minor": 155,
"Patch": 0
},
"demands": [],
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit afaa36d

Please sign in to comment.