ping

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PingCmd = &cobra.Command{
	Use:   "ping",
	Short: "Ping PR reviewers to remind them",
	Long:  `Ping PR reviewers to remind them to review the Pull Request.`,
	Run: func(cmd *cobra.Command, args []string) {
		isDryRun := viper.GetBool("dry-run")

		repository := viper.GetString("repository")

		if repository == "" {
			detectedRepo, err := githubclient.GetCurrentRepository()
			if err != nil {
				log.Fatal().Msgf("Error detecting current repository: %v. Please specify a repository using the --repository flag.", err)
			}
			if detectedRepo == "" {
				log.Fatal().Msg("Could not detect current repository. Please specify a repository using the --repository flag.")
			}
			repository = detectedRepo
			log.Debug().Msgf("Using detected repository: %s", repository)
		}

		pr := viper.GetString("pr")
		if pr == "" {
			log.Fatal().Msg("PR number must be specified")
		}

		repoParts := strings.Split(repository, "/")
		if len(repoParts) != 2 {
			log.Fatal().Msgf("Invalid repository format. Expected owner/repo, got %s", repository)
		}
		repoOwner = repoParts[0]
		repoName = repoParts[1]

		ctx := context.WithValue(cmd.Context(), "dry-run", isDryRun)
		ctx = context.WithValue(ctx, "enabled", enabled)
		ctx = context.WithValue(ctx, "delay", delay)
		ctx = context.WithValue(ctx, "repoOwner", repoOwner)
		ctx = context.WithValue(ctx, "repoName", repoName)
		ctx = context.WithValue(ctx, "pr", pr)

		globalIntegrations := rules.ParseGlobalIntegrations()

		if len(globalIntegrations) == 0 {
			globalIntegrations = []ping.Integration{
				{
					Type:       "stdout",
					Parameters: make(map[string]string),
				},
			}
		}

		ctx = context.WithValue(ctx, "integrations", globalIntegrations)
		client := githubclient.NewClient(viper.GetString("github-token"))

		prState, err := githubclient.GetPullRequestState(client, repoOwner, repoName, pr)
		if err != nil {
			// Check if the error is because the PR was not found
			var githubErr *github.ErrorResponse
			if errors.As(err, &githubErr) && githubErr.Response.StatusCode == http.StatusNotFound {
				log.Info().Msgf("Pull Request #%s was not found in %s/%s. Please check if the PR number and repository are correct.", pr, repoOwner, repoName)
				return
			}
			log.Fatal().Msgf("Error retrieving pull request state: %v", err)
		}

		if prState.IsClosed || prState.IsMerged {
			statusMsg := "merged"
			if prState.IsClosed {
				statusMsg = "closed"
			}
			log.Info().Msgf("Pull Request #%s is %s. No need to ping reviewers.", pr, statusMsg)
			return
		}

		log.Debug().Msgf("Pull Request #%s is open. Proceeding with reviewer checks.", pr)

		reviewRequests, err := githubclient.GetReviewRequests(client, repoOwner, repoName, pr)
		if err != nil {
			// Check if the error is because the PR was not found
			var githubErr *github.ErrorResponse
			if errors.As(err, &githubErr) && githubErr.Response.StatusCode == http.StatusNotFound {
				log.Info().Msgf("Pull Request #%s was not found in %s/%s. Please check if the PR number and repository are correct.", pr, repoOwner, repoName)
				return
			}
			log.Fatal().Msgf("Error retrieving review requests: %v", err)
		}

		if len(reviewRequests) == 0 {
			log.Info().Msgf("No reviewers found for PR #%s.\n", pr)
			return
		}

		ruleset := rules.ParseRules()

		pingRequests := rules.ApplyRules(ctx, reviewRequests, ruleset)

		ctx = context.WithValue(ctx, "pingRequests", pingRequests)

		integrationGroups := make(map[string][]ping.PingRequest)

		for _, req := range pingRequests {
			if !req.ShouldPing {
				continue
			}

			for _, integration := range req.Integrations {
				integrationGroups[integration.Type] = append(integrationGroups[integration.Type], req)
			}
		}

		for integrationType, requests := range integrationGroups {
			integrationFunc, ok := integrations.Integrations[integrationType]
			if !ok {
				log.Printf("Warning: Unknown integration: %s, skipping associated reviewers", integrationType)
				continue
			}

			integrationCtx := context.WithValue(ctx, "pingRequests", requests)

			integrationFunc.Run(integrationCtx)
		}
	},
}

pingCmd represents the ping command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL