-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.go
101 lines (87 loc) · 2.69 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
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package main
import (
"context"
"errors"
"flag"
"log"
"os"
"os/exec"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// Case-insensitive list of PRs for which changelog enforcement needs to be skipped
var skipTags = []string{"[tests-only]", "[build-deps]", "[docs-only]"}
func skipPR(prID int) bool {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_API_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
pr, _, err := client.PullRequests.Get(ctx, "cs3org", "reva", prID)
if err != nil {
log.Panic(err)
}
prTitle := strings.ToLower(pr.GetTitle())
for _, tag := range skipTags {
if strings.HasPrefix(prTitle, tag) {
log.Print("Skipping changelog check for tag: " + tag)
return true
}
}
return false
}
func main() {
repo := flag.String("repo", "", "the remote repo against which diff-index is to be derived")
branch := flag.String("branch", "master", "the branch against which diff-index is to be derived")
prID := flag.Int("pr", 0, "the ID of the PR")
flag.Parse()
if *prID > 0 && skipPR(*prID) {
return
}
if *repo != "" {
s := *repo + "/" + *branch
branch = &s
}
cmd := exec.Command("git", "diff-index", *branch, "--", ".")
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
// Return successfully if there are no changes
if len(out) == 0 {
return
}
cmd = exec.Command("git", "diff-index", *branch, "--", "changelog/unreleased")
out, err = cmd.Output()
if err != nil {
log.Fatal(err)
}
mods := strings.Split(string(out), "\n")
for _, m := range mods {
params := strings.Split(m, " ")
// The fifth param in the output of diff-index is always the status followed by optional score number
if len(params) >= 5 && (params[4][0] == 'A' || params[4][0] == 'M') {
return
}
}
log.Fatal(errors.New("no changelog added. Please create a changelog item based on your changes"))
}