-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (96 loc) · 3.96 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
package main
import (
"context"
"fmt"
"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/google/go-github/v62/github"
"os"
"os/exec"
"strings"
)
var (
token = os.Getenv("PLUGIN_GITHUB_AUTH_TOKEN")
repositoryName = os.Getenv("PLUGIN_REPOSITORY_NAME")
repositoryOwner = os.Getenv("PLUGIN_REPOSITORY_OWNER")
commands = os.Getenv("PLUGIN_COMMANDS")
outputFile = os.Getenv("DRONE_OUTPUT")
copyOutputFile = "outputVariables" // TODO: make the path configurable
)
func main() {
verifyPluginParameters([]string{"PLUGIN_GITHUB_AUTH_TOKEN", "PLUGIN_REPOSITORY_NAME", "PLUGIN_REPOSITORY_OWNER", "PLUGIN_COMMANDS"})
ctx := context.Background()
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
failOnErr(err)
client := github.NewClient(rateLimiter).WithAuthToken(token)
results, err := os.Create(outputFile)
failOnErr(err)
if strings.Contains(commands, "getPrDetails") {
verifyPluginParameters([]string{"PLUGIN_PR_NUMBER"})
fields := getPullRequest(client, &ctx, repositoryName, repositoryOwner, os.Getenv("PLUGIN_PR_NUMBER"))
writeResult(*results, fields)
}
if strings.Contains(commands, "getChangedFiles") {
verifyPluginParameters([]string{"PLUGIN_PR_NUMBER"})
prFields := getPullRequest(client, &ctx, repositoryName, repositoryOwner, os.Getenv("PLUGIN_PR_NUMBER"))
//writeResult(*results, prFields)
fields := getChanges(client, &ctx, repositoryName, repositoryOwner, prFields["BASE_SHA"], prFields["HEAD_SHA"])
writeResult(*results, fields)
}
if strings.Contains(commands, "setTag") {
verifyPluginParameters([]string{"PLUGIN_TAG_NAME", "PLUGIN_SHA"})
tagName := "refs/tags/" + os.Getenv("PLUGIN_TAG_NAME")
updateCreateTag(client, &ctx, repositoryName, repositoryOwner, tagName, os.Getenv("PLUGIN_SHA"))
}
if strings.Contains(commands, "createPullRequest") {
verifyPluginParameters([]string{"PLUGIN_PR_SOURCE_BRANCH", "PLUGIN_PR_TARGET_BRANCH", "PLUGIN_PR_TITLE", "PLUGIN_PR_BODY"})
fields := createPullRequest(client, &ctx, repositoryName, repositoryOwner,
os.Getenv("PLUGIN_PR_SOURCE_BRANCH"),
os.Getenv("PLUGIN_PR_TARGET_BRANCH"),
os.Getenv("PLUGIN_PR_TITLE"),
os.Getenv("PLUGIN_PR_BODY"),
os.Getenv("PLUGIN_PR_LABELS"))
writeResult(*results, fields)
}
if strings.Contains(commands, "AddPullRequestLabels") {
verifyPluginParameters([]string{"PLUGIN_PR_NUMBER", "PLUGIN_PR_LABELS"})
addPullRequestLabels(client, &ctx, repositoryName, repositoryOwner,
os.Getenv("PLUGIN_PR_NUMBER"),
os.Getenv("PLUGIN_PR_LABELS"))
}
if strings.Contains(commands, "setStatusCheck") {
verifyPluginParameters([]string{"PLUGIN_STATUS_CHECK_SHA",
"PLUGIN_STATUS_CHECK_CONTEXT",
"PLUGIN_STATUS_CHECK_STATUS",
"PLUGIN_STATUS_CHECK_URL",
"PLUGIN_STATUS_CHECK_DESCRIPTION"})
setStatusCheck(client, &ctx, repositoryName, repositoryOwner,
os.Getenv("PLUGIN_STATUS_CHECK_SHA"),
os.Getenv("PLUGIN_STATUS_CHECK_CONTEXT"),
os.Getenv("PLUGIN_STATUS_CHECK_STATUS"),
os.Getenv("PLUGIN_STATUS_CHECK_URL"),
os.Getenv("PLUGIN_STATUS_CHECK_DESCRIPTION"))
}
if strings.Contains(commands, "getStatuses") {
verifyPluginParameters([]string{"PLUGIN_REF"})
fields := listStatusChecks(client, &ctx, repositoryName, repositoryOwner, os.Getenv("PLUGIN_REF"))
writeResult(*results, fields)
}
if strings.Contains(commands, "waitForStatus") {
verifyPluginParameters([]string{"PLUGIN_REF", "PLUGIN_STATUS_CHECK_CONTEXT"})
fields := waitForStatus(client, &ctx, repositoryName, repositoryOwner,
os.Getenv("PLUGIN_REF"),
os.Getenv("PLUGIN_STATUS_CHECK_CONTEXT"),
os.Getenv("PLUGIN_STATUS_CHECK_WAIT_TIMEOUT"))
writeResult(*results, fields)
}
if strings.Contains(commands, "mergePr") {
verifyPluginParameters([]string{"PLUGIN_PR_NUMBER"})
mergePullRequest(client, &ctx, repositoryName, repositoryOwner,
os.Getenv("PLUGIN_PR_NUMBER"),
os.Getenv("PLUGIN_MERGE_COMMENT"))
}
results.Close()
out, err := exec.Command("cp", "-v", outputFile, copyOutputFile).Output()
failOnErr(err)
fmt.Print(string(out[:]))
}