Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
fix: correctly load base branch [PLAT-1782] (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
prescottprue authored Mar 17, 2022
1 parent 9409389 commit 22a9c08
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
yarn lint-staged && yarn build && git add dist
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[![Build Status][build-status-image]][build-status-url]
[![semantic-release][semantic-release-icon]][semantic-release-url]
[![Code Style][code-style-image]][code-style-url]
[![Coverage][coverage-image]][coverage-url]

</div>

Expand Down Expand Up @@ -127,3 +128,5 @@ jobs:
[code-style-url]: https://github.com/airbnb/javascript
[semantic-release-icon]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[coverage-image]: https://coveralls.io/repos/github/reside-eng/upload-coverage-base-action/badge.svg?branch=main&t=FFVNNF
[coverage-url]: https://coveralls.io/github/reside-eng/upload-coverage-base-action?branch=main
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ inputs:
description: 'Filename of workflow which uploaded coverage artifact'
default: 'verify.yml'
required: false
flag-name:
description: 'Flag name'
required: false
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
38 changes: 23 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12738,23 +12738,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.reportToCoveralls = void 0;
const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438);
const coveralls_api_1 = __importDefault(__nccwpck_require__(6398));
/**
* @param owner - Github repo owner
* @param repo - Github repo name
* @param branch - Github branch name (used as base branch in Coveralls)
* Report coverage to Coveralls for base branch
*/
async function reportToCoveralls(owner, repo, branch) {
core.debug(`Uploading base coverage to Coveralls for branch "${branch}"`);
async function reportToCoveralls() {
const { owner, repo } = github_1.context.repo;
const { ref: baseRef } = github_1.context?.payload?.pull_request?.base || {};
const branch = core.getInput('base-branch') || baseRef || 'main';
const flag = core.getInput('flag-name') || undefined; // default empty string to undefined
const jobSettings = {
lcov_path: core.getInput('lcov-path'),
service_job_id: `${github_1.context.runId}`,
service_name: 'github',
flag_name: flag,
commit_sha: github_1.context.sha,
git: {
branch,
},
};
core.debug(`Uploading base coverage to Coveralls with settings: ${JSON.stringify(jobSettings, null, 2)}`);
try {
const coveralls = new coveralls_api_1.default(core.getInput('coveralls-token'));
await coveralls.postJob('github', owner, repo, {
lcov_path: core.getInput('lcov-path'),
git: {
branch,
},
});
core.info('Successfully uploaded base coverage to Coveralls');
await coveralls.postJob('github', owner, repo, jobSettings);
core.info(`Successfully uploaded base coverage to Coveralls for branch "${branch}"`);
}
catch (err) {
core.error(`Error uploading lcov to Coveralls: ${JSON.stringify(err)}`);
Expand Down Expand Up @@ -12806,21 +12814,21 @@ const coveralls_1 = __nccwpck_require__(2047);
*/
async function run() {
const { owner, repo } = github_1.context.repo;
const { ref: headRef } = github_1.context?.payload?.pull_request?.head || {};
const baseBranch = core.getInput('base-branch') || headRef || 'main';
const coverageArtifact = await (0, actions_1.downloadCoverageArtifact)(owner, repo);
core.debug('Coverage artifact successfully downloaded, writing to disk');
// Confirm coverage folder exists before writing to disk
const coverageFolderPath = './coverage';
const coveragePath = `${coverageFolderPath}/lcov.info`;
if (!(0, fs_1.existsSync)(coverageFolderPath)) {
core.debug('coverage folder does not exist, creating');
(0, fs_1.mkdirSync)(coverageFolderPath);
core.debug('coverage folder create successful');
}
// Write lcov.info file to coverage/lcov.info
(0, fs_1.writeFileSync)(coveragePath, Buffer.from(coverageArtifact));
core.debug('Write to disk successful, uploading to Coveralls');
// Report to Coveralls as base
await (0, coveralls_1.reportToCoveralls)(owner, repo, baseBranch);
await (0, coveralls_1.reportToCoveralls)();
}
exports.run = run;

Expand Down
46 changes: 29 additions & 17 deletions src/coveralls.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import * as core from '@actions/core';
import Coveralls from 'coveralls-api';
import { context } from '@actions/github';
import Coveralls, { PostJobFromLCOVArgs } from 'coveralls-api';

/**
* @param owner - Github repo owner
* @param repo - Github repo name
* @param branch - Github branch name (used as base branch in Coveralls)
* Report coverage to Coveralls for base branch
*/
export async function reportToCoveralls(
owner: string,
repo: string,
branch: string,
) {
core.debug(`Uploading base coverage to Coveralls for branch "${branch}"`);
export async function reportToCoveralls() {
const { owner, repo } = context.repo;
const { ref: baseRef } = context?.payload?.pull_request?.base || {};
const branch = core.getInput('base-branch') || baseRef || 'main';
const flag = core.getInput('flag-name') || undefined; // default empty string to undefined
const jobSettings: PostJobFromLCOVArgs = {
lcov_path: core.getInput('lcov-path'),
service_job_id: `${context.runId}`,
service_name: 'github',
flag_name: flag,
commit_sha: context.sha,
git: {
branch,
},
};
core.debug(
`Uploading base coverage to Coveralls with settings: ${JSON.stringify(
jobSettings,
null,
2,
)}`,
);
try {
const coveralls = new Coveralls(core.getInput('coveralls-token'));
await coveralls.postJob('github', owner, repo, {
lcov_path: core.getInput('lcov-path'),
git: {
branch,
},
});
core.info('Successfully uploaded base coverage to Coveralls');
await coveralls.postJob('github', owner, repo, jobSettings);
core.info(
`Successfully uploaded base coverage to Coveralls for branch "${branch}"`,
);
} catch (err) {
core.error(`Error uploading lcov to Coveralls: ${JSON.stringify(err)}`);
throw err;
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import { reportToCoveralls } from './coveralls';
*/
export async function run() {
const { owner, repo } = context.repo;
const { ref: headRef } = context?.payload?.pull_request?.head || {};
const baseBranch = core.getInput('base-branch') || headRef || 'main';
const coverageArtifact = await downloadCoverageArtifact(owner, repo);
core.debug('Coverage artifact successfully downloaded, writing to disk');

// Confirm coverage folder exists before writing to disk
const coverageFolderPath = './coverage';
const coveragePath = `${coverageFolderPath}/lcov.info`;
if (!existsSync(coverageFolderPath)) {
core.debug('coverage folder does not exist, creating');
mkdirSync(coverageFolderPath);
core.debug('coverage folder create successful');
}

// Write lcov.info file to coverage/lcov.info
writeFileSync(coveragePath, Buffer.from(coverageArtifact));
core.debug('Write to disk successful, uploading to Coveralls');

// Report to Coveralls as base
await reportToCoveralls(owner, repo, baseBranch);
await reportToCoveralls();
}

0 comments on commit 22a9c08

Please sign in to comment.