diff --git a/cmd_gen/main.go b/cmd_gen/main.go index 1c4e38a..81b0288 100644 --- a/cmd_gen/main.go +++ b/cmd_gen/main.go @@ -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. @@ -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 @@ -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") @@ -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 @@ -333,6 +338,13 @@ 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] @@ -340,16 +352,21 @@ func main() { 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) @@ -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 diff --git a/commonci/commonci.go b/commonci/commonci.go index dc12dfc..db5370f 100644 --- a/commonci/commonci.go +++ b/commonci/commonci.go @@ -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 diff --git a/validators/confd/test.sh b/validators/confd/test.sh index b4b1864..5e0e49d 100755 --- a/validators/confd/test.sh +++ b/validators/confd/test.sh @@ -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 diff --git a/validators/misc-checks/test.sh b/validators/misc-checks/test.sh index b2a9111..97046c9 100755 --- a/validators/misc-checks/test.sh +++ b/validators/misc-checks/test.sh @@ -24,7 +24,7 @@ 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 @@ -32,13 +32,17 @@ 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 "git@github.com:$_REPO_SLUG.git" $REPODIR +git clone "git@github.com:$_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