Skip to content

Commit

Permalink
Merge pull request #91 from cicirello/fix-backward-compat
Browse files Browse the repository at this point in the history
Fallback to `set-output` if `$GITHUB_OUTPUT`  doesn't exist
  • Loading branch information
cicirello authored Oct 24, 2022
2 parents 96fd4ea + c483fb0 commit 2dc380e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - 2022-10-21
## [Unreleased] - 2022-10-24

### Added

Expand All @@ -17,11 +17,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Dependencies
* Bump cicirello/pyaction from 4.11.0 to 4.11.1

### CI/CD


## [2.8.1] - 2022-10-24

### Fixed
* The replacement for GitHub Action's deprecated `set-output` is not available yet for all self-hosted users. This patch
handles that by using the new `$GITHUB_OUTPUT` environment file if it exists, and otherwise falling back to `set-output`.

### Dependencies
* Bump cicirello/pyaction from 4.11.0 to 4.11.1


## [2.8.0] - 2022-10-21

### Added
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "jacoco-badge-generator"
version = "2.8.0"
version = "2.8.1"
authors = [
{ name="Vincent A. Cicirello", email="[email protected]" },
]
Expand Down
3 changes: 2 additions & 1 deletion src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@
generateSummary = sys.argv[18].lower() == "true",
summaryFilename = sys.argv[19],
coverageLabel = sys.argv[20],
branchesLabel = sys.argv[21]
branchesLabel = sys.argv[21],
ghActionsMode = True
)
3 changes: 2 additions & 1 deletion src/jacoco_badge_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,6 @@
generateSummary = args.generateSummary == "true",
summaryFilename = args.summaryFilename,
coverageLabel = args.coverageLabel,
branchesLabel = args.branchesLabel
branchesLabel = args.branchesLabel,
ghActionsMode = False
)
20 changes: 16 additions & 4 deletions src/jacoco_badge_generator/coverage_badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,22 +464,29 @@ def coverageDictionary(cov, branches) :
"""
return { "coverage" : 100 * cov, "branches" : 100 * branches }

def set_action_outputs(output_pairs) :
def set_action_outputs(output_pairs, ghActionsMode) :
"""Sets the GitHub Action outputs if running as a GitHub Action,
and otherwise logs coverage percentages to terminal if running in
CLI mode. Note that if the CLI mode is used within a GitHub Actions
workflow, it will be treated the same as GitHub Actions mode.
Temporary continued support for deprecated set-output for self-hosted
GitHub users with versions that don't have the new GITHUB_OUTPUT env
variable. Detected if running in GitHub Actions mode, but such that
GITHUB_OUTPUT doesn't exist.
Keyword arguments:
output_pairs - Dictionary of outputs with values
ghActionsMode - True if running as a GitHub Action, otherwise pass False
"""
if "GITHUB_OUTPUT" in os.environ :
with open(os.environ["GITHUB_OUTPUT"], "a") as f :
for key, value in output_pairs.items() :
print("{0}={1}".format(key, value), file=f)
else :
output_template = "::set-output name={0}::{1}" if ghActionsMode else "{0}={1}"
for key, value in output_pairs.items() :
print("{0}={1}".format(key, value))
print(output_template.format(key, value))

def add_workflow_job_summary(cov, branches) :
"""When running as a GitHub Action, adds a job summary, and otherwise
Expand Down Expand Up @@ -515,7 +522,8 @@ def main(jacocoCsvFile,
generateSummary,
summaryFilename,
coverageLabel,
branchesLabel) :
branchesLabel,
ghActionsMode) :
"""Main function.
Keyword arguments:
Expand Down Expand Up @@ -555,6 +563,7 @@ def main(jacocoCsvFile,
summaryFilename - The filename of the summary file.
coverageLabel - Text for the left-side of the coverage badge.
branchesLabel - Text for the left-side of the branches coverage badge.
ghActionsMode - True if running in GitHub Actions mode, or False otherwise.
"""
if onMissingReport not in {"fail", "quiet", "badges"} :
print("ERROR: Invalid value for on-missing-report.")
Expand Down Expand Up @@ -631,5 +640,8 @@ def main(jacocoCsvFile,
with open(summaryFilenameWithPath, "w") as summaryFile :
json.dump(coverageDictionary(cov, branches), summaryFile, sort_keys=True)

set_action_outputs({"coverage" : cov, "branches" : branches})
set_action_outputs(
{"coverage" : cov, "branches" : branches},
ghActionsMode
)
add_workflow_job_summary(cov, branches)

0 comments on commit 2dc380e

Please sign in to comment.