github.com/SAP/jenkins-library@v1.362.0/cmd/ascAppUpload.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/SAP/jenkins-library/pkg/asc"
     5  	"github.com/SAP/jenkins-library/pkg/command"
     6  	piperHttp "github.com/SAP/jenkins-library/pkg/http"
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/piperutils"
     9  	"github.com/SAP/jenkins-library/pkg/telemetry"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type ascAppUploadUtils interface {
    14  	command.ExecRunner
    15  }
    16  
    17  type ascAppUploadUtilsBundle struct {
    18  	*command.Command
    19  	*piperutils.Files
    20  }
    21  
    22  func newAscAppUploadUtils() ascAppUploadUtils {
    23  	utils := ascAppUploadUtilsBundle{
    24  		Command: &command.Command{},
    25  		Files:   &piperutils.Files{},
    26  	}
    27  	// Reroute command output to logging framework
    28  	utils.Stdout(log.Writer())
    29  	utils.Stderr(log.Writer())
    30  	return &utils
    31  }
    32  
    33  func ascAppUpload(config ascAppUploadOptions, telemetryData *telemetry.CustomData) {
    34  	utils := newAscAppUploadUtils()
    35  	client := &piperHttp.Client{}
    36  
    37  	ascClient, err := asc.NewSystemInstance(client, config.ServerURL, config.AppToken)
    38  	if err != nil {
    39  		log.Entry().WithError(err).Fatalf("Failed to create ASC client talking to URL %v", config.ServerURL)
    40  	} else {
    41  		err = runAscAppUpload(&config, telemetryData, utils, ascClient)
    42  	}
    43  
    44  	if err != nil {
    45  		log.Entry().WithError(err).Fatal("step execution failed")
    46  	}
    47  }
    48  
    49  func runAscAppUpload(config *ascAppUploadOptions, telemetryData *telemetry.CustomData, utils ascAppUploadUtils, ascClient asc.System) error {
    50  
    51  	if len(config.JamfTargetSystem) == 0 {
    52  		return errors.Errorf("jamfTargetSystem must be set")
    53  	}
    54  
    55  	log.Entry().Infof("Collect data to create new release in ASC")
    56  
    57  	app, err := ascClient.GetAppById(config.AppID)
    58  	if err != nil {
    59  		log.SetErrorCategory(log.ErrorConfiguration)
    60  		return errors.Wrapf(err, "failed to get app information")
    61  	}
    62  
    63  	log.Entry().Debugf("Found App with name %v", app.AppName)
    64  
    65  	log.Entry().Infof("Create release for %v in ASC (AppID %v)", app.AppName, app.AppId)
    66  
    67  	releaseResponse, err := ascClient.CreateRelease(app.AppId, config.ReleaseAppVersion, config.ReleaseDescription, config.ReleaseDate, config.ReleaseVisible)
    68  
    69  	if err != nil {
    70  		log.SetErrorCategory(log.ErrorService)
    71  		return errors.Wrapf(err, "failed to create release")
    72  	}
    73  
    74  	if releaseResponse.Status != "success" {
    75  		log.SetErrorCategory(log.ErrorService)
    76  		return errors.New(releaseResponse.Message)
    77  	}
    78  
    79  	log.Entry().Infof("Collect data to upload app to ASC & Jamf")
    80  
    81  	jamfAppInformationResponse, err := ascClient.GetJamfAppInfo(app.BundleId, config.JamfTargetSystem)
    82  	if err != nil {
    83  		log.SetErrorCategory(log.ErrorService)
    84  		return errors.Wrapf(err, "failed to get jamf app info")
    85  	}
    86  
    87  	jamfAppId := jamfAppInformationResponse.MobileDeviceApplication.General.Id
    88  
    89  	if jamfAppId == 0 {
    90  		return errors.Errorf("failed to get jamf app id")
    91  	}
    92  
    93  	log.Entry().Debugf("Got Jamf info for app %v, jamfId: %v", app.AppName, jamfAppId)
    94  
    95  	log.Entry().Infof("Upload ipa %v to ASC & Jamf", config.FilePath)
    96  
    97  	err = ascClient.UploadIpa(config.FilePath, jamfAppId, config.JamfTargetSystem, app.BundleId, releaseResponse.Data)
    98  	if err != nil {
    99  		log.SetErrorCategory(log.ErrorService)
   100  		return errors.Wrapf(err, "failed to upload ipa")
   101  	}
   102  
   103  	log.Entry().Infof("Successfully uploaded %v to ASC (AppId %v) & Jamf (Id %v)", config.FilePath, app.AppId, jamfAppId)
   104  
   105  	return nil
   106  }