| 
 | 1 | +package main  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"encoding/json"  | 
 | 5 | +	"fmt"  | 
 | 6 | +	"io"  | 
 | 7 | +	"net/http"  | 
 | 8 | +	"os"  | 
 | 9 | +	"strconv"  | 
 | 10 | +	"strings"  | 
 | 11 | +)  | 
 | 12 | + | 
 | 13 | +//nolint:forbidigo  | 
 | 14 | +func main() {  | 
 | 15 | +	// Check if we are in a merge request context  | 
 | 16 | +	mrIID := os.Getenv("CI_MERGE_REQUEST_IID")  | 
 | 17 | +	if mrIID == "" {  | 
 | 18 | +		fmt.Println("Not a merge request. Exiting.")  | 
 | 19 | +		os.Exit(0)  | 
 | 20 | +	}  | 
 | 21 | + | 
 | 22 | +	// Get necessary environment variables  | 
 | 23 | +	gitlabAPIURL := os.Getenv("CI_API_V4_URL")  | 
 | 24 | +	projectID := os.Getenv("CI_PROJECT_ID")  | 
 | 25 | +	sourceProjectID := os.Getenv("CI_MERGE_REQUEST_SOURCE_PROJECT_ID")  | 
 | 26 | +	gitlabToken := os.Getenv("GITLAB_TOKEN")  | 
 | 27 | + | 
 | 28 | +	if gitlabAPIURL == "" || projectID == "" || sourceProjectID == "" {  | 
 | 29 | +		fmt.Println("Missing required GitLab CI/CD environment variables.")  | 
 | 30 | +		os.Exit(1)  | 
 | 31 | +	}  | 
 | 32 | + | 
 | 33 | +	if gitlabToken == "" {  | 
 | 34 | +		fmt.Print("GitLab token not found in environment variable.\n")  | 
 | 35 | +		os.Exit(1)  | 
 | 36 | +	}  | 
 | 37 | + | 
 | 38 | +	// 1. Get all old pipelines for this Merge Request  | 
 | 39 | +	pipelinesToCancel, err := getOldMergeRequestPipelines(gitlabAPIURL, projectID, mrIID, gitlabToken)  | 
 | 40 | +	if err != nil {  | 
 | 41 | +		fmt.Printf("Error getting merge request pipelines: %v\n", err)  | 
 | 42 | +		os.Exit(1)  | 
 | 43 | +	}  | 
 | 44 | + | 
 | 45 | +	if len(pipelinesToCancel) == 0 {  | 
 | 46 | +		fmt.Println("No old, running pipelines found for this merge request.")  | 
 | 47 | +		os.Exit(0)  | 
 | 48 | +	}  | 
 | 49 | + | 
 | 50 | +	fmt.Printf("Found %d old pipelines to cancel.\n", len(pipelinesToCancel))  | 
 | 51 | + | 
 | 52 | +	// 2. Cancel all found pipelines  | 
 | 53 | +	for _, p := range pipelinesToCancel {  | 
 | 54 | +		fmt.Printf("Canceling pipeline ID %d on project ID %d\n", p.ID, p.ProjectID)  | 
 | 55 | +		err = cancelPipeline(gitlabAPIURL, strconv.Itoa(p.ProjectID), p.ID, gitlabToken)  | 
 | 56 | +		if err != nil {  | 
 | 57 | +			// Log error but continue trying to cancel others  | 
 | 58 | +			fmt.Printf("Failed to cancel pipeline %d: %v\n", p.ID, err)  | 
 | 59 | +		} else {  | 
 | 60 | +			fmt.Printf("Successfully requested cancellation for pipeline %d\n", p.ID)  | 
 | 61 | +		}  | 
 | 62 | +	}  | 
 | 63 | +}  | 
 | 64 | + | 
 | 65 | +type pipelineInfo struct {  | 
 | 66 | +	ID        int    `json:"id"`  | 
 | 67 | +	ProjectID int    `json:"project_id"`  | 
 | 68 | +	Status    string `json:"status"`  | 
 | 69 | +}  | 
 | 70 | + | 
 | 71 | +func getOldMergeRequestPipelines(apiURL, projectID, mrIID, token string) ([]pipelineInfo, error) {  | 
 | 72 | +	// Get the current pipeline ID to avoid canceling ourselves  | 
 | 73 | +	currentPipelineIDStr := os.Getenv("CI_PIPELINE_ID")  | 
 | 74 | +	var currentPipelineID int  | 
 | 75 | +	if currentPipelineIDStr != "" {  | 
 | 76 | +		// a non-integer value will result in 0, which is fine since pipeline IDs are positive  | 
 | 77 | +		currentPipelineID, _ = strconv.Atoi(currentPipelineIDStr)  | 
 | 78 | +	}  | 
 | 79 | + | 
 | 80 | +	url := fmt.Sprintf("%s/projects/%s/merge_requests/%s/pipelines", apiURL, projectID, mrIID)  | 
 | 81 | +	req, err := http.NewRequest("GET", url, nil) //nolint:noctx,usestdlibvars  | 
 | 82 | +	if err != nil {  | 
 | 83 | +		return nil, err  | 
 | 84 | +	}  | 
 | 85 | +	req.Header.Set("PRIVATE-TOKEN", token) //nolint:canonicalheader  | 
 | 86 | + | 
 | 87 | +	client := &http.Client{}  | 
 | 88 | +	resp, err := client.Do(req)  | 
 | 89 | +	if err != nil {  | 
 | 90 | +		return nil, err  | 
 | 91 | +	}  | 
 | 92 | +	defer resp.Body.Close()  | 
 | 93 | + | 
 | 94 | +	if resp.StatusCode != http.StatusOK {  | 
 | 95 | +		body, _ := io.ReadAll(resp.Body)  | 
 | 96 | +		return nil, fmt.Errorf("failed to list merge request pipelines: status %d, body: %s", resp.StatusCode, string(body))  | 
 | 97 | +	}  | 
 | 98 | + | 
 | 99 | +	var pipelines []pipelineInfo  | 
 | 100 | +	if err := json.NewDecoder(resp.Body).Decode(&pipelines); err != nil {  | 
 | 101 | +		return nil, err  | 
 | 102 | +	}  | 
 | 103 | + | 
 | 104 | +	var pipelinesToCancel []pipelineInfo  | 
 | 105 | +	for _, p := range pipelines {  | 
 | 106 | +		// Cancel pipelines that are running or pending, and are not the current pipeline  | 
 | 107 | +		if (p.Status == "running" || p.Status == "pending") && p.ID != currentPipelineID {  | 
 | 108 | +			pipelinesToCancel = append(pipelinesToCancel, p)  | 
 | 109 | +		}  | 
 | 110 | +	}  | 
 | 111 | + | 
 | 112 | +	return pipelinesToCancel, nil  | 
 | 113 | +}  | 
 | 114 | + | 
 | 115 | +func cancelPipeline(apiURL, projectID string, pipelineID int, token string) error {  | 
 | 116 | +	url := fmt.Sprintf("%s/projects/%s/pipelines/%d/cancel", apiURL, projectID, pipelineID)  | 
 | 117 | +	req, err := http.NewRequest("POST", url, nil) //nolint:noctx,usestdlibvars  | 
 | 118 | +	if err != nil {  | 
 | 119 | +		return err  | 
 | 120 | +	}  | 
 | 121 | +	req.Header.Set("PRIVATE-TOKEN", token) //nolint:canonicalheader  | 
 | 122 | + | 
 | 123 | +	client := &http.Client{}  | 
 | 124 | +	resp, err := client.Do(req)  | 
 | 125 | +	if err != nil {  | 
 | 126 | +		return err  | 
 | 127 | +	}  | 
 | 128 | +	defer resp.Body.Close()  | 
 | 129 | + | 
 | 130 | +	if resp.StatusCode != http.StatusOK {  | 
 | 131 | +		body, _ := io.ReadAll(resp.Body)  | 
 | 132 | +		// It's possible the pipeline is already finished.  | 
 | 133 | +		if strings.Contains(string(body), "Cannot cancel a pipeline that is not pending or running") {  | 
 | 134 | +			fmt.Println("Pipeline already finished, nothing to do.") //nolint:forbidigo  | 
 | 135 | +			return nil  | 
 | 136 | +		}  | 
 | 137 | +		return fmt.Errorf("failed to cancel pipeline: status %d, body: %s", resp.StatusCode, string(body))  | 
 | 138 | +	}  | 
 | 139 | + | 
 | 140 | +	return nil  | 
 | 141 | +}  | 
0 commit comments