forked from FRC1360/ReleaseBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (65 loc) · 1.6 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
package main
import (
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"strings"
"fmt"
"os"
"net/http"
"io"
"github.com/bluele/slack"
)
const (
SLACK_CHANNEL = "programming"
)
func main() {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: GITHUB_TOKEN},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
githubClient := github.NewClient(tc)
release, err := githubClient.Repositories.GetLatestRelease("FRC1360","Stronghold2016")
if(err != nil) {
panic(err)
}
if downloadFromUrl(release.ZipballURL) {
message := "Code Release Downloaded: " + release.AssetsURL + "\nSaved to Backup Server - Running copy cron job now."
api := slack.New(SLACK_TOKEN)
channel, err := api.FindChannelByName(SLACK_CHANNEL)
if err != nil {
panic(err)
}
err = api.ChatPostMessage(channel.Id, message, nil)
if err != nil {
panic(err)
}
}
}
func downloadFromUrl(url string)(downloaded bool){
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
if os.IsExist(fileName) {
fmt.Println("File already downloaded, ignoring.")
return false
}
output, err := os.Create(fileName)
if err != nil {
fmt.Println("Error while creating", fileName, "-", err)
return false
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return false
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return false
}
fmt.Println(n, "bytes downloaded.")
return true
}