Skip to content

Commit

Permalink
Merge pull request #32 from openconfig/support-forks
Browse files Browse the repository at this point in the history
Add support for PRs created from forks
  • Loading branch information
wenovus authored Jun 19, 2020
2 parents c1e39cc + b7bd773 commit f8c1b9b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
49 changes: 35 additions & 14 deletions cmd_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
// Commandline flags: should be string if it may not exist
modelRoot string // modelRoot is the root directory of the models.
repoSlug string // repoSlug is the "owner/repo" name of the models repo (e.g. openconfig/public).
prHeadRepoURL string // prHeadRepoURL is the URL of the HEAD repo for PRs (e.g. https://github.com/openconfig/public).
commitSHA string
branchName string // branchName is the name of the branch where the commit occurred.
prNumberStr string // prNumberStr is the PR number.
Expand All @@ -41,9 +42,11 @@ var (
skippedValidators string // e.g. "yanglint,pyang@head"

// Derived flags (for ease of use)
owner string
repo string
prNumber int
owner string
repo string
prNumber int
headOwner string
headRepo string

// local run flags
local bool // local run toggle
Expand All @@ -68,7 +71,8 @@ var (
func init() {
// GCB-required flags
flag.StringVar(&modelRoot, "modelRoot", "", "root directory to OpenConfig models")
flag.StringVar(&repoSlug, "repo-slug", "openconfig/public", "repo where CI is run")
flag.StringVar(&repoSlug, "repo-slug", "", "repo where CI is run")
flag.StringVar(&prHeadRepoURL, "pr-head-repo-url", "", "PR head repo URL")
flag.StringVar(&commitSHA, "commit-sha", "", "commit SHA of the PR")
flag.StringVar(&prNumberStr, "pr-number", "", "PR number")
flag.StringVar(&branchName, "branch", "", "branch name of commit")
Expand Down Expand Up @@ -266,6 +270,7 @@ func postInitialStatus(g *commonci.GithubRequestHandler, validatorId string, ver

if err := g.UpdatePRStatus(update); err != nil {
log.Printf("error: couldn't update PR: %s", err)
log.Printf("GithubPRUpdate: %+v", update)
return err
}
return nil
Expand Down Expand Up @@ -333,23 +338,35 @@ func main() {
}
}

if err := os.MkdirAll(commonci.ResultsDir, 0644); err != nil {
log.Fatalf("error while creating directory %q: %v", commonci.ResultsDir, err)
}
if err := os.MkdirAll(commonci.UserConfigDir, 0644); err != nil {
log.Fatalf("error while creating directory %q: %v", commonci.UserConfigDir, err)
}

repoSplit := strings.Split(repoSlug, "/")
owner = repoSplit[0]
repo = repoSplit[1]
if commitSHA == "" {
log.Fatalf("no commit SHA")
}

h, err := commonci.NewGitHubRequestHandler()
if err != nil {
log.Fatal(err)
}

if err := os.MkdirAll(commonci.ResultsDir, 0644); err != nil {
log.Fatalf("error while creating directory %q: %v", commonci.ResultsDir, err)
}
if err := os.MkdirAll(commonci.UserConfigDir, 0644); err != nil {
log.Fatalf("error while creating directory %q: %v", commonci.UserConfigDir, err)
headOwner = owner
headRepo = repo
if prHeadRepoURL != "" {
// Expected format: e.g. https://github.com/openconfig/public
URLSplit := strings.Split(prHeadRepoURL, "/")
headOwner = URLSplit[len(URLSplit)-2]
headRepo = URLSplit[len(URLSplit)-1]
if headOwner != owner || headRepo != repo {
remoteBranch := headOwner + "/" + headRepo
// If this is a fork, let later CI steps know the fork repo slug.
if err := ioutil.WriteFile(commonci.ForkSlugFile, []byte(remoteBranch), 0444); err != nil {
log.Fatalf("error while writing fork slug file %q: %v", commonci.ForkSlugFile, err)
}
log.Printf("fork detected for remote repo %q", remoteBranch)
}
}

compatReports = commonci.ValidatorAndVersionsDiff(compatReports, skippedValidators)
Expand All @@ -364,6 +381,10 @@ func main() {
_, skippedValidatorsMap := commonci.GetValidatorAndVersionsFromString(skippedValidators)

// Generate validation scripts, files, and post initial status on GitHub.
h, err := commonci.NewGitHubRequestHandler()
if err != nil {
log.Fatal(err)
}
for validatorId, validator := range commonci.Validators {
if validator.ReportOnly {
continue
Expand Down
3 changes: 3 additions & 0 deletions commonci/commonci.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
// CompatReportValidatorsFile notifies later CI steps of the validators
// that should be reported as a compatibility report.
CompatReportValidatorsFile = UserConfigDir + "/compat-report-validators.txt"
// ForkSlugFile is created by cmd_gen to store the fork slug, if
// present, for later CI steps.
ForkSlugFile = UserConfigDir + "/fork-slug.txt"
// ScriptFileName by convention is the script with the validator commands.
ScriptFileName = "script.sh"
// LatestVersionFileName by convention contains the version description
Expand Down
2 changes: 1 addition & 1 deletion validators/confd/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ unzip $ZIP_FILE -d $RESULTSDIR/confd-unzipped
find $RESULTSDIR/confd-unzipped -name 'confd-basic-*.linux.x86_64.installer.bin' -exec {} $RESULTSDIR/confd-install \;
CONFDC=$RESULTSDIR/confd-install/bin/confdc

CONFDPATH=`find $_MODEL_ROOT -type d | tr '\n' ':'`:$_MODEL_ROOT/../third_party/ietf
CONFDPATH=`find $_MODEL_ROOT -type d | tr '\n' ':'`:$ROOT_DIR/third_party/ietf

$CONFDC --version > $RESULTSDIR/latest-version.txt
if bash $RESULTSDIR/script.sh $CONFDC $CONFDPATH > $OUTFILE 2> $FAILFILE; then
Expand Down
12 changes: 8 additions & 4 deletions validators/misc-checks/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ find $_MODEL_ROOT -name '*.yang' > $RESULTSDIR/all-non-empty-files.txt 2>> $OUTF
# This requires the check logic within post_results to be changed.
# I'm delaying this because it remains to be seen whether goyang can be
# refactored so we don't need do this inter-process communication via files.
if bash $RESULTSDIR/script.sh > $OUTFILE 2>> $FAILFILE; then
if bash $RESULTSDIR/script.sh >> $OUTFILE 2>> $FAILFILE; then
# Delete fail file if it's empty and the script passed.
find $FAILFILE -size 0 -delete
fi
cat $RESULTSDIR/*.pr-file-parse-log > $RESULTSDIR/pr-file-parse-log 2>> $OUTFILE

# changed-files.txt
REPODIR=$RESULTSDIR/base_repo
git clone -b $BRANCH_NAME "[email protected]:$_REPO_SLUG.git" $REPODIR
git clone "[email protected]:$_REPO_SLUG.git" $REPODIR
cd $REPODIR
BASE_COMMIT=$(git merge-base $COMMIT_SHA origin/master)
PRBRANCH=gcb-ci-remote-repo-long-name-to-avoid-conflict
# fetching the PR directly from GitHub handles both normal PRs as well as forks.
git fetch origin pull/$_PR_NUMBER/head:$PRBRANCH
git checkout $PRBRANCH
BASE_COMMIT=$(git merge-base $PRBRANCH origin/master)
git diff --name-only $BASE_COMMIT | grep -E '.*\.yang$' > $RESULTSDIR/changed-files.txt 2>> $OUTFILE

# master-file-parse-log
git checkout $BASE_COMMIT &> $OUTFILE
git checkout $BASE_COMMIT &>> $OUTFILE
if find $REPODIR -name '*.yang' | xargs $GOPATH/bin/ocversion -p $REPODIR > $RESULTSDIR/master-file-parse-log 2>> $FAILFILE; then
# Delete fail file if it's empty and the script passed.
find $FAILFILE -size 0 -delete
Expand Down

0 comments on commit f8c1b9b

Please sign in to comment.