3232import org .springframework .util .ObjectUtils ;
3333import org .springframework .util .StringUtils ;
3434
35+ import io .choerodon .core .convertor .ApplicationContextHelper ;
3536import io .choerodon .core .exception .CommonException ;
3637import io .choerodon .devops .api .vo .GitConfigVO ;
3738import io .choerodon .devops .api .vo .GitEnvConfigVO ;
4041import io .choerodon .devops .app .service .DevopsEnvironmentService ;
4142import io .choerodon .devops .infra .dto .DevopsClusterDTO ;
4243import io .choerodon .devops .infra .dto .DevopsEnvironmentDTO ;
44+ import io .choerodon .devops .infra .dto .gitlab .DeployKeyDTO ;
4345import io .choerodon .devops .infra .dto .iam .ProjectDTO ;
4446import io .choerodon .devops .infra .dto .iam .Tenant ;
4547import io .choerodon .devops .infra .enums .EnvironmentType ;
4648import io .choerodon .devops .infra .feign .operator .BaseServiceClientOperator ;
49+ import io .choerodon .devops .infra .feign .operator .GitlabServiceClientOperator ;
4750import io .choerodon .devops .infra .mapper .DevopsClusterMapper ;
4851
4952/**
@@ -75,7 +78,8 @@ public class GitUtil {
7578 private String gitlabSshUrl ;
7679 @ Value ("${services.gitlab.internalsshUrl:}" )
7780 private String gitlabInternalsshUrl ;
78-
81+ @ Autowired
82+ private GitlabServiceClientOperator gitlabServiceClientOperator ;
7983
8084 public String getSshUrl () {
8185 if (org .apache .commons .lang3 .StringUtils .isNotBlank (gitlabInternalsshUrl )) {
@@ -702,7 +706,7 @@ public String cloneAppMarket(String name, String commit, String remoteUrl, Strin
702706 * @param sha 要打tag的散列值
703707 * @throws CommonException push error
704708 */
705- public static void createTagAndPush (Git git , String sshKey , String tagName , String sha ) {
709+ public static void createTagAndPush (DevopsEnvironmentDTO devopsEnvironmentDTO , Git git , String sshKey , String tagName , String sha ) {
706710 try {
707711 // 创建之前删除,保证本地不存在要创建的tag
708712 deleteTag (git , tagName );
@@ -715,7 +719,71 @@ public static void createTagAndPush(Git git, String sshKey, String tagName, Stri
715719 pushCommand .add (tagName );
716720 pushCommand .setRemote ("origin" );
717721 pushCommand .setForce (true );
718- pushCommand .setTransportConfigCallback (getTransportConfigCallback (sshKey )).call ();
722+ Iterable <PushResult > call = pushCommand .setTransportConfigCallback (getTransportConfigCallback (sshKey )).call ();
723+ Iterator <PushResult > iterator = call .iterator ();
724+ while (iterator .hasNext ()) {
725+ PushResult pushResult = iterator .next ();
726+ RemoteRefUpdate remoteUpdate = pushResult .getRemoteUpdate ("refs/tags/devops-sync" );
727+ if (!remoteUpdate .getStatus ().name ().equals ("OK" )) {
728+ // 尝试刷新公钥,然后再重新推一次tag
729+ Integer gitlabAdminUserId = GitUserNameUtil .getAdminId ();
730+ List <DeployKeyDTO > deployKeyDTOS = ApplicationContextHelper .getContext ().getBean (GitlabServiceClientOperator .class ).listDeployKey (devopsEnvironmentDTO .getGitlabEnvProjectId ().intValue (), gitlabAdminUserId );
731+ deployKeyDTOS .forEach (key -> {
732+ if (key .getTitle ().equals (devopsEnvironmentDTO .getCode ())) {
733+ ApplicationContextHelper .getContext ().getBean (GitlabServiceClientOperator .class ).deleteDeployKey (devopsEnvironmentDTO .getGitlabEnvProjectId ().intValue (), gitlabAdminUserId , key .getId ());
734+ }
735+ });
736+ // 以管理员身份创建deploy key
737+ ApplicationContextHelper .getContext ().getBean (GitlabServiceClientOperator .class ).createDeployKey (
738+ devopsEnvironmentDTO .getGitlabEnvProjectId ().intValue (),
739+ devopsEnvironmentDTO .getCode (),
740+ devopsEnvironmentDTO .getEnvIdRsaPub (),
741+ true ,
742+ GitUserNameUtil .getAdminId ()
743+ );
744+ // 尝试重新推送一次tag
745+ retryCreateTagAndPush (devopsEnvironmentDTO , git , sshKey , tagName , sha );
746+ break ;
747+ }
748+ }
749+ } catch (Exception e ) {
750+ throw new CommonException ("create tag fail" , e );
751+ }
752+ }
753+
754+
755+ /**
756+ * 重新尝试本地创建tag并推送远程仓库
757+ *
758+ * @param git git repo
759+ * @param sshKey ssh私钥
760+ * @param tagName tag名称
761+ * @param sha 要打tag的散列值
762+ * @throws CommonException push error
763+ */
764+ public static void retryCreateTagAndPush (DevopsEnvironmentDTO devopsEnvironmentDTO , Git git , String sshKey , String tagName , String sha ) {
765+ try {
766+ // 创建之前删除,保证本地不存在要创建的tag
767+ deleteTag (git , tagName );
768+ Repository repository = git .getRepository ();
769+ ObjectId id = repository .resolve (sha );
770+ RevWalk walk = new RevWalk (repository );
771+ RevCommit commit = walk .parseCommit (id );
772+ git .tag ().setObjectId (commit ).setName (tagName ).call ();
773+ PushCommand pushCommand = git .push ();
774+ pushCommand .add (tagName );
775+ pushCommand .setRemote ("origin" );
776+ pushCommand .setForce (true );
777+ Iterable <PushResult > call = pushCommand .setTransportConfigCallback (getTransportConfigCallback (sshKey )).call ();
778+ Iterator <PushResult > iterator = call .iterator ();
779+ while (iterator .hasNext ()) {
780+ PushResult pushResult = iterator .next ();
781+ RemoteRefUpdate remoteUpdate = pushResult .getRemoteUpdate ("refs/tags/devops-sync" );
782+ if (!remoteUpdate .getStatus ().name ().equals ("OK" )) {
783+ LOGGER .info ("failed to push devops-sync tag of env:{}, id :{} ,err code :{}" , devopsEnvironmentDTO .getCode (), devopsEnvironmentDTO .getId (), remoteUpdate .getStatus ().name ());
784+ throw new CommonException (remoteUpdate .getStatus ().name ());
785+ }
786+ }
719787 } catch (Exception e ) {
720788 throw new CommonException ("create tag fail" , e );
721789 }
@@ -771,9 +839,9 @@ public static void deleteTag(Git git, String tagName) {
771839 * @param tagName tag名称
772840 * @param sha 要打tag的commit的散列值
773841 */
774- public static void pushTag (Git git , String sshKey , String tagName , String sha ) {
842+ public static void pushTag (DevopsEnvironmentDTO devopsEnvironmentDTO , Git git , String sshKey , String tagName , String sha ) {
775843 deleteTag (git , tagName );
776- createTagAndPush (git , sshKey , tagName , sha );
844+ createTagAndPush (devopsEnvironmentDTO , git , sshKey , tagName , sha );
777845 }
778846
779847 /**
0 commit comments