package main // This action is reading all _patchinfo files, // finds the highest incident number // and updates all _patchinfo files which have not // set their incident attribute to a number. import ( "encoding/xml" "log" "io/ioutil" "os" "strings" "strconv" "path/filepath" "github.com/sethvargo/go-githubactions" ) type PatchinfoSimple struct { XMLName struct{} `xml:"patchinfo"` Incident string `xml:"incident,attr,omitempty"` Version string `xml:"version,attr,omitempty"` Inner []byte `xml:",innerxml"` } func updateIncident(filename string, incident string) { data, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } d := PatchinfoSimple{} if err := xml.Unmarshal(data, &d); err != nil { panic(err) } d.Incident = incident content, err := xml.Marshal(d) if err != nil { panic(err) } // fmt.Println(string(content)) if err := os.WriteFile(filename, content, 0644); err != nil { panic(err) } } func readIncident(filename string) string { data, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } d := PatchinfoSimple{} if err := xml.Unmarshal(data, &d); err != nil { panic(err) } return d.Incident } func main() { root := "." max_incident := 0 files_to_update := []string{} prefix := githubactions.GetInput("prefix") // read all _patchinfo files and find highest existend incident filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err == nil && info.Name() == "_patchinfo" { incident:= readIncident(path) if strings.HasPrefix(incident, prefix) { incident_nr, err := strconv.Atoi(strings.TrimPrefix(incident, prefix)) if err != nil { files_to_update = append(files_to_update, path) } else if max_incident < incident_nr { max_incident = incident_nr } } else { files_to_update = append(files_to_update, path) } } return nil }) // set incident of new _patchinfo files for _, file := range files_to_update { max_incident++ incident := prefix + strconv.Itoa(max_incident) println("Update " + file + " with " + incident) updateIncident(file, incident) } }