-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
144 lines (120 loc) · 3.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"strings"
medium "github.com/readme-update-actions/pkg/structs"
helpers "github.com/readme-update-actions/pkg/utils"
)
func main() {
// get the rss list from the actions env
// rss_medium, _ := helpers.GetEnvString("INPUT_RSS_LIST")
rss_medium := "https://imskr.medium.com/feed"
// get the number of posts or stories to commit
max_post, _ := helpers.GetEnvInteger("INPUT_MAX_POST")
// if max_post not in env var set default to 3
if max_post == 0 {
max_post = 3
}
// get readme path from the actions env
readme_path, _ := helpers.GetEnvString("INPUT_README_PATH")
// if path not provided default to root readme
if readme_path == "" {
readme_path = "./README.md"
}
// get username
commit_user, _ := helpers.GetEnvString("INPUT_COMMIT_USER")
if commit_user == "" {
commit_user = "readme-update-bot"
}
// git user email
commit_email, _ := helpers.GetEnvString("INPUT_COMMIT_EMAIL")
if commit_email == "" {
commit_email = "[email protected]"
}
// git commit message
commit_message, _ := helpers.GetEnvString("INPUT_COMMIT_MESSAGE")
if commit_message == "" {
commit_message = "Update readme with latest blogs"
}
// get medium.com rss feed
mediumResponse, err := http.Get(rss_medium)
if err != nil {
log.Println("Error making request to medium", err)
}
defer mediumResponse.Body.Close()
responseBody, err := ioutil.ReadAll(mediumResponse.Body)
if err != nil {
log.Println("Error reading response body", err)
}
// use RSS structs
var rss medium.RSS
errXMLParse := xml.Unmarshal(responseBody, &rss)
if errXMLParse != nil {
log.Println("Error xml parse", errXMLParse)
}
// store the posts
var items []string
// get the posts
// format it according to readme links format
for i := 0; i < max_post; i++ {
item := fmt.Sprintf("- [%s](%s)\n", rss.Channel.Item[i].Title, rss.Channel.Item[i].Link)
items = append(items, item)
}
// find readme and replace with our result
err = helpers.ReplaceFile(readme_path, items)
if err != nil {
log.Fatalf("Error updating readme %s", err)
}
// mark safe directory
safeCmd := exec.Command("git", "config", "--global", "--add", "safe.directory", "/github/workspace")
err = safeCmd.Run()
if err != nil {
log.Fatalf("Error setting safe directory %s", err)
}
// set git user name
nameCmd := exec.Command("git", "config", "user.name", commit_user)
err = nameCmd.Run()
if err != nil {
log.Fatalf("Error setting git user %s", err)
}
// set git user email
emailCmd := exec.Command("git", "config", "user.email", commit_email)
err = emailCmd.Run()
if err != nil {
log.Fatalf("Error setting git email %s", err)
}
// check git status
statusCmd, err := exec.Command("git", "status").Output()
if err != nil {
log.Fatal(err)
}
statusOutput := string(statusCmd)
if !strings.Contains(statusOutput, "nothing to commit") {
// add to staging area
addCmd := exec.Command("git", "add", readme_path)
err = addCmd.Run()
if err != nil {
log.Fatalf("Error adding to staging area %s", err)
return
}
// do git commit
commitCmd := exec.Command("git", "commit", "-m", commit_message)
err = commitCmd.Run()
if err != nil {
log.Fatalf("Error commiting to repo %s", err)
return
}
// do git push
pushCmd := exec.Command("git", "push")
err = pushCmd.Run()
if err != nil {
log.Fatalf("Error pushing to repo %s", err)
return
}
}
}