Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ataberkcanitez committed Dec 6, 2023
1 parent 2ac946b commit fd2197f
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 14 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 - present, Ataberk Canitez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# pr-links
### GitHub CLI Pull Request Viewer

pr-links is a command-line tool written in Go that enhances the functionality of the GitHub CLI by displaying Pull Request information along with their URLs. The GitHub CLI itself lacks the ability to show the Pull Request URL, and this tool bridges that gap.

### Installation


### Example Output

The tool fetches open Pull Requests that are currently assigned to you and presents them in a formatted table. The table includes columns for the repository name, Pull Request ID, truncated title, and a direct link to the Pull Request on GitHub.
![example-output](assets/example-output.png)

### License
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.


### Acknowledgments

- GitHub CLI for providing the foundation for interacting with GitHub from the command line.
- alexeyco/simpletable for simplifying the creation of formatted tables in the console.

### Contributing

Feel free to contribute by opening issues or creating pull requests. Your feedback and involvement are highly encouraged!

---
Enjoy using pr-links and feel free to reach out with any feedback or suggestions!
Binary file added assets/example-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"bufio"
"fmt"
"os/exec"
"strings"
)

type GitHubService struct{}

type PullRequest struct {
Repo string
Id string
Status string
Title string
}

func (gs *GitHubService) GetOpenPullRequests() ([]PullRequest, error) {
cmd := exec.Command("gh", "search", "prs", "--review-requested", "@me", "--state", "open")
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("error running git status: %v", err)
}
return parsePullRequests(string(output)), nil
}

func parsePullRequests(prLine string) []PullRequest {
var prs []PullRequest
scanner := bufio.NewScanner(strings.NewReader(prLine))
for scanner.Scan() {
line := strings.Split(scanner.Text(), "\t")
pr := PullRequest{
Repo: line[0],
Id: line[1],
Status: line[2],
Title: line[3],
}
prs = append(prs, pr)
}
return prs
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require github.com/cli/go-gh/v2 v2.4.0

require (
github.com/alexeyco/simpletable v1.0.0 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alexeyco/simpletable v1.0.0 h1:ZQ+LvJ4bmoeHb+dclF64d0LX+7QAi7awsfCrptZrpHk=
github.com/alexeyco/simpletable v1.0.0/go.mod h1:VJWVTtGUnW7EKbMRH8cE13SigKGx/1fO2SeeOiGeBkk=
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/cli/go-gh/v2 v2.4.0 h1:6j3YxA8uJVOL4lBWjqDmMiAQNnJ2fiZagCuEmQXl+pU=
Expand All @@ -19,11 +21,13 @@ github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
25 changes: 11 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ package main

import (
"fmt"

"github.com/cli/go-gh/v2/pkg/api"
"os"
)

func main() {
fmt.Println("hi world, this is the gh-pr-links extension!")
client, err := api.DefaultRESTClient()
githubService := &GitHubService{}
cliOutputHandler := &CliOutputHandler{}

pullRequests, err := githubService.GetOpenPullRequests()
if err != nil {
fmt.Println(err)
return
fmt.Println("Error:", err)
os.Exit(1)
}
response := struct {Login string}{}
err = client.Get("user", &response)
if err != nil {
fmt.Println(err)

if len(pullRequests) == 0 {
fmt.Println("No PR to show.")
return
}
fmt.Printf("running as %s\n", response.Login)
cliOutputHandler.PrintPullRequests(pullRequests)
}

// For more examples of using go-gh, see:
// https://github.com/cli/go-gh/blob/trunk/example_gh_test.go
62 changes: 62 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"github.com/alexeyco/simpletable"
"strings"
)

type CliOutputHandler struct{}

func (co *CliOutputHandler) PrintPullRequests(prs []PullRequest) {
table := simpletable.New()
table.Header = prepareHeader()
for _, row := range prs {
link := prepareLink(row.Repo, row.Id)
title := prepareTitle(row.Title)
r := []*simpletable.Cell{
{Text: row.Repo},
{Text: row.Id},
{Text: title},
{Text: link},
}
table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleUnicode)
fmt.Println(table.String())
}

func prepareHeader() *simpletable.Header {
return &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "REPO"},
{Align: simpletable.AlignCenter, Text: "ID"},
{Align: simpletable.AlignCenter, Text: "TITLE"},
{Align: simpletable.AlignCenter, Text: "LINK"},
},
}
}

func prepareLink(repo, id string) string {
return fmt.Sprintf("https://www.github.com/%s/pull/%s", repo, id)
}

func prepareTitle(input string) string {
const maxCharacters = 40
var result strings.Builder
var count int
words := strings.Fields(input)

for _, word := range words {
wordLen := len(word)
if count+wordLen > maxCharacters {
result.WriteString("\n")
count = 0
}
result.WriteString(word)
result.WriteString(" ")
count += wordLen + 1
}

return result.String()
}

0 comments on commit fd2197f

Please sign in to comment.