Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implemented CI Handler for adding CI files to a repo #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Vedant0898
Copy link
Member

Added a CIHandler endpoint for adding pre-commit CI checks depending on the call from GitHub Action.
Created pre-commit files for Python, Node and Golang.
I have made a script for adding the final pre-commit file into the specified repository.

Copy link
Member

@Harsh14901 Harsh14901 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe move all the functions related to CI to another file as src/git/ci.go

Comment on lines +37 to +39
BASE_FILES_PATH string = "../../precommit/"
// pre-commit config file
PRE_COMMIT_CONFIG string = "../../precommit/CI/pre-commit-config.yaml"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the style consistent, maybe change it to camel case as is with the other variables?

Comment on lines +24 to +25
// Logdir is the directory where all logs will be stored
logDir string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a separate logDir, can't we use the existing logger?

Comment on lines +55 to +62
Python string `json:"python"`
Golang string `json:"golang"`
Node string `json:"node"`
Ts string `json:"ts"`
Flutter string `json:"flutter"`
Dart string `json:"dart"`
Docker string `json:"docker"`
Shell string `json:"shell"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need all their types to be string. Can't we unmarshall to bool instead from the json request?

ciList = append(ciList, "Default")
v := reflect.ValueOf(ci)
for i := 0; i < v.NumField(); i++ {
if v.Field(i).String() == "true" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If CIAction can have bool types this should be changed accordingly.

ciList = append(ciList, v.Type().Field(i).Name)
}
}
addChecks(ciList)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this function call never return an error?

f, err := os.OpenFile(PRE_COMMIT_CONFIG, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Error("Error opening file:pre-commit.config")
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return the err from here

_, err = fmt.Fprintf(f, string(content)+"\n")
if err != nil {
fmt.Printf("Error writing to file:%s\n", PRE_COMMIT_CONFIG)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return err here as well

// combine the data of all files in ciList to a file named .pre-commit-config.yaml

// create the file even if it exists
os.Create(PRE_COMMIT_CONFIG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very comfortable with the idea of using the same file PRE_COMMIT_CONFIG for every call to addChecks. Imagine if there were two consecutive requests for adding CI checks (maybe the user was impatient, and didn't like to wait). Then this file can be overwritten while ci.sh is being executed and this can lead to hard-to-spot bugs. I would suggest some alternatives, you are free to implement any one of these.

  1. Use locks. Basically you need to ensure that the sequence of operations addChecks -> go addCI() is done atomically. So you would have to acquire a lock in the HTTP handler itself, which is not a very efficient solution imo.
  2. Have different files. Just don't forget to clean them up.
    • You can change the file name PRE_COMMIT_CONFIG to have a repo signature. Something like -> PRE_COMMIT_CONFIG + "_" + repoName. The script ci.sh would have to copy only this file instead of the entire directory.
    • You can create a temp directory (which will have an arbitrary name) where you can write PRE_COMMIT_CONFIG, and pass the temp directory name to ci.sh so that it would copy the entire directory as it is.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants