<commit_before>import Ember from 'ember';
import Cattle from 'ui/utils/cattle';

export default Ember.ObjectController.extend(Cattle.NewOrEditMixin, {
  error: null,
  credentials: null,
  editing: false,
  primaryResource: Ember.computed.alias('model.registry'),
  activeDriver: null,

  isCustom: Ember.computed.equal('activeDriver','custom'),

  actions: {
    selectDriver: function(name) {
      var driver = this.get('drivers').filterProperty('name',name)[0];
      this.set('activeDriver', driver.name);
      this.set('registry.serverAddress', driver.value);
    }
  },

  drivers: function() {
    var drivers = [
      {name: 'dockerhub', label: 'DockerHub',  css: 'dockerhub', value: 'index.docker.io', available: true  },
      {name: 'quay',      label: 'Quay.io',    css: 'quay',      value: 'quay.io',         available: true  },
      {name: 'custom',    label: 'Custom',     css: 'custom',    value: '',                available: true  },
    ];

    var active = this.get('activeDriver');
    drivers.forEach(function(driver) {
      driver.active = ( active === driver.name );
    });

    return drivers;
  }.property('activeDriver'),

  validate: function() {
    this._super();
    var errors = this.get('errors')||[];

    var cred = this.get('model.credential');
    cred.set('registryId', 'tbd');

    errors.pushObjects(cred.validationErrors());

    if ( errors.get('length') > 0 )
    {
      this.set('errors', errors.uniq());
      return false;
    }

    return true;
  },

  doSave: function() {
    var registry = this.get('model.registry');
    var existing = this.get('allRegistries').filterProperty('serverAddress', registry.get('serverAddress'))[0];
    if ( existing )
    {
      this.set('model.registry', existing);
      return Ember.RSVP.resolve();
    }
    else
    {
      return this._super();
    }
  },

  didSave: function() {
    var registry = this.get('model.registry');
    var cred = this.get('model.credential');
    var id = registry.get('id');

    cred.set('registryId', id);
    return cred.save();
  },

  doneSaving: function() {
    this.transitionToRoute('registries');
  },
});<commit_msg>Update the last existing credential on new registry that already exists<commit_after>import Ember from 'ember';
import Cattle from 'ui/utils/cattle';

export default Ember.ObjectController.extend(Cattle.NewOrEditMixin, {
  error: null,
  credentials: null,
  editing: false,
  primaryResource: Ember.computed.alias('model.registry'),
  activeDriver: null,

  isCustom: Ember.computed.equal('activeDriver','custom'),

  actions: {
    selectDriver: function(name) {
      var driver = this.get('drivers').filterProperty('name',name)[0];
      this.set('activeDriver', driver.name);
      this.set('registry.serverAddress', driver.value);
    }
  },

  drivers: function() {
    var drivers = [
      {name: 'dockerhub', label: 'DockerHub',  css: 'dockerhub', value: 'index.docker.io', available: true  },
      {name: 'quay',      label: 'Quay.io',    css: 'quay',      value: 'quay.io',         available: true  },
      {name: 'custom',    label: 'Custom',     css: 'custom',    value: '',                available: true  },
    ];

    var active = this.get('activeDriver');
    drivers.forEach(function(driver) {
      driver.active = ( active === driver.name );
    });

    return drivers;
  }.property('activeDriver'),

  validate: function() {
    this._super();
    var errors = this.get('errors')||[];

    var cred = this.get('model.credential');
    cred.set('registryId', 'tbd');

    errors.pushObjects(cred.validationErrors());

    if ( errors.get('length') > 0 )
    {
      this.set('errors', errors.uniq());
      return false;
    }

    return true;
  },

  doSave: function() {
    var registry = this.get('model.registry');
    var existing = this.get('allRegistries').filterProperty('serverAddress', registry.get('serverAddress'))[0];
    if ( existing )
    {
      this.set('model.registry', existing);
      return Ember.RSVP.resolve();
    }
    else
    {
      return this._super();
    }
  },

  didSave: function() {
    var registry = this.get('model.registry');
    var cred = this.get('model.credential');

    var existing = registry.get('credentials.lastObject');
    if ( existing )
    {
      existing.merge(cred);
      return existing.save()
    }
    else
    {
      cred.set('registryId', registry.get('id'));
      return cred.save();
    }
  },

  doneSaving: function() {
    this.transitionToRoute('registries');
  },
});
{'fraction_non_alphanumeric': 0.16732979664014147, 'fraction_numerical': 0.001326259946949602, 'mean_word_length': 3.007971656333038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6118886', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1391, 'n_words': 282}