Skip to content

Commit b44eec6

Browse files
committed
feat(actions): add working-directory
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 5aaefd1 commit b44eec6

11 files changed

Lines changed: 246 additions & 35 deletions

.github/workflows/__shared-ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ jobs:
2020
contents: read
2121
uses: ./.github/workflows/__test-action-matrix-outputs.yml
2222

23+
test-action-get-github-actions-bot-user:
24+
needs: linter
25+
permissions:
26+
contents: read
27+
uses: ./.github/workflows/__test-action-get-github-actions-bot-user.yml
28+
29+
test-action-get-issue-number:
30+
needs: linter
31+
permissions:
32+
contents: read
33+
uses: ./.github/workflows/__test-action-get-issue-number.yml
34+
2335
test-action-parse-ci-reports:
2436
needs: linter
2537
permissions:
@@ -41,3 +53,9 @@ jobs:
4153
permissions:
4254
contents: read
4355
uses: ./.github/workflows/__test-action-slugify.yml
56+
57+
test-action-working-directory:
58+
needs: linter
59+
permissions:
60+
contents: read
61+
uses: ./.github/workflows/__test-action-working-directory.yml
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Internal - Tests for get-github-actions-bot-user action
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
tests:
11+
name: Tests for get-github-actions-bot-user action
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Arrange - Checkout repository
15+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16+
with:
17+
persist-credentials: false
18+
19+
- name: Act - Run get-github-actions-bot-user action
20+
id: get-github-actions-bot-user
21+
uses: ./actions/get-github-actions-bot-user
22+
23+
- name: Assert - Check get-github-actions-bot-user outputs
24+
env:
25+
STEPS_GET_GITHUB_ACTIONS_BOT_USER_OUTPUTS_NAME: ${{ steps.get-github-actions-bot-user.outputs.name }}
26+
STEPS_GET_GITHUB_ACTIONS_BOT_USER_OUTPUTS_EMAIL: ${{ steps.get-github-actions-bot-user.outputs.email }}
27+
run: |
28+
if [ "${STEPS_GET_GITHUB_ACTIONS_BOT_USER_OUTPUTS_NAME}" != 'github-actions[bot]' ]; then
29+
echo "get-github-actions-bot-user output name is not valid"
30+
exit 1
31+
fi
32+
33+
if [ "${STEPS_GET_GITHUB_ACTIONS_BOT_USER_OUTPUTS_EMAIL}" != '41898282+github-actions[bot]@users.noreply.github.com' ]; then
34+
echo "get-github-actions-bot-user output email is not valid"
35+
exit 1
36+
fi
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Internal - Tests for get-issue-number action
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
tests:
11+
name: Tests for get-issue-number action
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Arrange - Checkout repository
15+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16+
with:
17+
persist-credentials: false
18+
19+
- name: Act - Run get-issue-number action
20+
id: get-issue-number
21+
continue-on-error: true
22+
uses: ./actions/get-issue-number
23+
24+
- name: Assert - Check get-issue-number behavior by event type
25+
env:
26+
STEPS_GET_ISSUE_NUMBER_OUTCOME: ${{ steps.get-issue-number.outcome }}
27+
STEPS_GET_ISSUE_NUMBER_OUTPUTS_ISSUE_NUMBER: ${{ steps.get-issue-number.outputs.issue-number }}
28+
EXPECTED_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
29+
run: |
30+
if [ "${GITHUB_EVENT_NAME}" = 'pull_request' ]; then
31+
if [ "${STEPS_GET_ISSUE_NUMBER_OUTCOME}" != 'success' ]; then
32+
echo "get-issue-number should succeed for pull_request events"
33+
exit 1
34+
fi
35+
36+
if [ "${STEPS_GET_ISSUE_NUMBER_OUTPUTS_ISSUE_NUMBER}" != "${EXPECTED_PULL_REQUEST_NUMBER}" ]; then
37+
echo "get-issue-number output is not valid for pull_request events"
38+
exit 1
39+
fi
40+
elif [ "${STEPS_GET_ISSUE_NUMBER_OUTCOME}" != 'failure' ]; then
41+
echo "get-issue-number should fail when event is not pull_request"
42+
exit 1
43+
fi

.github/workflows/__test-action-parse-ci-reports.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ jobs:
2525
name: Tests for parse-ci-reports action
2626
runs-on: ubuntu-latest
2727
steps:
28-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
28+
- name: Arrange - Checkout repository
29+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2930
with:
3031
persist-credentials: false
3132

@@ -71,7 +72,8 @@ jobs:
7172
]
7273
EOF
7374
74-
- id: parse-ci-reports
75+
- name: Act - Run parse-ci-reports action
76+
id: parse-ci-reports
7577
uses: ./actions/parse-ci-reports
7678
with:
7779
report-paths: |
@@ -81,7 +83,7 @@ jobs:
8183
report-name: "Test Reports"
8284
output-format: "summary,markdown"
8385

84-
- name: Check parse-ci-reports outputs
86+
- name: Assert - Check parse-ci-reports outputs
8587
run: |
8688
# Check that markdown output exists
8789
if [ -z "${STEPS_PARSE_CI_REPORTS_OUTPUTS_MARKDOWN}" ]; then

.github/workflows/__test-action-repository-owner-is-organization.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ jobs:
1111
name: Tests for repository-owner-is-organization action
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
14+
- name: Arrange - Checkout repository
15+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1516
with:
1617
persist-credentials: false
1718

18-
- id: repository-owner-is-organization
19+
- name: Act - Run repository-owner-is-organization action
20+
id: repository-owner-is-organization
1921
uses: ./actions/repository-owner-is-organization
2022

21-
- name: Check repository-owner-is-organization outputs
23+
- name: Assert - Check repository-owner-is-organization outputs
24+
env:
25+
STEPS_REPOSITORY_OWNER_IS_ORGANIZATION_OUTPUTS_IS_ORGANIZATION: ${{ steps.repository-owner-is-organization.outputs.is-organization }}
2226
run: |
2327
if [ "${STEPS_REPOSITORY_OWNER_IS_ORGANIZATION_OUTPUTS_IS_ORGANIZATION}" != 'true' ]; then
2428
echo "repository-owner-is-organization outputs result is not valid"
2529
exit 1
2630
fi
27-
env:
28-
STEPS_REPOSITORY_OWNER_IS_ORGANIZATION_OUTPUTS_IS_ORGANIZATION: ${{ steps.repository-owner-is-organization.outputs.is-organization }}

.github/workflows/__test-action-slugify.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ jobs:
1111
name: Tests for slugify action
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
14+
- name: Arrange - Checkout repository
15+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1516
with:
1617
persist-credentials: false
1718

18-
- id: slugify
19+
- name: Act - Run slugify action
20+
id: slugify
1921
uses: ./actions/slugify
2022
with:
2123
value: test content
2224

