Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
Support multiple artifacts from the same repo with different commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbera committed Jan 31, 2018
1 parent 5f2ca1f commit a9a0b73
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
21 changes: 12 additions & 9 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@
"sourceMaps": true,
"env": {
// This is the tag to be created
"RELEASE_RELEASENAME": "Mike - 1",
// This only supports TfsGit currently.
"RELEASE_ARTIFACTS_MASTER_BRANCH_REPOSITORY_PROVIDER": "TfsGit",
// The commit to tag
"RELEASE_ARTIFACTS_MASTER_BRANCH_SOURCEVERSION": "b950f0e25df229dee0fe7ff5ff7a6194f9580b21",
// The build id to get the repository id from. This won't be needed onece: https://github.com/Microsoft/vsts-agent/issues/976 is completed.
"RELEASE_ARTIFACTS_MASTER_BRANCH_BUILDID": "27",
"RELEASE_RELEASENAME": "Release - 1.0.20",

"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD1_REPOSITORY_PROVIDER": "TfsGit",
"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD1_SOURCEVERSION": "88c2da89b3aab9b33a2c205e5cf31f22d79b0a1a",
"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD1_REPOSITORY_ID": "4c9a5185-5d8f-4d36-935a-da19e0c4f97b",

"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD_REPOSITORY_PROVIDER": "TfsGit",
"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD_SOURCEVERSION": "1dea04b2c6a3524b53a1904c20af77b3418694cb",
"RELEASE_ARTIFACTS_SCRUMM-SAMPLE-BUILD_REPOSITORY_ID": "4c9a5185-5d8f-4d36-935a-da19e0c4f97b",

// Regex Settings
"INPUT_staticTagName": "DEV1",
"INPUT_regexFlags": "",
"INPUT_searchRegex": "",
"INPUT_replacePattern": "",
"INPUT_branchFolder": "patch/subpatch/",
"INPUT_branchFolder": "",


// Set these as an environment variables
//"ENDPOINT_AUTH_PARAMETER_SystemVssConnection_AccessToken": "<Your token>",
//"ENDPOINT_URL_SystemVssConnection": "https://<your account>.vsrm.visualstudio.com/",

"VSTS_PUBLIC_VARIABLES": "[\"RELEASE.ARTIFACTS.MASTER.BRANCH.REPOSITORY.PROVIDER\", \"RELEASE.ARTIFACTS.MASTER.BRANCH.SOURCEVERSION\", \"RELEASE.ARTIFACTS.MASTER.BRANCH.BUILDID\", \"RELEASE.RELEASENAME\"]"
"VSTS_PUBLIC_VARIABLES": "[\"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD1.REPOSITORY.PROVIDER\", \"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD1.SOURCEVERSION\", \"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD1.REPOSITORY.ID\", \"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD.REPOSITORY.PROVIDER\", \"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD.SOURCEVERSION\", \"RELEASE.ARTIFACTS.SCRUMM-SAMPLE-BUILD.REPOSITORY.ID\",\"RELEASE.RELEASENAME\"]"
}
}
]
Expand Down
74 changes: 70 additions & 4 deletions Tasks/common/GitRefCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export abstract class GitRefCreator {
let gitapi: git.IGitApi = connect.getGitApi();
let bldapi: bld.IBuildApi = connect.getBuildApi();

let artifactData: IArtifactData[] = await this.getAllGitArtifacts(bldapi);
let artifactData: IArtifactData[] = await this.getAllGitArtifacts(bldapi, gitapi);

if (artifactData.length === 0) {
tl.warning("No TfsGit artifacts found.");
Expand Down Expand Up @@ -76,7 +76,7 @@ export abstract class GitRefCreator {
return defaultValue;
}

private async getAllGitArtifacts(bldapi: bld.IBuildApi): Promise < IArtifactData[] > {
private async getAllGitArtifacts(bldapi: bld.IBuildApi, gitapi: git.IGitApi): Promise < IArtifactData[] > {
let artifactNames: IArtifactData[] = [];
let regexp: RegExp = new RegExp("RELEASE\.ARTIFACTS\.(.*)\.REPOSITORY\.PROVIDER", "gi");

Expand All @@ -102,14 +102,80 @@ export abstract class GitRefCreator {
let artifact: IArtifactData = {
"name": name,
"commit": tl.getVariable(`RELEASE.ARTIFACTS.${name}.SOURCEVERSION`),
"repositoryId": repositoryId,
"repositoryId": repositoryId.toLowerCase(),
"oldCommitId": "0000000000000000000000000000000000000000",
};

artifactNames.push(artifact);
}

return artifactNames;
return await this.filterArtifacts(gitapi, artifactNames);
}

// This is meant to fix https://github.com/jabbera/vsts-git-release-tag/issues/23
// The user is using the same repo, but 2 different commits via 2 different artifacts.
// For a given repository we should select only the most current commit.
// My kingdom for a groupby
private async filterArtifacts(gitapi: git.IGitApi, artifacts: IArtifactData[]) {
if (artifacts.length <= 1) {
return artifacts;
}

artifacts.sort( (x, y) => {
if (x.repositoryId === y.repositoryId) return 0;
if (x.repositoryId < y.repositoryId) return -1;
// if (tuple[0] > tuple[1])
return 1;
});

let i: number;
for (i = 1; i < artifacts.length; i++) {
const prev: IArtifactData = artifacts[i - 1];
const current: IArtifactData = artifacts[i];

if (prev.repositoryId !== current.repositoryId) {
continue;
}

if (prev.commit === current.commit) {
continue;
}

let search = <giti.GitQueryCommitsCriteria> {
ids: [prev.commit, current.commit],
};

tl.debug(`Attempting to determine which commit was last. Prev: ${prev.commit} Current: ${current.commit} for repository: ${prev.repositoryId}`);

const commits: giti.GitCommitRef[] = await gitapi.getCommitsBatch(search, prev.repositoryId);
if (commits.length !== 2) {
tl.setResult(tl.TaskResult.Failed, `Cannot resolve difference most recent between two commits: ${prev.commit} ${current.commit}`);
return artifacts;
}

let firstCommitArtifactIndex: number;
let secondCommitArtifactIndex: number;

if (artifacts[i - 1].commit === commits[0].commitId.toLowerCase()) {
firstCommitArtifactIndex = i - 1;
secondCommitArtifactIndex = i;
} else {
firstCommitArtifactIndex = i;
secondCommitArtifactIndex = i - 1;
}

tl.debug(`Commit Info: { Id: ${commits[0].commitId} Date: ${commits[0].committer.date}} {Id: ${commits[1].commitId} Date: ${commits[1].committer.date}}`);

if (commits[0].committer.date < commits[1].committer.date) {
tl.debug(`Winning commit: ${commits[1].commitId}`);
artifacts[firstCommitArtifactIndex].commit = artifacts[secondCommitArtifactIndex].commit;
} else {
tl.debug(`Winning commit: ${commits[0].commitId}`);
artifacts[secondCommitArtifactIndex].commit = artifacts[firstCommitArtifactIndex].commit;
}
}

return artifacts;
}

private async getRepositoryId(bldapi: bld.IBuildApi, name: string): Promise < string > {
Expand Down
2 changes: 2 additions & 0 deletions overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Branching:

The branch folder allows you to specify a subfolder to branch to. Adding to the example above. If the Branch Folder was set to: 'patch' the ref that would be created is: /refs/heads/patch/v3.0.17270.8

* Changes in 5.0.0
* Support multiple artifcats from the same repository with different commits. Newest to be commited wins.
* Changes in 4.0.1
* Update the error log to include previous commit id
* Changes in 4.0.0
Expand Down

0 comments on commit a9a0b73

Please sign in to comment.