Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
feat: checks if Python>=3.8 and pip are available on the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinDeJong-TomTom committed May 11, 2022
1 parent 6a674ac commit 5ecfd1a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/commisery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Check-out the repo under $GITHUB_WORKSPACE
uses: actions/checkout@v3

- name: Setup Python
- name: Setup Python v3.8
uses: actions/setup-python@v3
with:
python-version: '3.8'
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

Using this GitHub action, scan all commits in your Pull Request against the [Conventional Commits]
standard using [Commisery]
## Prerequisites

[Conventional Commits]: https://www.conventionalcommits.org/en/v1.0.0/
[Commisery]: https://pypi.org/project/commisery/
* [Commisery] requires at least `Python>3.8`
* `pip` needs to be installed for this Python version

## Usage

Expand Down Expand Up @@ -42,10 +43,14 @@ The workflow, usually declared in `.github/workflows/build.yml`, looks like:
- **token**: GitHub Token provided by GitHub, see [Authenticating with the GITHUB_TOKEN]
- **pull_request**: Pull Request number, provided by the [GitHub context].

[Authenticating with the GITHUB_TOKEN]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token
[GitHub context]: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context


## Example of Conventional Commit check results

![example](resources/example.png)
![example](resources/example.png)

[Conventional Commits]: https://www.conventionalcommits.org/en/v1.0.0/
[Commisery]: https://pypi.org/project/commisery/
[Authenticating with the GITHUB_TOKEN]: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token
[GitHub context]: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context

73 changes: 50 additions & 23 deletions dist/commisery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9916,7 +9916,7 @@ function strip_ansicolor(message) {
* @param message
* @returns multiline message
*/
function get_error_subjects(message, sha) {
function get_error_subjects(message) {
let errors = [];
for (var line of strip_ansicolor(message).split("\n")) {
if (line.startsWith(".commit-message")) {
Expand All @@ -9928,15 +9928,37 @@ function get_error_subjects(message, sha) {
}
return errors;
}
/**
* Confirms whether Python >=3.8 and pip are present on the runner
*/
function check_prerequisites() {
return __awaiter(this, void 0, void 0, function* () {
const python_version_re = /Python\s*(\d+)\.(\d+)\.(\d+)/;
const { stdout: python_version } = yield exec.getExecOutput("python", ["--version"], { silent: true });
const match = python_version_re.exec(python_version);
if (!match || match.length != 4) {
throw new Error("Unable to determine the installed Python version.");
}
if (!(parseInt(match[1]) == 3 && parseInt(match[2]) >= 8)) {
throw new Error(`Incorrect Python version installed; found ${match[1]}.${match[2]}.${match[3]}, expected >= 3.8.0`);
}
try {
const { stdout: pip_version } = yield exec.getExecOutput("python", ["-m", "pip", "--version"], { silent: true });
}
catch (_a) {
throw new Error("Unable to determine the installed Pip version.");
}
});
}
/**
* Installs the latest version of commisery
*/
function prepare_environment() {
return __awaiter(this, void 0, void 0, function* () {
// Upgrade pip to the latest version
yield exec.exec("python -m pip install --upgrade pip");
// Ensure Python (>= 3.8) and pip are installed
yield check_prerequisites();
// Install latest version of commisery
yield exec.exec("python -m pip install --upgrade commisery");
yield exec.exec("python", ["-m", "pip", "install", "--upgrade", "commisery"]);
});
}
/**
Expand Down Expand Up @@ -9981,33 +10003,38 @@ function is_commit_valid(commit) {
catch (error) {
core.debug("Error detected while executing commisery");
}
return [stderr == "", get_error_subjects(stderr, commit.sha)];
return [stderr == "", get_error_subjects(stderr)];
});
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
// Ensure that commisery is installed
yield prepare_environment();
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
// Validate each commit against Conventional Commit standard
let commits = yield get_commits(owner, repo, core.getInput("pull_request"));
let success = true;
for (const commit of commits) {
let [valid, errors] = yield is_commit_valid(commit);
if (!valid) {
core.summary
.addHeading(commit.commit.message, 2)
.addRaw(`<b>SHA:</b> <a href="${commit.html_url}"><code>${commit.sha}</code></a>`);
for (var error of errors) {
core.summary.addCodeBlock(error);
try {
yield prepare_environment();
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
// Validate each commit against Conventional Commit standard
let commits = yield get_commits(owner, repo, core.getInput("pull_request"));
let success = true;
for (const commit of commits) {
let [valid, errors] = yield is_commit_valid(commit);
if (!valid) {
core.summary
.addHeading(commit.commit.message, 2)
.addRaw(`<b>SHA:</b> <a href="${commit.html_url}"><code>${commit.sha}</code></a>`);
for (var error of errors) {
core.summary.addCodeBlock(error);
}
success = false;
}
success = false;
}
if (!success) {
core.setFailed(`Commits in your Pull Request are not compliant to Conventional Commits`);
// Post summary
core.summary.write();
}
}
if (!success) {
core.setFailed(`Commits in your Pull Request are not compliant to Conventional Commits`);
// Post summary
core.summary.write();
catch (ex) {
core.setFailed(ex.message);
}
});
}
Expand Down
95 changes: 66 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function strip_ansicolor(message: string) {
* @param message
* @returns multiline message
*/
function get_error_subjects(message: string, sha: string) {
function get_error_subjects(message: string) {
let errors: string[] = [];

for (var line of strip_ansicolor(message).split("\n")) {
Expand All @@ -52,15 +52,48 @@ function get_error_subjects(message: string, sha: string) {
return errors;
}

/**
* Confirms whether Python >=3.8 and pip are present on the runner
*/
async function check_prerequisites() {
const python_version_re = /Python\s*(\d+)\.(\d+)\.(\d+)/;
const { stdout: python_version } = await exec.getExecOutput(
"python",
["--version"],
{ silent: true }
);
const match = python_version_re.exec(python_version);

if (!match || match.length != 4) {
throw new Error("Unable to determine the installed Python version.");
}

if (!(parseInt(match[1]) == 3 && parseInt(match[2]) >= 8)) {
throw new Error(
`Incorrect Python version installed; found ${match[1]}.${match[2]}.${match[3]}, expected >= 3.8.0`
);
}

try {
const { stdout: pip_version } = await exec.getExecOutput(
"python",
["-m", "pip", "--version"],
{ silent: true }
);
} catch {
throw new Error("Unable to determine the installed Pip version.");
}
}

/**
* Installs the latest version of commisery
*/
async function prepare_environment() {
// Upgrade pip to the latest version
await exec.exec("python -m pip install --upgrade pip");
// Ensure Python (>= 3.8) and pip are installed
await check_prerequisites();

// Install latest version of commisery
await exec.exec("python -m pip install --upgrade commisery");
await exec.exec("python", ["-m", "pip", "install", "--upgrade", "commisery"]);
}

/**
Expand Down Expand Up @@ -111,44 +144,48 @@ async function is_commit_valid(commit): Promise<[boolean, string[]]> {
core.debug("Error detected while executing commisery");
}

return [stderr == "", get_error_subjects(stderr, commit.sha)];
return [stderr == "", get_error_subjects(stderr)];
}

async function run() {
// Ensure that commisery is installed
await prepare_environment();
try {
await prepare_environment();

let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");

// Validate each commit against Conventional Commit standard
let commits = await get_commits(owner, repo, core.getInput("pull_request"));
let success = true;
// Validate each commit against Conventional Commit standard
let commits = await get_commits(owner, repo, core.getInput("pull_request"));
let success = true;

for (const commit of commits) {
let [valid, errors] = await is_commit_valid(commit);
for (const commit of commits) {
let [valid, errors] = await is_commit_valid(commit);

if (!valid) {
core.summary
.addHeading(commit.commit.message, 2)
.addRaw(
`<b>SHA:</b> <a href="${commit.html_url}"><code>${commit.sha}</code></a>`
);
if (!valid) {
core.summary
.addHeading(commit.commit.message, 2)
.addRaw(
`<b>SHA:</b> <a href="${commit.html_url}"><code>${commit.sha}</code></a>`
);

for (var error of errors) {
core.summary.addCodeBlock(error);
}
for (var error of errors) {
core.summary.addCodeBlock(error);
}

success = false;
success = false;
}
}
}

if (!success) {
core.setFailed(
`Commits in your Pull Request are not compliant to Conventional Commits`
);
if (!success) {
core.setFailed(
`Commits in your Pull Request are not compliant to Conventional Commits`
);

// Post summary
core.summary.write();
// Post summary
core.summary.write();
}
} catch (ex) {
core.setFailed((ex as Error).message);
}
}

Expand Down

0 comments on commit 5ecfd1a

Please sign in to comment.