From 0f6a43ac80ca6f5ade4474105a5aeaec613e0101 Mon Sep 17 00:00:00 2001 From: Ryota Date: Fri, 3 Sep 2021 12:26:22 +0100 Subject: [PATCH] Refactor with extendable setup for Exporter handling (#49) * Refactor to use regexpplus * Add new placeholder indentation * Add merge-gatekeeper * Add CI setup to run importer update against merge-gatekeeper --- ...rter-markdown-ci.yaml => importer-ci.yaml} | 6 +++++- .github/workflows/merge-gatekeeper.yaml | 19 +++++++++++++++++++ internal/marker/indent.go | 1 + internal/marker/marker.go | 1 + internal/marker/process.go | 19 ++++++++++--------- 5 files changed, 36 insertions(+), 10 deletions(-) rename .github/workflows/{importer-markdown-ci.yaml => importer-ci.yaml} (81%) create mode 100644 .github/workflows/merge-gatekeeper.yaml diff --git a/.github/workflows/importer-markdown-ci.yaml b/.github/workflows/importer-ci.yaml similarity index 81% rename from .github/workflows/importer-markdown-ci.yaml rename to .github/workflows/importer-ci.yaml index e52b505..9916f7e 100644 --- a/.github/workflows/importer-markdown-ci.yaml +++ b/.github/workflows/importer-ci.yaml @@ -31,8 +31,12 @@ jobs: go-version: ${{ env.GO_VERSION }} - name: Run Go Build run: go build ./cmd/importer/ + + # These use locally built Importer, take off `./` for usual use cases + - name: Run Importer against README.md + run: ./importer update README.md - name: Run Importer against README.md - run: ./importer update README.md # This uses locally built Importer, take off `./` for usual use cases + run: ./importer update .github/workflows/merge-gatekeeper.yaml - name: Check if README.md has any change compared to the branch run: | diff --git a/.github/workflows/merge-gatekeeper.yaml b/.github/workflows/merge-gatekeeper.yaml new file mode 100644 index 0000000..79ba4ae --- /dev/null +++ b/.github/workflows/merge-gatekeeper.yaml @@ -0,0 +1,19 @@ +# == imptr: merge-gatekeeper / begin from: https://github.com/upsidr/merge-gatekeeper/blob/main/example/definitions.yaml#[standard-setup] == +--- +name: Merge Gatekeeper + +on: + pull_request: + branches: + - main + - master + +jobs: + merge-gatekeeper: + runs-on: ubuntu-latest + steps: + - name: Run Merge Gatekeeper + uses: upsidr/merge-gatekeeper@main + with: + token: ${{ secrets.GITHUB_TOKEN }} +# == imptr: merge-gatekeeper / end == diff --git a/internal/marker/indent.go b/internal/marker/indent.go index 10e1dce..630ae74 100644 --- a/internal/marker/indent.go +++ b/internal/marker/indent.go @@ -21,6 +21,7 @@ func adjustIndentation(lineData []byte, exporterMarkerIndent int, importerIndent lineData = prependWhitespaces(lineData, importerIndentation.Length) case AlignIndentation: lineData = handleAbsoluteIndentation(lineData, exporterMarkerIndent, importerIndentation.MarkerIndentation) + case KeepIndentation: // Explicitly handling this, as it is likely that the default behaviour woulld need to change } lineData = append(lineData, br) return lineData diff --git a/internal/marker/marker.go b/internal/marker/marker.go index 75a64a3..c9937f0 100644 --- a/internal/marker/marker.go +++ b/internal/marker/marker.go @@ -43,6 +43,7 @@ const ( AbsoluteIndentation IndentationMode = iota + 1 ExtraIndentation AlignIndentation + KeepIndentation ) // Indentation holds additional indentation handling option. diff --git a/internal/marker/process.go b/internal/marker/process.go index f66d205..07967a1 100644 --- a/internal/marker/process.go +++ b/internal/marker/process.go @@ -9,7 +9,6 @@ import ( "net/url" "os" "path/filepath" - "regexp" "strings" "github.com/upsidr/importer/internal/regexpplus" @@ -69,7 +68,6 @@ const br = byte('\n') func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) { result := []byte{} - reExport := regexp.MustCompile(ExporterMarkerMarkdown) withinExportMarker := false currentLine := 0 @@ -78,14 +76,18 @@ func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) { currentLine++ // Find Exporter Marker - match := reExport.FindStringSubmatch(scanner.Text()) - if len(match) != 0 { - // match[1] is export_marker_name - if match[1] == m.TargetExportMarker { + matches, err := regexpplus.MapWithNamedSubgroups(scanner.Text(), ExporterMarkerMarkdown) + if err != nil && !errors.Is(err, regexpplus.ErrNoMatch) { + panic(err) // Unknown error, should not happen + } + + if len(matches) != 0 { + if exporterName, found := matches["export_marker_name"]; found && + exporterName == m.TargetExportMarker { withinExportMarker = true } - // match[2] is exporter_marker_condition - if match[2] == "end" { + if exporterCondition, found := matches["exporter_marker_condition"]; found && + exporterCondition == "end" { withinExportMarker = false } continue @@ -119,7 +121,6 @@ func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) { func (m *Marker) processSingleMarkerYAML(file io.Reader) ([]byte, error) { result := []byte{} - // reExport := regexp.MustCompile(ExporterMarkerYAML) isNested := false nestedUnder := "" exporterMarkerIndentation := 0