generated from nginxinc/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nginxinc/import-repo
Importing relevant code from a private repo.
- Loading branch information
Showing
49 changed files
with
22,123 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Daily Reference Converter Run | ||
|
||
on: | ||
schedule: | ||
- cron: '0 14 * * *' # Run daily at 2pm UTC | ||
workflow_dispatch: | ||
|
||
defaults: | ||
run: | ||
working-directory: ./reference-converter | ||
shell: bash | ||
|
||
jobs: | ||
compare_outputs: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: make devtools-image | ||
- name: let the build create files | ||
run: chmod 777 . | ||
- run: make build | ||
- name: run converter | ||
run: ./dist/reference-converter -dst ./output/reference.json | ||
|
||
- name: diff reference.json | ||
id: diff | ||
run: | | ||
if ! diff -q <(grep -v '"version":' ../reference-lib/src/reference.json) <(grep -v '"version":' ./output/reference.json); then | ||
echo "reference_change=true" >> "$GITHUB_OUTPUT" | ||
mv ./output/reference.json ../reference-lib/src/reference.json | ||
fi | ||
- uses: actions/setup-node@v3 | ||
- name: update npm package version | ||
if: steps.diff.outputs.reference_change | ||
run: npm version patch --no-git-tag-version | ||
working-directory: ./reference-lib | ||
|
||
- name: create pull request if reference.json changed | ||
uses: peter-evans/create-pull-request@v3 | ||
if: steps.diff.outputs.reference_change | ||
with: | ||
commit-message: update reference.json | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
branch: reference-update | ||
title: "Reference Json Update" | ||
body: "Changes detected in reference.json" | ||
delete-branch: true | ||
add-paths: reference-lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
branches: ["main"] | ||
paths: | ||
[ | ||
"reference-converter/**", | ||
".github/workflows/reference-converter.yml", | ||
".golangci.yml", | ||
] | ||
|
||
defaults: | ||
run: | ||
working-directory: ./reference-converter | ||
shell: bash | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
env: | ||
testResults: ./reference-converter/results/test.xml | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: make devtools-image | ||
- name: let the build create files | ||
run: chmod 777 . | ||
- run: make build | ||
- run: make test | ||
- name: archive test artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: test-report | ||
path: ${{ env.testResults }} | ||
- name: Test Report | ||
uses: dorny/test-reporter@v1 | ||
if: success() || failure() | ||
with: | ||
name: Test Report | ||
path: ${{ env.testResults }} | ||
reporter: jest-junit | ||
lint: | ||
runs-on: ubuntu-latest | ||
env: | ||
working-directory: ./reference-converter | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: ${{ env.working-directory }}/go.mod | ||
cache: false | ||
- name: use the same version of GOLANGCI_LINT_VERSION as devtools | ||
run: make devtools-versions >> "$GITHUB_ENV" | ||
- uses: golangci/golangci-lint-action@v3 | ||
with: | ||
working-directory: ${{ env.working-directory }} | ||
version: v${{ env.GOLANGCI_LINT_VERSION }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Publish reference lib package to GitHub Packages | ||
on: | ||
push: | ||
branches: ['main'] | ||
paths: ['reference-lib/**'] | ||
pull_request: | ||
branches: ['main'] | ||
paths: ['reference-lib/**'] | ||
|
||
defaults: | ||
run: | ||
working-directory: ./reference-lib | ||
shell: bash | ||
|
||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '16.x' | ||
registry-url: 'https://npm.pkg.github.com' | ||
scope: '@nginxinc' | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Bundle using rollup | ||
run: npm run build | ||
- name: publish package | ||
if: github.event_name == 'push' | ||
run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
linters: | ||
# add non-default linters https://golangci-lint.run/usage/linters/#disabled-by-default | ||
enable: | ||
- bodyclose | ||
- errorlint | ||
|
||
issues: | ||
# Disable maximum issues count per one linter | ||
max-issues-per-linter: 0 | ||
|
||
# Disable maximum count of issues with the same text | ||
max-same-issues: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
results | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
ROOT_DIR:=$(shell git rev-parse --show-toplevel) | ||
include $(ROOT_DIR)/tools/devtools/Makefile | ||
|
||
.PHONY: build test lint | ||
|
||
build: | ||
@$(MAKE) .run args="go build -v -o dist/ ." | ||
|
||
test: | ||
@$(MAKE) .run args="./test.sh results/test.xml" | ||
|
||
lint: | ||
@$(MAKE) .run args="golangci-lint run -v" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Reference Converter | ||
|
||
This program converts the NGINX reference documentation from it's XML schema to JSON. The generated JSON is available as an npm package in the reference-lib folder and can be used for static content generation, markdoc tags, monaco plugins, etc. | ||
|
||
## Design | ||
|
||
```mermaid | ||
flowchart | ||
fetch_atom[read latest version from atom feed] | ||
read_datafile[read version from data file] | ||
version_matches{do the versions match?} | ||
download_tarball[download tarball of all XML] | ||
parse_xml[parse XML] | ||
render_md[translate prose to markdown] | ||
write[write JSON to disk] | ||
done((done)) | ||
read_datafile --> version_matches | ||
fetch_atom --> version_matches | ||
version_matches -->|Y| done | ||
version_matches -->|N| download_tarball --> parse_xml --> render_md --> write --> done | ||
``` | ||
|
||
The NGINX docs are publicly available at <http://hg.nginx.org/nginx.org>, in XML that's a mix of data and prose (`<para>` tags contain markup). The `<para>` contents will be translated in-order to generate equivalent markdown. | ||
|
||
The atom feed at <http://hg.nginx.org/nginx.org/atom-log> will tell us if there is updated content. | ||
|
||
A scheduled github pipeline ensures that we have up-to-date reference information. | ||
|
||
```mermaid | ||
flowchart | ||
run[./reference-converter] | ||
diff{git diff shows changes?} | ||
open[open a PR with the changes] | ||
slack[send slack notification] | ||
done((done)) | ||
run --> diff -->|N| done | ||
diff -->|Y| open --> slack --> done | ||
run -->|errored out| slack | ||
``` | ||
|
||
## Usage | ||
|
||
```bash | ||
make devtools-image | ||
make build | ||
./dist/reference-converter --dst <output-path> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package atom | ||
|
||
import ( | ||
"context" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type entry struct { | ||
Link struct { | ||
Href string `xml:"href,attr"` | ||
} `xml:"link"` | ||
} | ||
|
||
type feed struct { | ||
Entry []entry `xml:"entry"` | ||
} | ||
|
||
type config struct { | ||
Client http.Client | ||
} | ||
type Option = func(*config) | ||
|
||
// WithHttpClient uses the provided client instead of the default for | ||
// downloading tarballs. | ||
func WithHttpClient(c http.Client) Option { | ||
return func(o *config) { o.Client = c } | ||
} | ||
|
||
// GetVersion gets the first link of the first entry of the XML file | ||
func GetVersion(ctx context.Context, url string, opts ...Option) (string, error) { | ||
cfg := &config{} | ||
for _, opt := range opts { | ||
opt(cfg) | ||
} | ||
body, _ := openURL(ctx, url, cfg.Client) | ||
return parseXML(body) | ||
} | ||
func openURL(ctx context.Context, url string, client http.Client) ([]byte, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to download %s: %w", url, err) | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unable to download %s: %s", url, res.Status) | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read response body: %w", err) | ||
} | ||
return body, nil | ||
} | ||
func parseXML(XMLContent []byte) (string, error) { | ||
var f feed | ||
err := xml.Unmarshal(XMLContent, &f) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to parse XML: %w", err) | ||
} | ||
if len(f.Entry) > 0 { | ||
return f.Entry[0].Link.Href, nil | ||
} | ||
return "", fmt.Errorf("no entry was found in the feed") | ||
} |
Oops, something went wrong.