Skip to content

Commit

Permalink
Parse the changed files JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
codingkarthik committed Jul 16, 2024
1 parent 9fd2eb0 commit 42d9c9c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
53 changes: 22 additions & 31 deletions .github/workflows/registry-updates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,27 @@ jobs:
run: |
cat changed_files.txt
# - name: Get the actual changes in the files
# run: |
# echo "Fetching actual changes ..."
# while read -r FILE; do
# echo "Changes in $FILE: "
# git diff $BASE_SHA $HEAD_SHA -- "$FILE"
# done < changed_files.txt
# env:
# BASE_SHA: ${{ steps.list_files.outputs.base }}
# HEAD_SHA: ${{ steps.list_files.outputs.head }}

# - name: Pseudocode for Next steps
# id: next_steps
# run: |
# echo 'Detect status - added/modified/removed files'
# echo 'for each removed connector, remove from registry db'
# echo 'for each added connector, create a stub in the registry db'
# echo 'for each modified connector:'
# echo ' * Download tgz'
# echo ' * Re-upload tgz'
# echo ' * Extract'
# echo ' * Build payload for API'
# echo ' * PUT to API (gql)'
- name: Pseudocode for Next steps
id: next_steps
run: |
echo 'Detect status - added/modified/removed files'
echo 'for each removed connector, remove from registry db'
echo 'for each added connector, create a stub in the registry db'
echo 'for each modified connector:'
echo ' * Download tgz'
echo ' * Re-upload tgz'
echo ' * Extract'
echo ' * Build payload for API'
echo ' * PUT to API (gql)'
# - name: Setup Go
# uses: actions/setup-go@v4
# with:
# go-version: 1.21.x
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21.x

# - name: Run registry automaion program
# run: |
# cd registry-automation
# go run main.go ci
- name: Run registry automation program
env:
CHANGED_FILES_PATH: "changed_files.txt"
run: |
cd registry-automation
go run main.go ci
66 changes: 48 additions & 18 deletions registry-automation/cmd/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Copyright © 2024 Hasura
package cmd

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand All @@ -30,13 +29,19 @@ var ciCmd = &cobra.Command{
Run: runCI,
}

type ChangedFiles struct {
Added []string `json:"added_files"`
Modified []string `json:"modified_files"`
Deleted []string `json:"deleted_files"`
}

func init() {
rootCmd.AddCommand(ciCmd)

// Path for the changed files in the PR
var cfpE = os.Getenv(`CHANGED_FILES_PATH`)
ciCmd.PersistentFlags().String("changed-files-path", cfpE, "path to a line-separated list of changed files in the PR")
if cfpE == "" {
var changedFilesPathEnv = os.Getenv(`CHANGED_FILES_PATH`)
ciCmd.PersistentFlags().String("changed-files-path", changedFilesPathEnv, "path to a line-separated list of changed files in the PR")
if changedFilesPathEnv == "" {
ciCmd.MarkPersistentFlagRequired("changed-files-path")
}

Expand All @@ -55,28 +60,53 @@ func runCI(cmd *cobra.Command, args []string) {

// For each connector where a change is detected...

var changed_files = map[string]bool{}
// var changed_files = map[string]bool{}
// var changed_files_path = cmd.PersistentFlags().Lookup("changed-files-path").Value.String()
// file, err := os.Open(changed_files_path)
// if err != nil {
// log.Fatal(err)
// }
// defer file.Close()

// scanner := bufio.NewScanner(file)
// for scanner.Scan() {
// changed_files[filepath.Dir(scanner.Text())] = true // Just assume we'll treat each connector as if everything has changed.
// }

// if err := scanner.Err(); err != nil {
// panic(err)
// }

// var hub_directory = cmd.PersistentFlags().Lookup("connector-hub-directory").Value.String()

// for k := range changed_files {
// respondToChangedConnector(path.Join(hub_directory, k))
// }

var changed_files_path = cmd.PersistentFlags().Lookup("changed-files-path").Value.String()
file, err := os.Open(changed_files_path)
if err != nil {
log.Fatal(err)
changedFilesContent, err := os.Open(changed_files_path)

if err!= nil {
log.Fatalf("Failed to open the file: %v, err: %v", changed_files_path, err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
changed_files[filepath.Dir(scanner.Text())] = true // Just assume we'll treat each connector as if everything has changed.
defer changedFilesContent.Close()

// Read the file's contents
changedFilesByteValue, err := ioutil.ReadAll(changedFilesContent)
if err != nil {
log.Fatalf("Failed to read JSON file: %v", err)
}

if err := scanner.Err(); err != nil {
panic(err)
var changedFiles ChangedFiles
err = json.Unmarshal(changedFilesByteValue, &changedFiles)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}

var hub_directory = cmd.PersistentFlags().Lookup("connector-hub-directory").Value.String()
fmt.Printf("Parsed JSON: \n%+v\n", changedFiles)


for k := range changed_files {
respondToChangedConnector(path.Join(hub_directory, k))
}

}

Expand Down

0 comments on commit 42d9c9c

Please sign in to comment.