-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfbad4b
commit d487f4f
Showing
9 changed files
with
304 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/bin/bash | ||
|
||
dryRun=$1 | ||
action=$2 | ||
dir=$3 | ||
tagName=$4 | ||
|
||
if [ "$dryRun" != "false" ]; | ||
then | ||
dryRun="true" | ||
fi | ||
|
||
if [ "$action" == "" ]; | ||
then | ||
echo "Missing the second parameter: action" | ||
exit 1 | ||
fi | ||
|
||
if [ "$dir" == "" ]; | ||
then | ||
echo "Missing the third parameter: project directory" | ||
exit 1 | ||
fi | ||
|
||
if [ "$action" != "read-tags" ]; | ||
then | ||
if [ "$action" != "delete-tag" ]; | ||
then | ||
if [ "$action" != "add-tag" ]; | ||
then | ||
echo "Unknown action. The 'action' must be one of 'read-tags', 'delete-tag', 'add-tag'. The current value is ${action}" | ||
exit 1 | ||
fi | ||
fi | ||
fi | ||
|
||
if [ "$action" == "read-tags" ]; | ||
then | ||
cd $dir | ||
git show-ref --tags | ||
fi | ||
|
||
if [ "$action" == "delete-tag" ]; | ||
then | ||
if [ "$tagName" == "" ]; | ||
then | ||
echo "Missing the third parameter: tag name. Cannot delete tag." | ||
exit 1 | ||
fi | ||
cd $dir | ||
git tag --delete $tagName | ||
|
||
if [ "$dryRun" == "false" ]; | ||
then | ||
git push --delete origin $tagName | ||
else | ||
echo "DRY RUN mode is ON. The tag ${tagName} will not be delted from remote." | ||
echo "DRY RUN. git push --delete origin $tagName" | ||
fi | ||
fi | ||
|
||
if [ "$action" == "add-tag" ]; | ||
then | ||
cd $dir | ||
git tag $tagName | ||
if [ "$dryRun" == "false" ]; | ||
then | ||
git push origin $tagName | ||
else | ||
echo "DRY RUN mode is ON. The tag ${tagName} will not be pushed to remote." | ||
echo "DRY RUN. git push origin $tagName" | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class GitConfig { | ||
static PARAM_GIT_REPOSTIRY_URL = "--git-repository-url" | ||
static PARAM_GIT_REF = "--git-ref" | ||
|
||
constructor(readonly gitRepoUrl: string, readonly gitRef: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as NodeShell from "node:child_process" | ||
|
||
import { ActionType, Config } from "../config.js" | ||
import { logger } from "../logger.js" | ||
import { TestReport } from "../report/schema-v1.js" | ||
import { didTestFail } from "../report/utils.js" | ||
|
||
type TagInfo = { tag: string, sha: string } | ||
|
||
export class GitTaggingService { | ||
constructor( | ||
private readonly config: Config) {} | ||
|
||
private readCommitSha = (workDir: string): string => { | ||
const script = `${this.config.appRootDir}/scripts/git-co.sh` | ||
let commitSha: string | ||
try { | ||
commitSha = NodeShell | ||
.execSync(`${this.config.appRootDir}/scripts/git-co.sh ${this.config.gitConfig.gitRepoUrl} ${this.config.gitConfig.gitRef} ${workDir}`) | ||
.toString() | ||
.trim() | ||
} catch (e) { | ||
throw new Error(`Error invoking script ${script}`, { cause: e }) | ||
} | ||
|
||
if (commitSha.indexOf("COMMIT_SHA=") !== 0) { | ||
throw new Error(`Git checkout might have failed. Unexpected data returned from: "${script}"`) | ||
} | ||
|
||
commitSha = commitSha.substring(11) | ||
return commitSha | ||
} | ||
|
||
private getCurrentTags = (workDir: string): Array<TagInfo> => { | ||
// This comes in the format of "long-sha long-tag-name" | ||
const listOfTags = NodeShell | ||
.execSync(`${ this.config.appRootDir }/scripts/git-tags.sh ${ this.config.dryRun } read-tags ${ workDir }`) | ||
.toString() | ||
.trim() | ||
|
||
logger.info("getCurrentTags(): \n%s", listOfTags) | ||
|
||
// Convert into the list of "short sha, short tag name" | ||
const formattedList = listOfTags | ||
.split("\n") | ||
.map(v => v.trim()) | ||
.filter(v => v.length > 0) | ||
.map(v => { | ||
const parsed = v.split(" ").map(a => a.trim()).filter(a => a.length > 0) | ||
return { | ||
sha: parsed[0].substring(0, 7), | ||
tag: parsed[1].substring("refs/tags/".length) | ||
} | ||
}) | ||
|
||
return formattedList | ||
} | ||
|
||
private deleteOldDptTags = (workDir: string, tags: Array<TagInfo>, commitSha: string) => { | ||
const currentTags = tags.filter(t => t.sha === commitSha) | ||
logger.info("deleteOldDptTags(): %s", JSON.stringify(currentTags, null, 2)) | ||
|
||
if (currentTags.length == 0) return | ||
|
||
// we found some tags at the current commit sha | ||
for (let tagInfo of currentTags) { | ||
if (tagInfo.tag == `dpt-fail-${commitSha}` || tagInfo.tag == `dpt-pass-${ commitSha }`) { | ||
// we found DPT tag at the current commit sha, lets remove it | ||
logger.info("deleting tag: %s at %s", tagInfo.tag, tagInfo.sha) | ||
NodeShell.execSync(`${ this.config.appRootDir }/scripts/git-tags.sh ${ this.config.dryRun } delete-tag ${ workDir } ${ tagInfo.tag }`) | ||
} | ||
} | ||
} | ||
|
||
executeActions = async (report: TestReport) => { | ||
logger.info("") | ||
logger.info("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") | ||
logger.info("Git tagging module is processing the report: \"%s\"", report.name) | ||
logger.info("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") | ||
|
||
const appRootDir = this.config.appRootDir | ||
|
||
const processWhenFailed = this.config.testFailActions.includes(ActionType.TAG_GIT_REPOSITORY) | ||
const processWhenPassed = this.config.testPassActions.includes(ActionType.TAG_GIT_REPOSITORY) | ||
if (!processWhenFailed && !processWhenPassed) { | ||
logger.info("No action is required from git-tag/GitTaggingService module") | ||
return | ||
} | ||
|
||
const workDir = `${ appRootDir }/${ Date.now() }` | ||
const commitSha = this.readCommitSha(workDir) | ||
const tagsList = this.getCurrentTags(workDir) | ||
|
||
this.deleteOldDptTags(workDir, tagsList, commitSha) | ||
|
||
// ok, now the repo is clean from the previous runs. Lets just tag it based on test results | ||
|
||
const isFailure = didTestFail(report) | ||
const newTagName = `dpt-${ isFailure ? 'fail' : 'pass' }-${ commitSha }` | ||
|
||
if (isFailure && processWhenFailed || !isFailure && processWhenPassed) { | ||
logger.info("Tagging the repository with tag %s", newTagName) | ||
NodeShell.execSync(`${ this.config.appRootDir }/scripts/git-tags.sh ${ this.config.dryRun } add-tag ${ workDir } ${ newTagName }`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.