Skip to content

Commit

Permalink
Merge pull request #1 from sentrysoftware/feature/upload-buildinfo
Browse files Browse the repository at this point in the history
Added buildinfo files upload.
  • Loading branch information
ChristopheClermont authored Mar 27, 2024
2 parents 7ae6802 + bda6aca commit 52dd62d
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 5 deletions.
5 changes: 4 additions & 1 deletion __tests__/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const core = require('@actions/core');
const { readBuildInfoFiles } = require('../src/buildinfo.js');
const { run } = require('../src/run.js');
const { searchBuildinfo } = require('../src/search.js');
const { uploadArtifact } = require('../src/upload.js');
const { uploadArtifact, uploadBuildInfo } = require('../src/upload.js');

// Mock dependencies
jest.mock('@actions/core', () => ({
Expand All @@ -20,6 +20,7 @@ jest.mock('../src/search.js', () => ({
}));
jest.mock('../src/upload.js', () => ({
uploadArtifact: jest.fn(),
uploadBuildInfo: jest.fn(),
}));

describe('test run', () => {
Expand All @@ -36,6 +37,7 @@ describe('test run', () => {

expect(searchBuildinfo).toHaveBeenCalled();
expect(readBuildInfoFiles).toHaveBeenCalledWith(['path/to/buildinfo1', 'path/to/buildinfo2']);
expect(uploadBuildInfo).toHaveBeenCalledWith(['path/to/buildinfo1', 'path/to/buildinfo2']);
expect(uploadArtifact).toHaveBeenCalledTimes(2); // Assuming two artifacts are found and uploaded
});

Expand All @@ -49,6 +51,7 @@ describe('test run', () => {
expect(searchBuildinfo).toHaveBeenCalled();
expect(readBuildInfoFiles).toHaveBeenCalledWith([]);
expect(core.warning).toHaveBeenCalledWith('No output file found for upload.');
expect(uploadBuildInfo).toHaveBeenCalledWith([]);
expect(uploadArtifact).not.toHaveBeenCalled();
});

Expand Down
103 changes: 101 additions & 2 deletions __tests__/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const artifact = require('@actions/artifact');
const core = require('@actions/core');
const github = require('@actions/github');

const { uploadArtifact } = require('../src/upload.js');
const { findCommonDirectory, uploadArtifact, uploadBuildInfo } = require('../src/upload.js');
const inputs = require('../src/inputs.js');

jest.mock('path', () => ({
Expand All @@ -18,6 +18,52 @@ jest.mock('@actions/core');
jest.mock('@actions/artifact');
jest.mock('@actions/github');

describe('test findCommonDirectory', () => {

test('common base directory', () => {
const paths = [
"/home/user/project/src/index.js",
"/home/user/project/src/app/app.js",
"/home/user/project/src/app/components/button.js"
];
expect(findCommonDirectory(paths)).toBe("/home/user/project/src");
});

test('no common directory', () => {
const paths = [
"/home/user/project/src/index.js",
"/root/docs/readme.md"
];
expect(findCommonDirectory(paths)).toBe("");
});

test('mixed separators', () => {
const paths = [
"/home/user/project/file1.txt",
"\\home\\user\\project\\file2.txt"
];
expect(findCommonDirectory(paths)).toBe("/home/user/project");
});

test('single path', () => {
const paths = ["/home/user/project/src/index.js"];
expect(findCommonDirectory(paths)).toBe("/home/user/project/src/index.js");
});

test('empty array', () => {
const paths = [];
expect(findCommonDirectory(paths)).toBe("");
});

test('root directory', () => {
const paths = [
"/",
"/home",
];
expect(findCommonDirectory(paths)).toBe("");
});
});

describe('test uploadArtifact', () => {
beforeEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -49,7 +95,7 @@ describe('test uploadArtifact', () => {
});

test('upload error', () => {

// Mock other modules
jest.spyOn(inputs, 'getUploadOptions').mockImplementation(() => {});

Expand All @@ -66,3 +112,56 @@ describe('test uploadArtifact', () => {
});

});

describe('test uploadBuildInfo', () => {
beforeEach(() => {
jest.clearAllMocks();

github.context.repo = { owner: 'ownerName', repo: 'repoName' };
github.context.serverUrl = 'https://github.com';
github.context.runId = '123456';
});

test('upload success', async () => {
// Mock other modules
global.findCommonDirectory = jest.fn().mockReturnValue('/path/to');
global.getUploadOptions = jest.fn().mockReturnValue({});

// Setup default mocks for artifact client and GitHub context
const mockUploadArtifact = jest.fn().mockResolvedValue({
size: 100,
id: 'artifact123'
});
artifact.DefaultArtifactClient.mockReturnValue({
uploadArtifact: mockUploadArtifact
});

uploadBuildInfo(['/path/to/file1', '/path/to/file2']);

expect(mockUploadArtifact).toHaveBeenCalledWith(
'buildinfo.zip',
['/path/to/file1', '/path/to/file2'],
'/path/to',
{}
);

});

test('upload error', async () => {
// Mock other modules
global.findCommonDirectory = jest.fn().mockReturnValue('/path/to');
global.getUploadOptions = jest.fn().mockReturnValue({});

const errorMessage = 'Upload failed';

const mockUploadArtifact = jest.fn().mockRejectedValue(new Error(errorMessage));
artifact.DefaultArtifactClient.mockReturnValue({
uploadArtifact: mockUploadArtifact
});

uploadBuildInfo(['/path/to/file1', '/path/to/file2']);

expect(artifact.DefaultArtifactClient).toHaveBeenCalled();
});

});
63 changes: 62 additions & 1 deletion dist/index.js

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

4 changes: 3 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const core = require('@actions/core');
const { readBuildInfoFiles } = require('./buildinfo.js');
const { searchBuildinfo } = require('./search.js');
const { uploadArtifact } = require('./upload.js');
const { uploadArtifact, uploadBuildInfo } = require('./upload.js');

function run() {
try {
const buildInfoPaths = searchBuildinfo();

let outputs = readBuildInfoFiles(buildInfoPaths);

uploadBuildInfo(buildInfoPaths);

if (outputs) {
outputs.forEach(output => {
uploadArtifact(output);
Expand Down
59 changes: 59 additions & 0 deletions src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ const github = require('@actions/github');

const { getUploadOptions } = require('./inputs.js');

function findCommonDirectory(paths) {
// Split each path into parts
const pathParts = paths.map(path => path.split(/\/|\\/));
const shortestPathLength = Math.min(...pathParts.map(parts => parts.length));

let commonParts = [];
if (pathParts.length > 0) {
for (let i = 0; i < shortestPathLength; i++) {
let currentPart = pathParts[0][i];

// Check if the current part is common to all paths
if (pathParts.every(parts => parts[i] === currentPart)) {
commonParts.push(currentPart);
} else {
// If a non-matching part is found, stop the search
break;
}
}
}

const commonPath = commonParts.join("/");
return commonPath;
}

function uploadArtifact(filePath) {

core.info(`uploadArtifact(${filePath})`);
Expand Down Expand Up @@ -46,6 +70,41 @@ function uploadArtifact(filePath) {

}

function uploadBuildInfo(buildInfoPaths) {

core.info(`uploadBuildInfo(${buildInfoPaths})`);

const rootDirectory = findCommonDirectory(buildInfoPaths);

const options = getUploadOptions();

const client = new artifact.DefaultArtifactClient();
const uploadPromise = client.uploadArtifact(
'buildinfo.zip',
buildInfoPaths,
rootDirectory,
options
);

core.info('Started artifact buildinfo.zip upload');

uploadPromise.then(uploadResponse => {
core.info(
`Artifact buildinfo.zip has been successfully uploaded! Final size is ${uploadResponse.size} bytes. Artifact ID is ${uploadResponse.id}`
);

const repository = github.context.repo;
const artifactURL = `${github.context.serverUrl}/${repository.owner}/${repository.repo}/actions/runs/${github.context.runId}/artifacts/${uploadResponse.id}`;
core.info(`Artifact download URL: ${artifactURL}`);

}).catch(err => {
core.error(err);
});

}

module.exports = {
findCommonDirectory,
uploadArtifact,
uploadBuildInfo,
};

0 comments on commit 52dd62d

Please sign in to comment.