github.com/cs3org/reva/v2@v2.27.7/tools/check-changelog/main.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package main 20 21 import ( 22 "context" 23 "errors" 24 "flag" 25 "log" 26 "os" 27 "os/exec" 28 "strings" 29 30 "github.com/google/go-github/github" 31 "golang.org/x/oauth2" 32 ) 33 34 // Case-insensitive list of PRs for which changelog enforcement needs to be skipped 35 var skipTags = []string{"[tests-only]", "[build-deps]", "[docs-only]"} 36 37 func skipPR(prID int) bool { 38 ctx := context.Background() 39 ts := oauth2.StaticTokenSource( 40 &oauth2.Token{AccessToken: os.Getenv("GITHUB_API_TOKEN")}, 41 ) 42 tc := oauth2.NewClient(ctx, ts) 43 client := github.NewClient(tc) 44 45 pr, _, err := client.PullRequests.Get(ctx, "cs3org", "reva", prID) 46 if err != nil { 47 log.Panic(err) 48 } 49 prTitle := strings.ToLower(pr.GetTitle()) 50 51 for _, tag := range skipTags { 52 if strings.HasPrefix(prTitle, tag) { 53 log.Print("Skipping changelog check for tag: " + tag) 54 return true 55 } 56 } 57 return false 58 } 59 60 func main() { 61 repo := flag.String("repo", "", "the remote repo against which diff-index is to be derived") 62 branch := flag.String("branch", "master", "the branch against which diff-index is to be derived") 63 prID := flag.Int("pr", 0, "the ID of the PR") 64 flag.Parse() 65 66 if *prID > 0 && skipPR(*prID) { 67 return 68 } 69 70 if *repo != "" { 71 s := *repo + "/" + *branch 72 branch = &s 73 } 74 75 cmd := exec.Command("git", "diff-index", *branch, "--", ".") 76 out, err := cmd.Output() 77 if err != nil { 78 log.Fatal(err) 79 } 80 // Return successfully if there are no changes 81 if len(out) == 0 { 82 return 83 } 84 85 cmd = exec.Command("git", "diff-index", *branch, "--", "changelog/unreleased") 86 out, err = cmd.Output() 87 if err != nil { 88 log.Fatal(err) 89 } 90 91 mods := strings.Split(string(out), "\n") 92 for _, m := range mods { 93 params := strings.Split(m, " ") 94 // The fifth param in the output of diff-index is always the status followed by optional score number 95 if len(params) >= 5 && (params[4][0] == 'A' || params[4][0] == 'M') { 96 return 97 } 98 } 99 100 log.Fatal(errors.New("no changelog added. Please create a changelog item based on your changes")) 101 }