Skip to content

Commit 5a385f8

Browse files
feat: Add support for other events
**Added events:** - Forks: Notify when a repository is forked - Issues: Notify when an issue is opened or closed - Pull Requests: Notify when a pull request is opened or closed - Comments: Notify when an issue / pull request is commented on - Releases: Notify when a release is published - Stars: Notify when the repository is starred
1 parent 3bef663 commit 5a385f8

File tree

18 files changed

+565
-60
lines changed

18 files changed

+565
-60
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616
.env
17+
events/

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "main.go"
13+
}
14+
]
15+
}

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,44 @@ Add the following lines of code in your YML file.
2929
chat_id: '${{ secrets.CHAT_ID }}'
3030
```
3131

32+
## Supported events
33+
34+
- [x] Commits
35+
- [x] Forks
36+
- [x] Watch
37+
- [x] stars
38+
- [x] Issues
39+
- [x] created
40+
- [x] closed
41+
- [x] opened
42+
- [x] reopened
43+
- [x] locked
44+
- [x] unlocked
45+
- [x] Issue comments
46+
- [x] created
47+
- [x] deleted
48+
- [x] Pull Request
49+
- [x] created
50+
- [x] closed
51+
- [x] opened
52+
- [x] reopened
53+
- [x] locked
54+
- [x] unlocked
55+
- [x] synchronize
56+
- [x] Pull Request comments
57+
- [x] created
58+
- [x] deleted
59+
- [x] Releases
60+
- [x] published
61+
- [x] released
62+
- [ ] Discussions
63+
- [ ] Discussion comments
64+
3265
## Contributing
33-
Feel free to open a PR in case of any minor fix and please open an issue first for major changes.
66+
67+
If you can't find the event you are looking for, assume that it's not tested yet. You are free to open a pull request.
68+
69+
Also, feel free to open a PR in case of any minor fix and please open an issue first for major changes.
3470

3571
## License
3672

main.go

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,28 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
"net/http"
7-
"net/url"
5+
"github-telegram-notify/types"
6+
"github-telegram-notify/utils"
87
"os"
98
)
109

11-
type Metadata struct {
12-
Sha string `json:"sha"`
13-
RepositoryName string `json:"repository"`
14-
Event Event `json:"event"`
15-
Ref_name string `json:"ref_name"`
16-
ServerUrl string `json:"server_url"`
17-
}
18-
19-
func (m *Metadata) repoUrl() string {
20-
return fmt.Sprint(m.ServerUrl, "/", m.RepositoryName)
21-
}
22-
23-
type Event struct {
24-
Commits []Commit `json:"commits"`
25-
Compare string `json:"compare"`
26-
}
27-
28-
type Commit struct {
29-
Id string `json:"id"`
30-
Message string `json:"message"`
31-
Url string `json:"url"`
32-
Ref string `json:"ref"`
33-
Author Author `json:"author"`
34-
}
35-
36-
type Author struct {
37-
Name string `json:"name"`
38-
Email string `json:"email"`
39-
Username string `json:"username"`
40-
}
41-
42-
func (a *Author) Url() string {
43-
return fmt.Sprint("https://github.com/", a.Username)
44-
}
45-
4610
func main() {
47-
apiBaseUri, _ := url.Parse("https://api.telegram.org")
4811
tg_token := os.Getenv("INPUT_BOT_TOKEN")
49-
chatId := os.Getenv("INPUT_CHAT_ID")
12+
chatID := os.Getenv("INPUT_CHAT_ID")
5013
gitEventRaw := os.Getenv("INPUT_GIT_EVENT")
5114
print(gitEventRaw)
52-
var gitEvent Metadata
15+
var gitEvent *types.Metadata
5316
err := json.Unmarshal([]byte(gitEventRaw), &gitEvent)
5417
if err != nil {
5518
panic(err)
5619
}
57-
req_url, _ := url.Parse(fmt.Sprint(apiBaseUri, "/bot", tg_token, "/sendMessage"))
58-
params := url.Values{}
59-
text := fmt.Sprintf("<b>🔨 %d New commit to</b> <a href=\"%s\">%s</a>[<code>%s</code>]\n\n", len(gitEvent.Event.Commits), gitEvent.repoUrl(), gitEvent.RepositoryName, gitEvent.Ref_name)
60-
for _, commit := range gitEvent.Event.Commits {
61-
text += fmt.Sprintf("• <a href=\"%s\">%s</a> - %s by <a href=\"%s\">%s</a>\n", commit.Url, commit.Id[:7], commit.Message, commit.Author.Url(), commit.Author.Name)
62-
}
63-
params.Set("chat_id", chatId)
64-
params.Set("text", text)
65-
params.Set("parse_mode", "html")
66-
params.Set("disable_web_page_preview", "true")
67-
kyb, err := json.Marshal(map[string][][]map[string]string{
68-
"inline_keyboard": {
69-
{{"text": "Open changes", "url": gitEvent.Event.Compare}},
70-
},
71-
})
20+
text, markupText, markupUrl, err := utils.CreateContents(gitEvent)
7221
if err != nil {
7322
panic(err)
7423
}
75-
params.Set("reply_markup", string(kyb))
76-
req_url.RawQuery = params.Encode()
77-
http.Get(req_url.String())
24+
error := utils.SendMessage(tg_token, chatID, text, markupText, markupUrl)
25+
if error.Description != "" {
26+
panic(error.String())
27+
}
28+
7829
}

types/base.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
type Metadata struct {
8+
Sha string `json:"sha"`
9+
RepositoryName string `json:"repository"`
10+
RawEvent *json.RawMessage `json:"event"`
11+
Ref_name string `json:"ref_name"`
12+
ServerUrl string `json:"server_url"`
13+
EventName string `json:"event_name"`
14+
}
15+
16+
func (e *Metadata) ParseEvent() (event_type interface{}, err error) {
17+
switch e.EventName {
18+
case "fork":
19+
event_type = &ForkEvent{}
20+
case "issue_comment":
21+
event_type = &IssueCommentEvent{}
22+
case "issues":
23+
event_type = &IssuesEvent{}
24+
case "pull_request":
25+
event_type = &PullRequestEvent{}
26+
case "pull_request_review_comment":
27+
event_type = &PullRequestReviewCommentEvent{}
28+
case "push":
29+
event_type = &PushEvent{}
30+
case "release":
31+
event_type = &ReleaseEvent{}
32+
case "watch":
33+
event_type = &WatchEvent{}
34+
}
35+
err = json.Unmarshal(*e.RawEvent, &event_type)
36+
return event_type, err
37+
}

types/commit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package types
2+
3+
type PushEvent struct {
4+
Ref string `json:"ref,omitempty"`
5+
Commits []Commit `json:"commits,omitempty"`
6+
Action string `json:"action,omitempty"`
7+
Repo Repository `json:"repository,omitempty"`
8+
}
9+
10+
type Commit struct {
11+
Id string `json:"id"`
12+
Message string `json:"message"`
13+
Url string `json:"url"`
14+
Ref string `json:"ref"`
15+
Author User `json:"author"`
16+
}

types/errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package types
2+
3+
import "fmt"
4+
5+
type Error struct {
6+
Module string
7+
Description string
8+
Message string
9+
}
10+
11+
func (e Error) String() string {
12+
return fmt.Sprintf("%s: %s\n%s", e.Module, e.Description, e.Message)
13+
}
14+
15+
type TelegramError struct {
16+
OK bool `json:"ok"`
17+
ErrorCode int `json:"error_code"`
18+
Description string `json:"description"`
19+
}

types/fork.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package types
2+
3+
type ForkEvent struct {
4+
Forkee Repository `json:"forkee,omitempty"`
5+
Repo Repository `json:"repository,omitempty"`
6+
Sender User `json:"sender,omitempty"`
7+
}

types/issues.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package types
2+
3+
type Issue struct {
4+
Number int `json:"number,omitempty"`
5+
State string `json:"state,omitempty"`
6+
Locked bool `json:"locked,omitempty"`
7+
Title string `json:"title,omitempty"`
8+
Body string `json:"body,omitempty"`
9+
AuthorAssociation string `json:"author_association,omitempty"`
10+
User User `json:"user,omitempty"`
11+
HTMLURL string `json:"html_url,omitempty"`
12+
Repository Repository `json:"repository,omitempty"`
13+
}
14+
15+
type IssueCommentEvent struct {
16+
// Possible values are: "created", "edited", "deleted".
17+
Action string `json:"action,omitempty"`
18+
Issue *Issue `json:"issue,omitempty"`
19+
Comment *IssueComment `json:"comment,omitempty"`
20+
Repo Repository `json:"repository,omitempty"`
21+
Sender User `json:"sender,omitempty"`
22+
}
23+
24+
type IssueComment struct {
25+
User User `json:"user,omitempty"`
26+
HTMLURL string `json:"html_url,omitempty"`
27+
IssueURL string `json:"issue_url,omitempty"`
28+
}
29+
30+
type IssuesEvent struct {
31+
Action string `json:"action,omitempty"`
32+
Issue *Issue `json:"issue,omitempty"`
33+
Repo Repository `json:"repository,omitempty"`
34+
Sender User `json:"sender,omitempty"`
35+
}

types/organization.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package types
2+
3+
type Organization struct {
4+
AvatarURL string `json:"avatar_url,omitempty"`
5+
HTMLURL string `json:"html_url,omitempty"`
6+
Name string `json:"name,omitempty"`
7+
Email string `json:"email,omitempty"`
8+
}

0 commit comments

Comments
 (0)