-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (78 loc) · 2.61 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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/google/go-github/v65/github"
"golang.org/x/oauth2"
)
var client *github.Client
func main() {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present")
}
// getConfig
conf, err := parseConfigFile("config.toml")
if err != nil {
log.Fatalf("Missing or wrong config.toml - %v", err)
}
log.Printf("Source repo: %s\n", conf.SourceRepoReleases)
// github client
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
// getCurrentHugoVersion
sourceOwner, sourceRepo := getRepoPath(conf.SourceRepoReleases)
hugoVersion, releaseURL, releaseInfo, err := getCurrentHugoVersion(ctx, client, sourceOwner, sourceRepo)
if err != nil {
log.Fatal(err)
}
for _, repository := range conf.TargetRepository {
// getCurrentDeployedVersion for all config.targetRepos (done)
owner, repo := getRepoPath(repository.Repo)
deployVersion, deployContent, err := getCurrentDeployedVersion(ctx, client, owner, repo, repository.TargetFile, repository.Branch)
if err != nil {
log.Fatalf("Error in getCurrentDeployedVersion - %v", err)
}
messageHugoVersion := []byte(hugoVersion)
errHugo := os.WriteFile(".hugo-version", messageHugoVersion, 0644)
if errHugo != nil {
log.Fatal(err)
}
messageDeployVersion := []byte(deployVersion)
errDeployed := os.WriteFile(".deployed-version", messageDeployVersion, 0644)
if errDeployed != nil {
log.Fatal(err)
}
if isNewVersion(hugoVersion, deployVersion) {
updatedContent := updateVersion(hugoVersion, deployContent)
fmt.Println(updatedContent)
commitBranch := getCommitBranch(hugoVersion)
ref, newBranch, err := getRef(ctx, client, owner, repo, repository.Branch, commitBranch)
if err != nil {
log.Fatalf("Error in getRef %v", err)
}
if newBranch {
tree, err := getTree(ctx, client, owner, repo, ref, "netlify.toml", updatedContent)
if err != nil {
log.Fatalf("Error in getTree %v", err)
}
errCommit := pushCommit(ctx, client, owner, repo, ref, tree, hugoVersion)
if errCommit != nil {
log.Fatalf("Error in pushCommit %v", errCommit)
}
errPR := createPullRequest(ctx, client, owner, repo, repository.Branch, hugoVersion, releaseURL, releaseInfo, commitBranch)
if errPR != nil {
log.Fatalf("Error in createPullRequest %v", errPR)
}
} else {
log.Printf("PR branch (%s) already exists.\n", commitBranch)
}
} else {
log.Printf("No new version in %s/%s (current: %s)\n", owner, repo, deployVersion)
}
}
}