github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/util.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yaml
    16  
    17  import (
    18  	"bufio"
    19  	"bytes"
    20  	"io"
    21  	"strings"
    22  )
    23  
    24  // helper function that repairs un unparseable `on` section
    25  // of the yaml. The github parser allows a key witout a
    26  // trailing semi-colon, like this:
    27  //
    28  //     on:
    29  //       push
    30  //
    31  // this function converts to this:
    32  //
    33  //     on:
    34  //       push: {}
    35  //
    36  func repairOn(in io.Reader) bytes.Buffer {
    37  	var out bytes.Buffer
    38  
    39  	scanner := bufio.NewScanner(in)
    40  	for scanner.Scan() {
    41  		line := scanner.Text()
    42  
    43  		// here we look for keywords to replace. this is
    44  		// probably in-efficient but is good enough for now.
    45  		temp := strings.TrimSpace(line)
    46  		for _, keyword := range keywords {
    47  			if temp == keyword {
    48  				line = strings.Replace(line, keyword, keyword+": {}", 1)
    49  				break
    50  			}
    51  		}
    52  
    53  		out.WriteString(line)
    54  		out.WriteString("\n")
    55  	}
    56  
    57  	return out
    58  }
    59  
    60  var keywords = []string{
    61  	"branch_protection_rule",
    62  	"check_run",
    63  	"check_suite",
    64  	"create",
    65  	"delete",
    66  	"deployment",
    67  	"deployment_status",
    68  	"discussion",
    69  	"discussion_comment",
    70  	"fork",
    71  	"gollum",
    72  	"issue_comment",
    73  	"issues",
    74  	"label",
    75  	"member",
    76  	"merge_group",
    77  	"milestone",
    78  	"page_build",
    79  	"project",
    80  	"project_card",
    81  	"project_column",
    82  	"public",
    83  	"pull_request",
    84  	"pull_request_review",
    85  	"pull_request_review_comment",
    86  	"pull_request_target",
    87  	"push",
    88  	"registry_package",
    89  	"repository_dispatch",
    90  	"release",
    91  	"schedule",
    92  	"status",
    93  	"watch",
    94  	"workflow_call",
    95  	"workflow_dispatch",
    96  	"workflow_run,omitempty",
    97  }