2020-10-26 23:22:13 +03:00
|
|
|
// Copyright 2020 Security Scorecard Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-10-19 02:54:15 +03:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
2021-07-21 19:40:40 +03:00
|
|
|
"fmt"
|
2020-10-19 02:54:15 +03:00
|
|
|
"time"
|
|
|
|
|
2021-07-26 21:01:25 +03:00
|
|
|
"github.com/ossf/scorecard/v2/checker"
|
|
|
|
sce "github.com/ossf/scorecard/v2/errors"
|
2020-10-19 02:54:15 +03:00
|
|
|
)
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
const (
|
2021-08-01 01:31:34 +03:00
|
|
|
// CheckActive is the exported check name for Active.
|
2021-07-21 19:40:40 +03:00
|
|
|
CheckActive = "Active"
|
|
|
|
lookBackDays = 90
|
|
|
|
commitsPerWeek = 1
|
|
|
|
daysInOneWeek = 7
|
2021-04-10 15:26:56 +03:00
|
|
|
)
|
2020-10-19 02:54:15 +03:00
|
|
|
|
2021-05-22 20:19:52 +03:00
|
|
|
//nolint:gochecknoinits
|
2020-10-19 02:54:15 +03:00
|
|
|
func init() {
|
2021-05-28 00:54:34 +03:00
|
|
|
registerCheck(CheckActive, IsActive)
|
2020-10-19 02:54:15 +03:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// IsActive runs Active check.
|
2021-04-19 22:18:34 +03:00
|
|
|
func IsActive(c *checker.CheckRequest) checker.CheckResult {
|
2021-07-30 00:18:58 +03:00
|
|
|
archived, err := c.RepoClient.IsArchived()
|
|
|
|
if err != nil {
|
|
|
|
return checker.CreateRuntimeErrorResult(CheckActive, err)
|
|
|
|
}
|
|
|
|
if archived {
|
|
|
|
return checker.CreateMinScoreResult(CheckActive, "repo is marked as archived")
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := c.RepoClient.ListCommits()
|
2020-10-19 02:54:15 +03:00
|
|
|
if err != nil {
|
2021-07-21 19:40:40 +03:00
|
|
|
return checker.CreateRuntimeErrorResult(CheckActive, err)
|
2020-10-19 02:54:15 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:34 +03:00
|
|
|
tz, err := time.LoadLocation("UTC")
|
|
|
|
if err != nil {
|
2021-07-21 19:40:40 +03:00
|
|
|
e := sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("time.LoadLocation: %v", err))
|
|
|
|
return checker.CreateRuntimeErrorResult(CheckActive, e)
|
2021-04-19 22:18:34 +03:00
|
|
|
}
|
2021-07-21 19:40:40 +03:00
|
|
|
threshold := time.Now().In(tz).AddDate(0, 0, -1*lookBackDays)
|
2020-10-19 02:54:15 +03:00
|
|
|
totalCommits := 0
|
|
|
|
for _, commit := range commits {
|
2021-07-30 00:18:58 +03:00
|
|
|
if commit.CommittedDate.After(threshold) {
|
2020-10-19 02:54:15 +03:00
|
|
|
totalCommits++
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 19:40:40 +03:00
|
|
|
return checker.CreateProportionalScoreResult(CheckActive,
|
|
|
|
fmt.Sprintf("%d commit(s) found in the last %d days", totalCommits, lookBackDays),
|
|
|
|
totalCommits, commitsPerWeek*lookBackDays/daysInOneWeek)
|
2020-10-19 02:54:15 +03:00
|
|
|
}
|