-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assign): adds a new assign action (#191)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a16cb10
commit 090e4b5
Showing
7 changed files
with
271 additions
and
10 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
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,86 @@ | ||
/* | ||
Package assign implements an [actions.Runner] that assigns the subject of a | ||
notification. | ||
It only works when the notifications has an issue or pull request for subject. | ||
It takes as arguments the usernames to assign. | ||
Usage in the config: | ||
rules: | ||
- action: assign | ||
args: ["user0", "user1"] | ||
Usage in the REPL: | ||
:assign user0 user1 | ||
Refs: https://docs.github.com/en/rest/issues/assignees?apiVersion=2022-11-28#add-assignees-to-an-issue | ||
*/ | ||
package assign | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/nobe4/gh-not/internal/colors" | ||
"github.com/nobe4/gh-not/internal/gh" | ||
"github.com/nobe4/gh-not/internal/notifications" | ||
) | ||
|
||
type Runner struct { | ||
Client *gh.Client | ||
} | ||
|
||
type Body struct { | ||
Assignees []string `json:"assignees"` | ||
} | ||
|
||
func (a *Runner) Run(n *notifications.Notification, assignees []string, w io.Writer) error { | ||
slog.Debug("assigning notification", "notification", n, "assignees", assignees) | ||
|
||
if len(assignees) == 0 { | ||
return errors.New("no assignees provided") | ||
} | ||
|
||
url, ok := issueURL(n.Subject.URL) | ||
if !ok { | ||
slog.Warn("not an issue or pull", "notification", n) | ||
return nil | ||
} | ||
|
||
assigneesUrl := url + "/assignees" | ||
|
||
body, err := json.Marshal(Body{Assignees: assignees}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = a.Client.API.Request(http.MethodPost, assigneesUrl, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(w, colors.Red("ASSIGN ")+n.String()+" to "+strings.Join(assignees, ", ")) | ||
|
||
return nil | ||
} | ||
|
||
func issueURL(url string) (string, bool) { | ||
re := regexp.MustCompile(`^(https://api\.github\.com/repos/.+/.+/)(issues|pulls)(/\d+)$`) | ||
matches := re.FindStringSubmatch(url) | ||
|
||
if len(matches) == 0 { | ||
return "", false | ||
} | ||
|
||
return fmt.Sprintf("%sissues%s", matches[1], matches[3]), true | ||
} |
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,169 @@ | ||
package assign | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/nobe4/gh-not/internal/api/mock" | ||
"github.com/nobe4/gh-not/internal/gh" | ||
"github.com/nobe4/gh-not/internal/notifications" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
w := &bytes.Buffer{} | ||
|
||
api := &mock.Mock{} | ||
client := gh.Client{API: api} | ||
|
||
runner := Runner{Client: &client} | ||
|
||
t.Run("not assignees", func(t *testing.T) { | ||
n := ¬ifications.Notification{URL: "http://example.com"} | ||
|
||
if err := runner.Run(n, []string{}, w); err == nil { | ||
t.Fatal("expected error", err) | ||
} | ||
|
||
if err := api.Done(); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
}) | ||
|
||
t.Run("not an issue or pull", func(t *testing.T) { | ||
n := ¬ifications.Notification{URL: "http://example.com"} | ||
|
||
if err := runner.Run(n, []string{"user"}, w); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
|
||
if err := api.Done(); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
}) | ||
|
||
t.Run("return an API failure", func(t *testing.T) { | ||
expectedError := errors.New("expected error") | ||
|
||
api.Calls = append(api.Calls, mock.Call{ | ||
Verb: "POST", | ||
Url: "https://api.github.com/repos/owner/repo/issues/123/assignees", | ||
Data: `{"assignees":["user"]}`, | ||
Error: expectedError, | ||
}) | ||
n := ¬ifications.Notification{ | ||
Subject: notifications.Subject{ | ||
URL: "https://api.github.com/repos/owner/repo/pulls/123", | ||
}, | ||
} | ||
|
||
if err := runner.Run(n, []string{"user"}, w); err != expectedError { | ||
t.Fatalf("expected %#v but got %#v", expectedError, err) | ||
} | ||
|
||
if err := api.Done(); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
}) | ||
|
||
t.Run("works for an issue", func(t *testing.T) { | ||
api.Calls = append(api.Calls, mock.Call{ | ||
Verb: "POST", | ||
Url: "https://api.github.com/repos/owner/repo/issues/123/assignees", | ||
Data: `{"assignees":["user"]}`, | ||
Response: &http.Response{StatusCode: http.StatusCreated}, | ||
}) | ||
n := ¬ifications.Notification{ | ||
Subject: notifications.Subject{ | ||
URL: "https://api.github.com/repos/owner/repo/issues/123", | ||
}, | ||
} | ||
|
||
if err := runner.Run(n, []string{"user"}, w); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
|
||
if err := api.Done(); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
}) | ||
|
||
t.Run("works for a pull", func(t *testing.T) { | ||
api.Calls = append(api.Calls, mock.Call{ | ||
Verb: "POST", | ||
Url: "https://api.github.com/repos/owner/repo/issues/123/assignees", | ||
Data: `{"assignees":["user"]}`, | ||
Response: &http.Response{StatusCode: http.StatusCreated}, | ||
}) | ||
n := ¬ifications.Notification{ | ||
Subject: notifications.Subject{ | ||
URL: "https://api.github.com/repos/owner/repo/pulls/123", | ||
}, | ||
} | ||
|
||
if err := runner.Run(n, []string{"user"}, w); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
|
||
if err := api.Done(); err != nil { | ||
t.Fatal("unexpected error", err) | ||
} | ||
}) | ||
} | ||
|
||
func TestIsIssueOrPull(t *testing.T) { | ||
tests := []struct { | ||
url string | ||
want string | ||
match bool | ||
}{ | ||
{ | ||
url: "http://example.com", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://github.com", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://api.github.com", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://api.github.com/repos/owner/repo", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://api.github.com/repos/owner/repo/pulls", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://api.github.com/repos/owner/repo/issues", | ||
match: false, | ||
}, | ||
{ | ||
url: "https://api.github.com/repos/owner/repo/pulls/123", | ||
want: "https://api.github.com/repos/owner/repo/issues/123", | ||
match: true, | ||
}, | ||
{ | ||
url: "https://api.github.com/repos/owner/repo/issues/123", | ||
want: "https://api.github.com/repos/owner/repo/issues/123", | ||
match: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.url, func(t *testing.T) { | ||
got, match := issueURL(test.url) | ||
if match != test.match { | ||
t.Errorf("want %v but got %v", test.match, match) | ||
} | ||
|
||
if got != test.want { | ||
t.Errorf("want %v but got %v", test.want, got) | ||
} | ||
}) | ||
} | ||
} |
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
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