|
| 1 | +// Copyright The Linux Foundation and each contributor to CommunityBridge. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package github |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/linuxfoundation/easycla/cla-backend-go/events" |
| 12 | + "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/models" |
| 13 | + log "github.com/linuxfoundation/easycla/cla-backend-go/logging" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +// propertyMatches returns true if value matches the pattern. |
| 18 | +// - "*" matches anything |
| 19 | +// - "re:..." matches regex (value must be non-empty) |
| 20 | +// - otherwise, exact match |
| 21 | +func propertyMatches(pattern, value string) bool { |
| 22 | + f := logrus.Fields{ |
| 23 | + "functionName": "github.propertyMatches", |
| 24 | + "pattern": pattern, |
| 25 | + "value": value, |
| 26 | + } |
| 27 | + if pattern == "*" { |
| 28 | + return true |
| 29 | + } |
| 30 | + if value == "" { |
| 31 | + return false |
| 32 | + } |
| 33 | + if strings.HasPrefix(pattern, "re:") { |
| 34 | + regex := pattern[3:] |
| 35 | + re, err := regexp.Compile(regex) |
| 36 | + if err != nil { |
| 37 | + log.WithFields(f).Debugf("Error in propertyMatches: bad regexp: %s, error: %v", regex, err) |
| 38 | + return false |
| 39 | + } |
| 40 | + return re.MatchString(value) |
| 41 | + } |
| 42 | + return value == pattern |
| 43 | +} |
| 44 | + |
| 45 | +// stripOrg removes the organization part from the repository name. |
| 46 | +// If input is "org/repo", returns "repo". If no "/", returns input unchanged. |
| 47 | +func stripOrg(repoFull string) string { |
| 48 | + idx := strings.Index(repoFull, "/") |
| 49 | + if idx >= 0 && idx+1 < len(repoFull) { |
| 50 | + return repoFull[idx+1:] |
| 51 | + } |
| 52 | + return repoFull |
| 53 | +} |
| 54 | + |
| 55 | +// isActorSkipped returns true if the given actor should be skipped according to the skip_cla config pattern. |
| 56 | +// config format: "<username_pattern>;<email_pattern>" |
| 57 | +// Actor.CommitAuthor.Login and Actor.CommitAuthor.Email should be *string, can be nil. |
| 58 | +func isActorSkipped(actor *UserCommitSummary, config string) bool { |
| 59 | + f := logrus.Fields{ |
| 60 | + "functionName": "github.isActorSkipped", |
| 61 | + "config": config, |
| 62 | + } |
| 63 | + // Defensive: must have exactly one ';' |
| 64 | + if !strings.Contains(config, ";") { |
| 65 | + log.WithFields(f).Debugf("Invalid skip_cla config format: %s, expected '<username_pattern>;<email_pattern>'", config) |
| 66 | + return false |
| 67 | + } |
| 68 | + parts := strings.SplitN(config, ";", 2) |
| 69 | + if len(parts) != 2 { |
| 70 | + return false |
| 71 | + } |
| 72 | + usernamePattern := parts[0] |
| 73 | + emailPattern := parts[1] |
| 74 | + var ( |
| 75 | + username string |
| 76 | + email string |
| 77 | + ) |
| 78 | + if actor != nil && actor.CommitAuthor != nil && actor.CommitAuthor.Login != nil { |
| 79 | + username = *actor.CommitAuthor.Login |
| 80 | + } |
| 81 | + if actor != nil && actor.CommitAuthor != nil && actor.CommitAuthor.Email != nil { |
| 82 | + email = *actor.CommitAuthor.Email |
| 83 | + } |
| 84 | + |
| 85 | + return propertyMatches(usernamePattern, username) && propertyMatches(emailPattern, email) |
| 86 | +} |
| 87 | + |
| 88 | +// SkipWhitelistedBots- check if the actors are whitelisted based on the skip_cla configuration. |
| 89 | +// Returns two lists: |
| 90 | +// - actors still missing cla: actors who still need to sign the CLA after checking skip_cla |
| 91 | +// - whitelisted actors: actors who are skipped due to skip_cla configuration |
| 92 | +// :param orgModel: The GitHub organization model instance. |
| 93 | +// :param orgRepo: The repository name in the format 'org/repo'. |
| 94 | +// :param actorsMissingCla: List of UserCommitSummary objects representing actors who are missing CLA. |
| 95 | +// :return: two arrays (actors still missing CLA, whitelisted actors) |
| 96 | +// : in cla-{stage}-github-orgs table there can be a skip_cla field which is a dict with the following structure: |
| 97 | +// |
| 98 | +// { |
| 99 | +// "repo-name": "<username_pattern>;<email_pattern>", |
| 100 | +// "re:repo-regexp": "<username_pattern>;<email_pattern>", |
| 101 | +// "*": "<username_pattern>;<email_pattern>" |
| 102 | +// } |
| 103 | +// |
| 104 | +// where: |
| 105 | +// - repo-name is the exact repository name under given org (e.g., "my-repo" not "my-org/my-repo") |
| 106 | +// - re:repo-regexp is a regex pattern to match repository names |
| 107 | +// - * is a wildcard that applies to all repositories |
| 108 | +// - <username_pattern> is a GitHub username pattern (exact match or regex prefixed by re: or match all '*') |
| 109 | +// - <email_pattern> is a GitHub email pattern (exact match or regex prefixed by re: or match all '*') |
| 110 | +// The username and email patterns are separated by a semicolon (;). |
| 111 | +// If the skip_cla is not set, it will skip the whitelisted bots check. |
| 112 | +func SkipWhitelistedBots(ev events.Service, orgModel *models.GithubOrganization, orgRepo, projectID string, actorsMissingCLA []*UserCommitSummary) ([]*UserCommitSummary, []*UserCommitSummary) { |
| 113 | + repo := stripOrg(orgRepo) |
| 114 | + f := logrus.Fields{ |
| 115 | + "functionName": "github.SkipWhitelistedBots", |
| 116 | + "orgRepo": orgRepo, |
| 117 | + "repo": repo, |
| 118 | + "projectID": projectID, |
| 119 | + } |
| 120 | + outActorsMissingCLA := []*UserCommitSummary{} |
| 121 | + whitelistedActors := []*UserCommitSummary{} |
| 122 | + |
| 123 | + skipCLA := orgModel.SkipCla |
| 124 | + if skipCLA == nil { |
| 125 | + log.WithFields(f).Debug("skip_cla is not set, skipping whitelisted bots check") |
| 126 | + return actorsMissingCLA, []*UserCommitSummary{} |
| 127 | + } |
| 128 | + |
| 129 | + var config string |
| 130 | + |
| 131 | + // 1. Exact match |
| 132 | + if val, ok := skipCLA[repo]; ok { |
| 133 | + config = val |
| 134 | + log.WithFields(f).Debugf("skip_cla config found for repo (exact hit): '%s'", config) |
| 135 | + } |
| 136 | + |
| 137 | + // 2. Regex match (if no exact hit) |
| 138 | + if config == "" { |
| 139 | + log.WithFields(f).Debug("No skip_cla config found for repo, checking regex patterns") |
| 140 | + for k, v := range skipCLA { |
| 141 | + if !strings.HasPrefix(k, "re:") { |
| 142 | + continue |
| 143 | + } |
| 144 | + pattern := k[3:] |
| 145 | + re, err := regexp.Compile(pattern) |
| 146 | + if err != nil { |
| 147 | + log.WithFields(f).Warnf("Invalid regex in skip_cla: '%s': %+v", pattern, err) |
| 148 | + continue |
| 149 | + } |
| 150 | + if re.MatchString(repo) { |
| 151 | + config = v |
| 152 | + log.WithFields(f).Debugf("Found skip_cla config for repo via regex pattern: '%s'", config) |
| 153 | + break |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // 3. Wildcard fallback |
| 159 | + if config == "" { |
| 160 | + if val, ok := skipCLA["*"]; ok { |
| 161 | + config = val |
| 162 | + log.WithFields(f).Debugf("No skip_cla config found for repo, using wildcard config: '%s'", config) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // 4. No match |
| 167 | + if config == "" { |
| 168 | + log.WithFields(f).Debug("No skip_cla config found for repo, skipping whitelisted bots check") |
| 169 | + return actorsMissingCLA, []*UserCommitSummary{} |
| 170 | + } |
| 171 | + const nullStr = "(null)" |
| 172 | + |
| 173 | + for _, actor := range actorsMissingCLA { |
| 174 | + if isActorSkipped(actor, config) { |
| 175 | + if actor == nil { |
| 176 | + continue |
| 177 | + } |
| 178 | + id, login, username, email := nullStr, nullStr, nullStr, nullStr |
| 179 | + if actor.CommitAuthor != nil && actor.CommitAuthor.ID != nil { |
| 180 | + id = fmt.Sprintf("%v", *actor.CommitAuthor.ID) |
| 181 | + } |
| 182 | + if actor.CommitAuthor != nil && actor.CommitAuthor.Login != nil { |
| 183 | + login = *actor.CommitAuthor.Login |
| 184 | + } |
| 185 | + if actor.CommitAuthor != nil && actor.CommitAuthor.Name != nil { |
| 186 | + username = *actor.CommitAuthor.Name |
| 187 | + } |
| 188 | + if actor.CommitAuthor != nil && actor.CommitAuthor.Email != nil { |
| 189 | + email = *actor.CommitAuthor.Email |
| 190 | + } |
| 191 | + actorData := fmt.Sprintf("id='%v',login='%v',username='%v',email='%v'", id, login, username, email) |
| 192 | + msg := fmt.Sprintf( |
| 193 | + "Skipping CLA check for repo='%s', actor: %s due to skip_cla config: '%s'", |
| 194 | + orgRepo, actorData, config, |
| 195 | + ) |
| 196 | + log.WithFields(f).Info(msg) |
| 197 | + eventData := events.BypassCLAEventData{ |
| 198 | + Repo: orgRepo, |
| 199 | + Config: config, |
| 200 | + Actor: actorData, |
| 201 | + } |
| 202 | + ev.LogEvent(&events.LogEventArgs{ |
| 203 | + EventType: events.BypassCLA, |
| 204 | + EventData: &eventData, |
| 205 | + UserID: id, |
| 206 | + UserName: login, |
| 207 | + ProjectID: projectID, |
| 208 | + }) |
| 209 | + log.WithFields(f).Debugf("event logged") |
| 210 | + actor.Authorized = true |
| 211 | + whitelistedActors = append(whitelistedActors, actor) |
| 212 | + } else { |
| 213 | + outActorsMissingCLA = append(outActorsMissingCLA, actor) |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return outActorsMissingCLA, whitelistedActors |
| 218 | +} |
0 commit comments