-
Notifications
You must be signed in to change notification settings - Fork 3
Cleanup - Initial Project Refactor #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
351f0e9
58c87a8
151d16e
6e7530b
5c38b06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/kuhlman-labs/gh-commit-remap | ||
module github.com/mona-actions/gh-commit-remap | ||
|
||
go 1.22.5 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package commitremap | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Struct to represent a single entry in the commit map | ||
type CommitMapEntry struct { | ||
Old string | ||
New string | ||
} | ||
|
||
// Parses the file and returns a map of old commit hashes to new commit hashes | ||
func ParseCommitMap(filePath string) (*[]CommitMapEntry, error) { | ||
commitMap := []CommitMapEntry{} | ||
|
||
// Read the commit-map file | ||
content, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Split the file content into lines | ||
lines := strings.Split(string(content), "\n") | ||
|
||
// Iterate over the lines and parse the old and new commit hashes | ||
for _, line := range lines { | ||
if strings.TrimSpace(line) == "" { | ||
continue | ||
} | ||
|
||
fields := strings.Fields(line) | ||
if len(fields) != 2 { | ||
return nil, fmt.Errorf("invalid line: %s", line) | ||
} | ||
|
||
commitMap = append(commitMap, CommitMapEntry{ | ||
Old: fields[0], | ||
New: fields[1], | ||
}) | ||
} | ||
return &commitMap, nil | ||
} | ||
|
||
func ProcessFiles(archiveLocation string, prefixes []string, commitMap *[]CommitMapEntry) { | ||
|
||
for _, prefix := range prefixes { | ||
// Get a list of all files that match the pattern | ||
files, err := filepath.Glob(filepath.Join(archiveLocation, prefix+"_*.json")) | ||
if err != nil { | ||
log.Fatalf("Error getting files: %v", err) | ||
} | ||
|
||
// Process each file | ||
for _, file := range files { | ||
log.Println("Processing file:", file) | ||
|
||
updateMetadataFile(file, commitMap) | ||
} | ||
} | ||
} | ||
|
||
func updateMetadataFile(filePath string, commitMap *[]CommitMapEntry) { | ||
// Read the JSON file | ||
data, err := os.ReadFile(filePath) | ||
if err != nil { | ||
log.Fatalf("Error reading data: %v", err) | ||
} | ||
|
||
var dataMap interface{} | ||
err = json.Unmarshal(data, &dataMap) | ||
if err != nil { | ||
log.Fatalf("Error unmarshaling data: %v", err) | ||
} | ||
|
||
// Iterate over the commit map and replace the old commit hashes with the new ones | ||
for _, commit := range *commitMap { | ||
replaceSHA(dataMap, commit.Old, commit.New) | ||
} | ||
|
||
// Marshal the updated data to JSON and pretty print it | ||
updatedData, err := json.MarshalIndent(dataMap, "", " ") | ||
if err != nil { | ||
log.Fatalf("Error marshaling updated data: %v", err) | ||
} | ||
|
||
// Overwrite the original file with the updated data | ||
err = os.WriteFile(filePath, updatedData, 0644) | ||
if err != nil { | ||
log.Fatalf("Error writing updated data: %v", err) | ||
} | ||
} | ||
|
||
func replaceSHA(data interface{}, oldSHA string, newSHA string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a later PR, I would like to rework this function as I'm getting super spooked around the usage of empty interfaces here (e.g., any type) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ssulei7 good catch! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
if data == nil { | ||
return | ||
} | ||
|
||
switch v := data.(type) { | ||
case map[string]interface{}: | ||
for key, value := range v { | ||
if str, ok := value.(string); ok && str == oldSHA { | ||
v[key] = newSHA | ||
} else { | ||
replaceSHA(value, oldSHA, newSHA) | ||
} | ||
} | ||
case []interface{}: | ||
for i, value := range v { | ||
if str, ok := value.(string); ok && str == oldSHA { | ||
v[i] = newSHA | ||
} else { | ||
replaceSHA(value, oldSHA, newSHA) | ||
} | ||
} | ||
default: | ||
// Unsupported type, do nothing | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.