Skip to content

Commit fd61c9f

Browse files
authored
feat: create a github action to create a bcr entry (#236)
1 parent 31d2f41 commit fd61c9f

File tree

17 files changed

+578
-73
lines changed

17 files changed

+578
-73
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pnpm-lock.yaml linguist-generated
22
dist/cli/index.js linguist-generated
3+
dist/action/index.js linguist-generated

.github/workflows/action-e2e.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# End-to-end tests for the Publish to BCR custom GitHub action
2+
name: action-e2e
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
workflow_dispatch:
9+
concurrency:
10+
# Cancel previous actions from the same PR or branch except 'main' branch.
11+
# See https://docs.github.com/en/actions/using-jobs/using-concurrency and https://docs.github.com/en/actions/learn-github-actions/contexts for more info.
12+
group: concurrency-group::${{ github.workflow }}::${{ github.event.pull_request.number > 0 && format('pr-{0}', github.event.pull_request.number) || github.ref_name }}${{ github.ref_name == 'main' && format('::{0}', github.run_id) || ''}}
13+
cancel-in-progress: ${{ github.ref_name != 'main' }}
14+
jobs:
15+
# Each job is an e2e test. Unfortunately a full workflow cannot be added as
16+
# required status check on branch protection rules, so each test must be added
17+
# as a required check: https://github.com/orgs/community/discussions/12395.
18+
test-happy-path:
19+
# Create a new module entry and test the content
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v2
23+
with:
24+
path: this
25+
- name: Setup test fixture
26+
run: this/e2e/action/setup-test-fixture.sh versioned versioned-1.0.0
27+
- name: Create registry
28+
run: |
29+
mkdir -p bazel-central-registry/modules
30+
cd bazel-central-registry
31+
git init
32+
- name: Create entry
33+
uses: ./this
34+
with:
35+
tag: v1.0.0
36+
module-version: 1.0.0
37+
github-repository: foobar/versioned
38+
templates-dir: this/e2e/fixtures/versioned/.bcr
39+
local-registry: bazel-central-registry
40+
- name: Test entry content
41+
run: this/e2e/action/test-happy-path-content.sh
42+
test-github-repository-default:
43+
# Test that the `github-repository` input defaults to ${{ github.repository }}
44+
# indirectly by checking the resulting subtituted source.json file in the entry.
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v2
48+
with:
49+
path: this
50+
- name: Setup test fixture
51+
run: this/e2e/action/setup-test-fixture.sh versioned publish-to-bcr-1.0.0
52+
- name: Create registry
53+
run: |
54+
mkdir -p bazel-central-registry/modules
55+
cd bazel-central-registry
56+
git init
57+
- name: Create entry
58+
uses: ./this
59+
with:
60+
tag: v1.0.0
61+
module-version: 1.0.0
62+
templates-dir: this/e2e/fixtures/versioned/.bcr
63+
local-registry: bazel-central-registry
64+
- name: Test repository substitution
65+
run: this/e2e/action/test-github-repository-default-substitution.sh

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dist/*
12
pnpm-lock.yaml
23
templates/.bcr
34
templates/README.md

action.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: 'Publish to BCR'
2+
description: 'Create a new module version entry in a Bazel registry'
3+
inputs:
4+
github-repository:
5+
description: 'GitHub repository for the module being published. Used to substititue the OWNER and REPO vars into the source template. Defaults to the repository this action runs in.'
6+
required: false
7+
default: ${{ github.repository }}
8+
local-registry:
9+
description: 'Path to a locally checked out registry where the entry files will be created.'
10+
required: true
11+
metadata-template:
12+
description: 'Content of the metadata template. An alternative to supplying a metadata.template.json file in the templates dir.'
13+
required: false
14+
default: ''
15+
module-version:
16+
description: 'The module version to publish to the registry.'
17+
required: true
18+
patch:
19+
description: 'Patch content to apply to the release archive. An alternative to supplying a patches folder in the templates dir. The GitHub action is limited to a single patch input so all patches should be merged into one.'
20+
required: false
21+
default: ''
22+
presubmit:
23+
description: 'Content of the presubmit. An alternative to supplying a presubmit.yml file in the templates dir.'
24+
required: false
25+
default: ''
26+
source-template:
27+
description: 'Content of the source template. An alternative to supplying a source.template.json file in the templates dir.'
28+
required: false
29+
default: ''
30+
tag:
31+
description: "Tag of the the module repository's release. Used for substitution in the source template."
32+
required: false
33+
default: ''
34+
templates-dir:
35+
description: 'Directory containing BCR release template files: metadata.template.json, source.template.json, presubmit.yaml, patches/. Equivalent to the .bcr directory required by the legacy GitHub app.'
36+
required: false
37+
default: ''
38+
runs:
39+
using: 'node20'
40+
main: 'dist/action/index.js'

dist/action/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
2+
3+
write_source_file(
4+
name = "action",
5+
in_file = "//src/application/action:bundle",
6+
out_file = "index.js",
7+
)

dist/action/index.js

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cli/index.js

Lines changed: 68 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/action/setup-test-fixture.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -o errexit -o nounset -o pipefail -o xtrace
3+
4+
FIXTURE="${1}"
5+
STRIP_PREFIX="${2}"
6+
7+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
8+
ACTION_REPO_PATH="${SCRIPT_DIR}/../../"
9+
10+
# Create a release archive from a fixture
11+
tar -czvf archive.tar.gz -C "${ACTION_REPO_PATH}/e2e/fixtures/${FIXTURE}" --transform "s,^./,${STRIP_PREFIX}/," --sort=name --owner=root:0 --group=root:0 --mtime="UTC 1980-02-01" .
12+
13+
# Substitute the archive url to a local file path
14+
cat "${ACTION_REPO_PATH}/e2e/fixtures/${FIXTURE}/.bcr/source.template.json" | jq ".url = \"file://$(realpath archive.tar.gz)\"" > "/tmp/source.template.json"
15+
mv /tmp/source.template.json "${ACTION_REPO_PATH}/e2e/fixtures/${FIXTURE}/.bcr/source.template.json"
16+
cat "${ACTION_REPO_PATH}/e2e/fixtures/${FIXTURE}/.bcr/source.template.json"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -o errexit -o nounset -o pipefail -o xtrace
3+
4+
STRIP_PREFIX="$(cat bazel-central-registry/modules/versioned/1.0.0/source.json | jq -r ".strip_prefix")"
5+
EXPECTED="publish-to-bcr-1.0.0"
6+
if [ "${STRIP_PREFIX}" != "${EXPECTED}" ]; then
7+
echo "Incorrect strip prefix"
8+
echo "Expected ${EXPECTED} but got ${STRIP_PREFIX}"
9+
exit 1
10+
fi
11+
echo "Success"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
set -o errexit -o nounset -o pipefail -o xtrace
3+
4+
EXPECTED_PATH="expected"
5+
mkdir -p "${EXPECTED_PATH}/1.0.0"
6+
7+
cat > "${EXPECTED_PATH}/metadata.json" <<- EOF
8+
{
9+
"homepage": "https://github.com/testorg/versioned",
10+
"maintainers": [
11+
{
12+
"name": "Foo McBar",
13+
"email": "[email protected]",
14+
"github": "foobar"
15+
}
16+
],
17+
"repository": [
18+
"github:testorg/versioned"
19+
],
20+
"versions": [
21+
"1.0.0"
22+
],
23+
"yanked_versions": {}
24+
}
25+
EOF
26+
cat > "${EXPECTED_PATH}/1.0.0/MODULE.bazel" <<- EOF
27+
module(
28+
name = "versioned",
29+
version = "1.0.0",
30+
)
31+
EOF
32+
cat > "${EXPECTED_PATH}/1.0.0/source.json" <<- EOF
33+
{
34+
"integrity": "sha256-t39EUhiPjM65oWhYtLQ2dq4fWDU/oQ0L/DNBvYkNfyk=",
35+
"strip_prefix": "versioned-1.0.0",
36+
"url": "file:///home/runner/work/publish-to-bcr/publish-to-bcr/archive.tar.gz"
37+
}
38+
EOF
39+
cat > "${EXPECTED_PATH}/1.0.0/presubmit.yml" <<- EOF
40+
bcr_test_module:
41+
module_path: "e2e/bzlmod"
42+
matrix:
43+
platform: ["debian10", "macos", "ubuntu2004", "windows"]
44+
bazel: [6.x, 7.x]
45+
tasks:
46+
run_tests:
47+
name: "Run test module"
48+
platform: \${{ platform }}
49+
bazel: \${{ bazel }}
50+
test_targets:
51+
- "//..."
52+
EOF
53+
54+
ENTRY_PATH="bazel-central-registry/modules/versioned"
55+
56+
diff <(jq --sort-keys . "${ENTRY_PATH}/metadata.json") <(jq --sort-keys . "${EXPECTED_PATH}/metadata.json")
57+
diff <(jq --sort-keys . "${ENTRY_PATH}/1.0.0/source.json") <(jq --sort-keys . "${EXPECTED_PATH}/1.0.0/source.json")
58+
diff "${ENTRY_PATH}/1.0.0/MODULE.bazel" "${EXPECTED_PATH}/1.0.0/MODULE.bazel"
59+
diff "${ENTRY_PATH}/1.0.0/presubmit.yml" "${EXPECTED_PATH}/1.0.0/presubmit.yml"

0 commit comments

Comments
 (0)