Skip to content

Commit

Permalink
JIRA-1234(SHOW): This PR was created using bc pr command (#22)
Browse files Browse the repository at this point in the history
* add(bc): add pr creation

* update

* add(bc): create pr from cli

* del(bc): remote commented out code for maintainability

* update
  • Loading branch information
pgaijin66 authored Apr 12, 2024
1 parent bec80da commit 3bbd045
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 5 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ Example usage:

Opens relevant git repository in browser

*DEMO:* [bc-open](assets/bc-open.mp4)
*DEMO:* ![bc-open](https://github.com/pgaijin66/bc/blob/main/assets/bc-open.mp4)

https://github.com/pgaijin66/bc/blob/main/assets/bc-open.mp4


##### `bc commit`
```
Expand Down Expand Up @@ -161,7 +164,9 @@ echo 'export GH_TOKEN="github_pat_1....REDACTED"' >> ~/.bash_rc

2. Navigate to the cloned directory.

3. Run `make install` to install
3. Run `task install-osx` to install

*Note: I am working on setting up installation via homebrew, however for now you can install it using task install-osx. You might have to install go-task for thos*


### Help
Expand Down
Binary file added bin/bc-darwin
Binary file not shown.
173 changes: 170 additions & 3 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package main

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
Expand Down Expand Up @@ -191,6 +196,170 @@ func openBrowser() {
}
}

func isBranchExistsInOrigin(branchName string) bool {
cmd := exec.Command("git", "ls-remote", "--heads", "origin", branchName)
output, err := cmd.Output()
if err != nil {
fmt.Println("Error:", err)
return false
}

outputStr := strings.TrimSpace(string(output))

lines := strings.Split(outputStr, "\n")
lineCount := len(lines)

// hack, this is to be done better
return lineCount > 0
}

func ReadSentence(question string) string {
fmt.Printf("%s", question)

scanner := bufio.NewScanner(os.Stdin)

// Scan for the next token (until newline)
if scanner.Scan() {
// Get the text entered by the user
return scanner.Text()
}

// If an error occurs during scanning, return an empty string
return ""
}

func createPr() {
currentBranchName, err := getCurrentBranchName()
if err != nil {
log.Fatal("coult not get current branch name")
}

if !isBranchExistsInOrigin(currentBranchName) {
fmt.Println("Branch:", currentBranchName, "has not been pushed to origin. Please push and try again.")
os.Exit(1)
}

repoPathCmd := exec.Command("git", "rev-parse", "--show-toplevel")
repoPathOut, err := repoPathCmd.Output()
if err != nil {
fmt.Println("Error getting repository path:", err)
os.Exit(1)
}
repositoryPath := strings.TrimSpace(string(bytes.TrimSpace(repoPathOut)))
repositoryName := filepath.Base(repositoryPath)
repoOwnerCmd := exec.Command("git", "remote", "-v")
remoteOutput, err := repoOwnerCmd.Output()
if err != nil {
fmt.Println("Error getting remote information:", err)
os.Exit(1)
}

remoteURL := string(remoteOutput)

lines := strings.Split(remoteURL, "\n")

// hack :D
lastLine := lines[len(lines)-2]

parts := strings.Split(lastLine, ":")
if len(parts) < 2 {
fmt.Println("Invalid remote URL format")
return
}

// Take the first part after splitting by "/"
repoOwner := strings.Split(parts[1], "/")[0]

prTitle := ReadSentence("Title of the Pull Request: ")
prTicket := ReadSentence("Is this PR associated with any ticket (eg: JIRA-124): ")
prType := ReadSentence("PR type (eg: SHOW, SHIP. ASK): ")
prChangeType := ReadSentence("What kind of change is this (eg: Bufix, Feature, Breaking Change, Doc update): ")
srcBranch := ReadSentence("Source branch name: ")
destBranch := ReadSentence("Destination branch name: ")

fmt.Println("Explain work done in this PR (When finished hit ctrl-d on a new line to proceed):")
scanner := bufio.NewScanner(os.Stdin)
var prMessage strings.Builder
for scanner.Scan() {
prMessage.WriteString(scanner.Text())
prMessage.WriteString("\n")
}

updatedTitle := fmt.Sprintf("%s(%s): %s", prTicket, prType, prTitle)

prBody := fmt.Sprintf(`# Change Description
%s
-------------------------------------------
# Type of PR
- [X] %s
-------------------------------------------
# Type of Change
- [X] %s
-------------------------------------------
## Checklist before requesting a review
- [X] I have performed a self-review of my code
- [X] I am ready to get this code reviewed
- [X] I have locally tested this code against linting and validating.`, prMessage.String(), prType, prChangeType)

payload := map[string]string{
"title": updatedTitle,
"body": prBody,
"head": srcBranch,
"base": destBranch,
}

payloadBytes, err := json.Marshal(payload)
if err != nil {
fmt.Println("Error marshaling payload:", err)
os.Exit(1)
}

ghToken := os.Getenv("GH_TOKEN")
if ghToken == "" {
fmt.Println("GitHub token not set")
os.Exit(1)
}

githubURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/pulls", repoOwner, repositoryName)
req, err := http.NewRequest("POST", githubURL, bytes.NewBuffer(payloadBytes))
if err != nil {
fmt.Println("Error creating HTTP request:", err)
os.Exit(1)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+ghToken)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making HTTP request:", err)
os.Exit(1)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
os.Exit(1)
}

if strings.Contains(string(body), "A pull request already exists for") {
fmt.Println("\n\nPR already exists for that branch. Please close the PR to create new one.")
}
}

func main() {
if !hasGit() {
fmt.Println("This is not a git repo. I am not needed here. Ta Ta !!!")
Expand Down Expand Up @@ -219,9 +388,7 @@ func main() {
case "help":
usage()
case "pr":
// Handle pull request creation
fmt.Println("Pull request creation functionality is not implemented yet.")
os.Exit(1)
createPr()
default:
fmt.Println("Could not understand the command. Try running \"bc help\".")
os.Exit(1)
Expand Down

0 comments on commit 3bbd045

Please sign in to comment.