23-
- name: Check slugify outputs
25+
- name: Assert - Check slugify outputs
2426
run: |
2527
if [ "${STEPS_SLUGIFY_OUTPUTS_RESULT}" != 'test-content' ]; then
2628
echo "Slugify outputs result is not valid"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Internal - Tests for working-directory action
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
tests:
11+
name: Tests for working-directory action
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Arrange - Checkout repository
15+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16+
with:
17+
persist-credentials: false
18+
19+
- name: Act - Resolve workspace root directory
20+
id: working-directory
21+
uses: ./actions/working-directory
22+
with:
23+
working-directory: .
24+
25+
- name: Assert - Check workspace root outputs
26+
run: |
27+
if [ "${STEPS_WORKING_DIRECTORY_OUTPUTS_ABSOLUTE_PATH}" != "${GITHUB_WORKSPACE}" ]; then
28+
echo "working-directory absolute-path output is not valid"
29+
exit 1
30+
fi
31+
32+
if [ "${STEPS_WORKING_DIRECTORY_OUTPUTS_WORKSPACE_RELATIVE_PATH}" != "." ]; then
33+
echo "working-directory workspace-relative-path output is not valid"
34+
exit 1
35+
fi
36+
env:
37+
STEPS_WORKING_DIRECTORY_OUTPUTS_ABSOLUTE_PATH: ${{ steps.working-directory.outputs.absolute-path }}
38+
STEPS_WORKING_DIRECTORY_OUTPUTS_WORKSPACE_RELATIVE_PATH: ${{ steps.working-directory.outputs.workspace-relative-path }}
39+
40+
- name: Act - Run outside-workspace path with enforcement enabled
41+
id: outside-workspace-disallowed
42+
continue-on-error: true
43+
uses: ./actions/working-directory
44+
with:
45+
working-directory: /tmp
46+
enforce-path-in-workspace: true
47+
48+
- name: Assert - Check enforcement failure outside workspace
49+
run: |
50+
if [ "${STEPS_OUTSIDE_WORKSPACE_DISALLOWED_OUTCOME}" != "failure" ]; then
51+
echo "working-directory should fail when path is outside workspace and enforcement is enabled"
52+
exit 1
53+
fi
54+
env:
55+
STEPS_OUTSIDE_WORKSPACE_DISALLOWED_OUTCOME: ${{ steps.outside-workspace-disallowed.outcome }}
56+
57+
- name: Act - Run outside-workspace path with enforcement disabled
58+
id: outside-workspace-allowed
59+
uses: ./actions/working-directory
60+
with:
61+
working-directory: /tmp
62+
enforce-path-in-workspace: false
63+
64+
- name: Assert - Check enforcement opt-out outside workspace
65+
run: |
66+
if [ "${STEPS_OUTSIDE_WORKSPACE_ALLOWED_OUTPUTS_ABSOLUTE_PATH}" != "/tmp" ]; then
67+
echo "working-directory absolute-path output is not valid when enforcement is disabled"
68+
exit 1
69+
fi
70+
71+
if [[ "${STEPS_OUTSIDE_WORKSPACE_ALLOWED_OUTPUTS_WORKSPACE_RELATIVE_PATH}" != ..* ]]; then
72+
echo "working-directory workspace-relative-path should be outside workspace when enforcement is disabled"
73+
exit 1
74+
fi
75+
env:
76+
STEPS_OUTSIDE_WORKSPACE_ALLOWED_OUTPUTS_ABSOLUTE_PATH: ${{ steps.outside-workspace-allowed.outputs.absolute-path }}
77+
STEPS_OUTSIDE_WORKSPACE_ALLOWED_OUTPUTS_WORKSPACE_RELATIVE_PATH: ${{ steps.outside-workspace-allowed.outputs.workspace-relative-path }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Opinionated GitHub Actions and reusable workflows for foundational continuous-in
3636

3737
- [Parse CI reports](actions/parse-ci-reports/README.md) - parses CI reports (tests, linting, coverage) into GitHub summaries and Markdown for PR comments.
3838
- [Repository owner is organization](actions/repository-owner-is-organization/README.md) - checks whether the repository owner is an organization.
39-
- [Working directory](actions/working-directory/README.md) - resolves and validates a working directory path as an absolute path.
39+
- [Working directory](actions/working-directory/README.md) - resolves and validates a working directory path, exposing absolute and workspace-relative outputs.
4040
- [Slugify](actions/slugify/README.md) - converts free-form strings into GitHub-friendly slugs.
4141

4242
## Reusable Workflows

actions/get-issue-number/action.yml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ branding:
88
outputs:
99
issue-number:
1010
description: "The issue number."
11-
value: ${{ steps.get-issue-number.outputs.result }}
11+
value: ${{ steps.get-issue-number.outputs.issue-number }}
1212

1313
runs:
1414
using: "composite"
1515
steps:
1616
- id: get-issue-number
17-
shell: bash
18-
run: |
19-
if [ ! -z "${{ github.event.pull_request.number }}" ]; then
20-
echo "result=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
21-
elif [ ! -z "${{ github.event.issue.number }}" ]; then
22-
echo "result=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
23-
else
24-
echo "No PR or issue number found for the current event"
25-
exit 1
26-
fi
17+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
18+
with:
19+
script: |
20+
const pullRequestNumber = context.payload.pull_request?.number;
21+
const issueNumber = context.payload.issue?.number;
22+
const result = pullRequestNumber ?? issueNumber;
23+
24+
if (!result) {
25+
core.setFailed('No PR or issue number found for the current event');
26+
return;
27+
}
28+
29+
core.setOutput('issue-number', result);

actions/working-directory/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ Action to resolve and validate a working directory path.
4242
4343
## Inputs
4444
45-
| **Input** | **Description** | **Required** | **Default** |
46-
| ----------------------- | ------------------------------------------------------- | ------------ | ----------- |
47-
| **`working-directory`** | Relative or absolute working directory path to resolve. | **false** | `.` |
45+
| **Input** | **Description** | **Required** | **Default** |
46+
| ------------------------------- | --------------------------------------------------------------------- | ------------ | ----------- |
47+
| **`working-directory`** | Relative or absolute working directory path to resolve. | **false** | `.` |
48+
| **`enforce-path-in-workspace`** | Whether to fail when the resolved path is outside `GITHUB_WORKSPACE`. | **false** | `true` |
4849

4950
<!-- inputs:end -->
5051
<!-- secrets:start -->
@@ -53,9 +54,10 @@ Action to resolve and validate a working directory path.
5354

5455
## Outputs
5556

56-
| **Output** | **Description** |
57-
| ----------------------- | --------------------------------------------- |
58-
| **`working-directory`** | The resolved absolute working directory path. |
57+
| **Output** | **Description** |
58+
| ----------------------------- | ------------------------------------------------------------------- |
59+
| **`absolute-path`** | The resolved absolute working directory path. |
60+
| **`workspace-relative-path`** | The resolved working directory path relative to `GITHUB_WORKSPACE`. |
5961

6062
<!-- outputs:end -->
6163
<!-- examples:start -->

0 commit comments

Comments
 (0)