Skip to content

Commit

Permalink
QUick look
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
  • Loading branch information
ArangoGutierrez committed Feb 29, 2024
1 parent 2abb3b9 commit 1d98178
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions cmd/nv-ci-bot/retitle/retitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package retitle

import (
"context"
"os"
"regexp"
"strings"

"golang.org/x/oauth2"
"k8s.io/klog/v2"

"github.com/google/go-github/v59/github"
Expand Down Expand Up @@ -64,16 +67,49 @@ func (m command) build() *cli.Command {
func (m command) run(c *cli.Context) error {
action := githubactions.New()

// Create the GitHub client
accessToken := os.Getenv("INPUT_GITHUB-TOKEN")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(c.Context, ts)
gh := github.NewClient(tc)

// Get the event
context, err := action.Context()
if err != nil {
m.log.Error(err, "Failed to get action context")
return err
}
org, repo := context.Repo()

// Check if user is trusted first
var user string
if context.Event != nil {
if comment, ok := context.Event["comment"].(map[string]any); ok {
if userMap, ok := comment["user"].(map[string]any); ok {
if login, ok := userMap["login"].(string); ok {
user = login
}
}
}
}
trusted, err := isMemberOfOrg(c.Context, gh, org, user)
if err != nil {
m.log.Error(err, "Failed to check if user is trusted")
return err
}
if !trusted {
m.log.Info("User is not trusted")
return nil
}

// Extract the new title and issue number
var newTitle string
var issueNumber int
var issueNumber float64
if context.Event != nil {
if comment, ok := context.Event["comment"].(map[string]any); ok {
// Extract the comment body
if body, ok := comment["body"].(string); ok {
// Make sure they are requesting a re-title
if !retitleRe.MatchString(body) {
Expand All @@ -83,24 +119,21 @@ func (m command) run(c *cli.Context) error {
newTitle = getNewTitle(body)
}
}
// get the issue number
if issue, ok := context.Event["issue"].(map[string]any); ok {
if num, ok := issue["number"].(int); ok {
if num, ok := issue["number"].(float64); ok {
issueNumber = num
}
}
}
m.log.Info("New title", "title", newTitle)
m.log.Info("Issue number", "number", issueNumber)

org, repo := context.Repo()
ghToken := action.Getenv("GITHUB_TOKEN")
gh := github.NewClient(nil).WithAuthToken(ghToken)

// Update the title
req := &github.IssueRequest{
Title: &newTitle,
}
_, _, err = gh.Issues.Edit(c.Context, org, repo, issueNumber, req)
_, _, err = gh.Issues.Edit(c.Context, org, repo, int(issueNumber), req)
if err != nil {
m.log.Error(err, "Failed to update issue")
return err
Expand All @@ -116,3 +149,12 @@ func getNewTitle(body string) string {
}
return strings.TrimSpace(matches[1])
}

// Check if the user is a member of the Org
func isMemberOfOrg(ctx context.Context, gh *github.Client, org, user string) (bool, error) {
membership, _, err := gh.Organizations.GetOrgMembership(ctx, org, user)
if err != nil {
return false, err
}
return membership.State != nil && *membership.State == "active", nil
}

0 comments on commit 1d98178

Please sign in to comment.