-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
457 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: release | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: cli/[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/gh-merge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
|
||
"github.com/cli/go-gh" | ||
) | ||
|
||
func GetNumber(args []string) (string, error) { | ||
if len(args) > 0 { | ||
return args[0], nil | ||
} | ||
|
||
data, _, err := gh.Exec("pr", "view", "--json", "number") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
type Result struct { | ||
Number int `json:"number"` | ||
} | ||
|
||
var r Result | ||
|
||
err = json.Unmarshal(data.Bytes(), &r) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return strconv.Itoa(r.Number), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/cli/go-gh" | ||
"github.com/leofeyer/gh-merge/util" | ||
) | ||
|
||
func MergePr(pr string, auto bool, admin bool) error { | ||
subject, err := getSubject(pr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := getBody(pr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("") | ||
fmt.Println(body) | ||
|
||
prompt := util.Confirm("Merge '"+subject+"' now?", false) | ||
if prompt == false { | ||
return errors.New("Cancelled.") | ||
} | ||
|
||
var args []string | ||
args = append(args, "pr", "merge", pr, "--subject", subject, "--body", body, "--squash") | ||
|
||
if auto { | ||
args = append(args, "--auto") | ||
} | ||
|
||
if admin { | ||
args = append(args, "--admin") | ||
} | ||
|
||
data, _, err := gh.Exec(args...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print(data.String()) | ||
return nil | ||
} | ||
|
||
func getSubject(pr string) (string, error) { | ||
data, _, err := gh.Exec("pr", "view", pr, "--json", "title") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
type Result struct { | ||
Title string `json:"title"` | ||
} | ||
|
||
var r Result | ||
|
||
err = json.Unmarshal(data.Bytes(), &r) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fmt.Sprintf("%s (see #%s)", r.Title, pr), nil | ||
} | ||
|
||
func getBody(pr string) (string, error) { | ||
data, _, err := gh.Exec("pr", "view", pr, "--json", "author,body,commits") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
type Result struct { | ||
Author struct { | ||
Login string `json:"login"` | ||
} | ||
Body string `json:"body"` | ||
Commits []struct { | ||
Oid string `json:"oid"` | ||
Headline string `json:"messageHeadline"` | ||
Authors []struct { | ||
Login string `json:"login"` | ||
Email string `json:"email"` | ||
} | ||
} | ||
} | ||
|
||
var r Result | ||
|
||
err = json.Unmarshal(data.Bytes(), &r) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
x := regexp.MustCompile("(?s)<!--.*?-->") | ||
|
||
ret := "Description\n-----------\n\n" | ||
ret += strings.TrimSpace(x.ReplaceAllString(r.Body, "")) | ||
ret += "\n\nCommits\n-------\n\n" | ||
|
||
authors := make(map[string]string) | ||
|
||
for i := 0; i < len(r.Commits); i++ { | ||
if r.Commits[i].Headline == "CS" { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(r.Commits[i].Headline, "Merge branch ") { | ||
continue | ||
} | ||
|
||
ret += fmt.Sprintf("%.8s", r.Commits[i].Oid) + " " + r.Commits[i].Headline + "\n" | ||
|
||
if r.Commits[i].Authors[0].Login == r.Author.Login { | ||
continue | ||
} | ||
|
||
authors[r.Commits[i].Authors[0].Login] = r.Commits[i].Authors[0].Email | ||
} | ||
|
||
if len(authors) > 0 { | ||
ret += "\n" | ||
|
||
for author, email := range authors { | ||
ret += "Co-authored-by: " + author + " <" + email + ">\n" | ||
} | ||
} | ||
|
||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/cli/go-gh" | ||
"github.com/leofeyer/gh-merge/util" | ||
) | ||
|
||
func ThankAuthor(pr string) error { | ||
user, err := getUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
author, err := getAuthor(pr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// We do not want to thank ourselves | ||
if user == author { | ||
return nil | ||
} | ||
|
||
prompt := util.Confirm("Say thank you?", true) | ||
if prompt == false { | ||
return nil | ||
} | ||
|
||
data, _, err := gh.Exec("pr", "comment", pr, "--body", "Thank you @"+author+".") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print(data.String()) | ||
return nil | ||
} | ||
|
||
func getUser() (string, error) { | ||
data, _, err := gh.Exec("config", "get", "user", "-h", "github.com") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return data.String(), nil | ||
} | ||
|
||
func getAuthor(pr string) (string, error) { | ||
data, _, err := gh.Exec("pr", "view", pr, "--json", "author") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
type Result struct { | ||
Author struct { | ||
Login string `json:"login"` | ||
} | ||
} | ||
|
||
var r Result | ||
|
||
err = json.Unmarshal(data.Bytes(), &r) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return r.Author.Login, nil | ||
} |
Oops, something went wrong.