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 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
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;
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";
}
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