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

Tag Filters: GitHub Release Task #10703

Merged
merged 9 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
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;
TheLayman marked this conversation as resolved.
Show resolved Hide resolved

// 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";
}
29 changes: 24 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,13 @@ export class Helper {
// Else fetch tag from commit
if (!!normalizedBranch) {
tag = normalizedBranch;
if (!!tagPattern && !Utility.isTagMatches(tag, tagPattern)) {
tag = null;
tl.warning(tl.loc("NoMatchingTagsFound", tagPattern));
}
}
else {
tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha);
tag = await this._getTagForCommit(githubEndpointToken, repositoryName, commit_sha, tagPattern);
}
}

Expand Down Expand Up @@ -238,16 +244,29 @@ 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;

if (!tagPattern) {
filteredTag = await this.filterTag(githubEndpointToken, repositoryName, commit_sha, this._filterTagsByCommitSha);
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
}
else {
let filterTagsbyCommitShaAndTagPattern = (tagsList: any[], commit_sha: string): any[] => {
tagsList = this._filterTagsByCommitSha(tagsList, commit_sha);
return tagsList.filter((tag: any) => Utility.isTagMatches(tag[GitHubAttributes.nameAttribute], tagPattern));
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
}

filteredTag = await this.filterTag(githubEndpointToken, repositoryName, commit_sha, filterTagsbyCommitShaAndTagPattern);
}

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 isTagMatches(tag: string, tagPattern: string): boolean {
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 11 additions & 2 deletions 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": 154,
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
"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 Expand Up @@ -261,6 +269,7 @@
"TagRequiredCreateAction": "Tag source is set to manual- please specify a tag for create action. For yaml syntax see: https://aka.ms/AA3m1bq",
"NoFileFoundMatchingPattern": "No files found matching '%s'. Nothing to upload.",
"PatternIsADirectory": "'%s' cannot be uploaded as it is a directory. Please specify a file.",
"SearchingFileMatchingPattern": "Searching for file(s) matching '%s'."
"SearchingFileMatchingPattern": "Searching for file(s) matching '%s'.",
"NoMatchingTagsFound": "No Matching tags found for given regex: %s Unable to perform the action."
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
TheLayman marked this conversation as resolved.
Show resolved Hide resolved
}
}