diff --git a/server/events/vcs/instrumented_client.go b/server/events/vcs/instrumented_client.go index d5d5809d9c..36aaf9b605 100644 --- a/server/events/vcs/instrumented_client.go +++ b/server/events/vcs/instrumented_client.go @@ -206,6 +206,12 @@ func (c *InstrumentedClient) PullIsMergeable(logger logging.SimpleLogging, repo } func (c *InstrumentedClient) UpdateStatus(logger logging.SimpleLogging, repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string, url string) error { + // If the plan isn't coming from a pull request, + // don't attempt to update the status. + if pull.Num == 0 { + return nil + } + scope := c.StatsScope.SubScope("update_status") scope = SetGitScopeTags(scope, repo.FullName, pull.Num) diff --git a/server/events/working_dir.go b/server/events/working_dir.go index f77378a2a4..3d2d9fa2b9 100644 --- a/server/events/working_dir.go +++ b/server/events/working_dir.go @@ -79,9 +79,9 @@ type FileWorkspace struct { // TestingOverrideBaseCloneURL can be used during testing to override the // URL of the base repo to be cloned. If it's empty then we clone normally. TestingOverrideBaseCloneURL string - // GithubAppEnabled is true if we should fetch the ref "pull/PR_NUMBER/head" - // from the "origin" remote. If this is false, we fetch "+refs/heads/$HEAD_BRANCH" - // from the "head" remote. + // GithubAppEnabled is true and a PR number is supplied, we should fetch + // the ref "pull/PR_NUMBER/head" from the "origin" remote. If this is false, + // we fetch "+refs/heads/$HEAD_BRANCH" from the "head" remote. GithubAppEnabled bool // use the global setting without overriding GpgNoSigningEnabled bool @@ -106,12 +106,13 @@ func (w *FileWorkspace) Clone(logger logging.SimpleLogging, headRepo models.Repo logger.Debug("clone directory '%s' already exists, checking if it's at the right commit", cloneDir) // We use git rev-parse to see if our repo is at the right commit. - // If just checking out the pull request branch, we can use HEAD. + // If just checking out the pull request branch or if there is no + // pull request (API triggered with a custom git ref), we can use HEAD. // If doing a merge, then HEAD won't be at the pull request's HEAD // because we'll already have performed a merge. Instead, we'll check // HEAD^2 since that will be the commit before our merge. pullHead := "HEAD" - if w.CheckoutMerge { + if w.CheckoutMerge && c.pr.Num > 0 { pullHead = "HEAD^2" } revParseCmd := exec.Command("git", "rev-parse", pullHead) // #nosec @@ -331,7 +332,7 @@ func (w *FileWorkspace) wrappedGit(logger logging.SimpleLogging, c wrappedGitCon func (w *FileWorkspace) mergeToBaseBranch(logger logging.SimpleLogging, c wrappedGitContext) error { fetchRef := fmt.Sprintf("+refs/heads/%s:", c.pr.HeadBranch) fetchRemote := "head" - if w.GithubAppEnabled { + if w.GithubAppEnabled && c.pr.Num > 0 { fetchRef = fmt.Sprintf("pull/%d/head:", c.pr.Num) fetchRemote = "origin" }