Skip to content

Commit

Permalink
IT-522 - Add support for path (#9)
Browse files Browse the repository at this point in the history
* check file path using endsWith

* check file path using endsWith

* add tests for path change

* get fileName from full path

* Update project-version for branch into main project

Co-authored-by: Henrik Adamski <hadamski@avides.com>
  • Loading branch information
branonbarrett and Henrik Adamski committed Jan 12, 2022
1 parent e6aa039 commit 41943dd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If the version update is valid then the new version is available as output. Usag

## Example usage
```
- uses: avides/actions-project-version-check@v1.2.0
- uses: avides/actions-project-version-check@v1.2.1
- with:
token: ${{ secrets.GITHUB_TOKEN }}
file-to-check: package.json
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "actions-project-version-check",
"version": "1.2.0",
"version": "1.2.1",
"description": "GitHub Action that checks if the project version has been updated",
"main": "dist/index.js",
"scripts": {
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const core = require('@actions/core');
const github = require('@actions/github');
const xml2js = require('xml2js');
const fs = require('fs');
const path = require('path');
const semverDiff = require('semver-diff');

// constants
Expand All @@ -25,15 +26,15 @@ function getProjectVersionFromPackageJsonFile(fileContent) {
}

function getProjectVersion(fileContent, fileName) {
if (fileName == 'pom.xml') {
if (fileName === 'pom.xml') {
return getProjectVersionFromMavenFile(fileContent);
}

if (fileName == 'package.json') {
if (fileName === 'package.json') {
return getProjectVersionFromPackageJsonFile(fileContent);
}

if (fileName == 'version.txt') {
if (fileName === 'version.txt') {
return new String(fileContent).trim();
}

Expand Down Expand Up @@ -88,14 +89,15 @@ async function run() {

// get updated project version
var updatedBranchFileContent = fs.readFileSync(repositoryLocalWorkspace + fileToCheck);
var updatedProjectVersion = getProjectVersion(updatedBranchFileContent, fileToCheck);
const fileName = path.basename(repositoryLocalWorkspace + fileToCheck);
var updatedProjectVersion = getProjectVersion(updatedBranchFileContent, fileName);

// check version update
if (core.getInput('only-return-version') == 'false') {
octokit.repos.getContent({ owner: repositoryOwner, repo: repositoryName, path: fileToCheck, ref: targetBranch, headers: { 'Accept': 'application/vnd.github.v3.raw' } }).then(response => {
// get target project version
var targetBranchFileContent = response.data;
var targetProjectVersion = getProjectVersion(targetBranchFileContent, fileToCheck);
var targetProjectVersion = getProjectVersion(targetBranchFileContent, fileName);

checkVersionUpdate(targetProjectVersion, updatedProjectVersion, additionalFilesToCheck);
}).catch(error => console.log('Cannot resolve `' + fileToCheck + '` in target branch! No version check required. ErrMsg => ' + error));
Expand Down
24 changes: 24 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ afterEach(() =>
jest.clearAllMocks();
});

test('testGetProjectVersion should recognize pom.xml as valid in path and get version', async() =>
{
var result = Index.getProjectVersion('<project><version>1.0.0</version></project>', 'pom.xml');
expect(result).toBe('1.0.0');
});

test('testGetProjectVersion should recognize package.json as valid in path and get version', async() =>
{
var result = Index.getProjectVersion('{"version":"1.0.0"}', 'package.json');
expect(result).toBe('1.0.0');
});

test('testGetProjectVersion should recognize version.txt as valid in path and get version', async() =>
{
var result = Index.getProjectVersion('1.0.0', 'version.txt');
expect(result).toBe('1.0.0');
});

test('testGetProjectVersion should return undefined with invalid file', async() =>
{
var result = Index.getProjectVersion('1.0.0', 'version.jar');
expect(result).toBe(undefined);
});

test('testGetProjectVersionFromMavenFile', async() =>
{
var result = Index.getProjectVersionFromMavenFile('<project><version>1.0.0</version></project>');
Expand Down

0 comments on commit 41943dd

Please sign in to comment.