<commit_before>'use strict';

/* global
    HookPayloads: false,
    KnownHooks: false,
    processHookPingEvent: false
*/

Meteor.methods({
  processPublishRequest: function(payload) {
    // console.log('Received publish request');
    // console.log('Payload:');
    // console.dir(payload);

    // Stores the received payload into the requests log...
    HookPayloads.insert({
      receivedAt: new Date(),
      payload: payload,
    });

    // Checks if this is a webhook ping event
    // See https://developer.github.com/webhooks/#ping-event
    if (payload.zen) {
      processHookPingEvent(payload);
    }

    // Makes sure it's a 'valid' github webhook payload...
    if (!payload.repository) {
      throw new Meteor.Error(400, "Invalid Payload!");
    }

    var
      repository = payload.repository,
      repoId = repository.id,
      repoName = repository.name,
      repoFullName = repository.full_name
    ;
    // console.log('Repository: ' + repoName);

    // Looks for a valid subscription
    var sub = Subscriptions.findOne({
      repo: repoName,
      repoId: repoId,
    });

    if (sub) {
      if (payload.hook) {
        // Test Payload
        /*
        var hookId = payload.hook_id;

        // Simply sets 'tested' field to true
        if (sub.hookId === hookId) {
          Subscriptions.update(sub._id, {
            $set: {
              tested: true
            }
          });
          // console.log("Subscription successfully tested!");
        } else {
          throw new Meteor.Error(
            409,
            "Invalid hook id! Try disabling/re-enabling autopublish..."
          );
        }
        */
      } else if (payload.action === 'published' && payload.release) {
        // console.log('  release action');
        // New Release!
        var
          release = payload.release,
          publishedAt = release.published_at,
          tagName = release.tag_name,
          releaseName = release.name,
          releaseBody = release.body,
          releaseTargetCommittish = release.target_commitish,
          repoCloneUrl = repository.clone_url
        ;

        // Adds the new publish request
        // documents are in the form:
        //
        // {
        //   createdAt: Date
        //   completedAt: Date
        //   packageName: String
        //   version: String
        //   arch: String
        //   status: String ('queueing, successful, errored')
        //  error: String
        // }
        AutoPublish.insert({
          createdAt: new Date(),
          publishedAt: publishedAt,
          pkgName: sub.pkgName,
          tagName: tagName,
          releaseName: releaseName,
          releaseTargetCommittish: releaseTargetCommittish,
          repoCloneUrl: repoCloneUrl,
          status: 'queueing',
        });

        // console.log('New Publish request created!');
      } else if (payload.ref_type === "tag") {
        /*
        "ref": "0.0.9",
        "ref_type": "tag",
        "master_branch": "master",
        */
        // New Tag!
        // console.log("Tag action");
        var
          now = new Date(),
          releaseName = payload.ref,
          releaseTargetCommittish = payload.master_branch,
          repoCloneUrl = payload.repository.clone_url,
          tagName = payload.ref
        ;

        // Adds the new publish request
        // documents are in the form:
        //
        // {
        //   createdAt: Date
        //   publishedAt: Date
        //   completedAt: Date
        //   pkgName: String
        //   tagName: String
        //   version: String
        //   arch: String
        //   status: String ('queueing, successful, errored')
        //   errors: [String]
        // }
        AutoPublish.insert({
          createdAt: now,
          publishedAt: now,
          pkgName: sub.pkgName,
          tagName: tagName,
          releaseTargetCommittish: releaseTargetCommittish,
          repoCloneUrl: repoCloneUrl,
          status: 'queueing',
        });

        // console.log('New Publish request created!');
      } else if (payload.head_commit) {
        // console.log('  push action');
        var
          repoCommit = payload.head_commit.id,
          repoName = payload.repository.name,
          repoUrl = payload.repository.git_url,
          meteorUser = process.env.METEOR_USER,
          meteorPwd = process.env.METEOR_PWD;

        // console.log('  commit: ' + repoCommit);
        // console.log('  name  : ' + repoName);
        // console.log('  url   : ' + repoUrl);

        //publishPackage(repoUrl, repoName, repoCommit, meteorUser, meteorPwd);
      }
    }
  },
  republish: function(actionId) {
    // Checks the user is an admin
    if (Roles.userIsInRole(this.userId, ['admin'])) {
      var publishAction = AutoPublish.findOne(actionId);
      if (publishAction) {
        AutoPublish.update(actionId, {
          $set: {
            status: "queueing"
          }
        });
      }
    }
  }<commit_msg>considering only approved hooks!<commit_after>'use strict';

/* global
    HookPayloads: false,
    KnownHooks: false,
    processHookPingEvent: false
*/

Meteor.methods({
  processPublishRequest: function(payload) {
    // console.log('Received publish request');
    // console.log('Payload:');
    // console.dir(payload);

    // Stores the received payload into the requests log...
    HookPayloads.insert({
      receivedAt: new Date(),
      payload: payload,
    });

    // Checks if this is a webhook ping event
    // See https://developer.github.com/webhooks/#ping-event
    if (payload.zen) {
      processHookPingEvent(payload);
    }

    // Makes sure it's a 'valid' github webhook payload...
    if (!payload.repository) {
      throw new Meteor.Error(400, "Invalid Payload!");
    }

    var
      repository = payload.repository,
      repoId = repository.id,
      repoName = repository.name,
      repoFullName = repository.full_name
    ;
    // console.log('Repository: ' + repoName);

    // Checks whether the hook is already known
    var hook = KnownHooks.findOne({
      'repoFullName': repoFullName
    });

    // Looks for a valid subscription
    var sub = Subscriptions.findOne({
      repo: repoName,
      repoId: repoId,
    });

    if (sub) {
      // TODO: check subscription for approval!

      if (payload.hook) {
        // Test Payload
        /*
        var hookId = payload.hook_id;

        // Simply sets 'tested' field to true
        if (sub.hookId === hookId) {
          Subscriptions.update(sub._id, {
            $set: {
              tested: true
            }
          });
          // console.log("Subscription successfully tested!");
        } else {
          throw new Meteor.Error(
            409,
            "Invalid hook id! Try disabling/re-enabling autopublish..."
          );
        }
        */
      } else if (payload.action === 'published' && payload.release) {
        // console.log('  release action');
        // New Release!
        var
          release = payload.release,
          publishedAt = release.published_at,
          tagName = release.tag_name,
          releaseName = release.name,
          releaseBody = release.body,
          releaseTargetCommittish = release.target_commitish,
          repoCloneUrl = repository.clone_url
        ;

        if (hook.approved) {
          // Adds the new publish request
          // documents are in the form:
          //
          // {
          //   createdAt: Date
          //   completedAt: Date
          //   packageName: String
          //   version: String
          //   arch: String
          //   status: String ('queueing, successful, errored')
          //  error: String
          // }
          AutoPublish.insert({
            createdAt: new Date(),
            publishedAt: publishedAt,
            pkgName: sub.pkgName,
            tagName: tagName,
            releaseName: releaseName,
            releaseTargetCommittish: releaseTargetCommittish,
            repoCloneUrl: repoCloneUrl,
            status: 'queueing',
          });
        }
        else {
          // TODO: signal unapproved hook by creating a new issue somewhere...
          console.log('Got unapproved hook!');

          AutoPublish.insert({
            createdAt: new Date(),
            publishedAt: publishedAt,
            pkgName: sub.pkgName,
            tagName: tagName,
            releaseName: releaseName,
            releaseTargetCommittish: releaseTargetCommittish,
            repoCloneUrl: repoCloneUrl,
            errors: [
              'Hook was not yet approved!'
            ],
            status: 'errored',
          });
        }
        // console.log('New Publish request created!');
      } else if (payload.ref_type === "tag") {
        /*
        "ref": "0.0.9",
        "ref_type": "tag",
        "master_branch": "master",
        */
        // New Tag!
        // console.log("Tag action");
        var
          now = new Date(),
          releaseName = payload.ref,
          releaseTargetCommittish = payload.master_branch,
          repoCloneUrl = payload.repository.clone_url,
          tagName = payload.ref
        ;

        if (hook.approved) {
          // Adds the new publish request
          // documents are in the form:
          //
          // {
          //   createdAt: Date
          //   publishedAt: Date
          //   completedAt: Date
          //   pkgName: String
          //   tagName: String
          //   version: String
          //   arch: String
          //   status: String ('queueing, successful, errored')
          //   errors: [String]
          // }
          AutoPublish.insert({
            createdAt: now,
            publishedAt: now,
            pkgName: sub.pkgName,
            tagName: tagName,
            releaseTargetCommittish: releaseTargetCommittish,
            repoCloneUrl: repoCloneUrl,
            status: 'queueing',
          });
        }
        else {
          // TODO: signal unapproved hook by creating a new issue somewhere...
          console.log('Got unapproved hook!');

          AutoPublish.insert({
            createdAt: new Date(),
            publishedAt: now,
            pkgName: sub.pkgName,
            tagName: tagName,
            releaseName: releaseName,
            releaseTargetCommittish: releaseTargetCommittish,
            repoCloneUrl: repoCloneUrl,
            errors: [
              'Hook was not yet approved!'
            ],
            status: 'errored',
          });
        }
        // console.log('New Publish request created!');
      } else if (payload.head_commit) {
        // console.log('  push action');
        var
          repoCommit = payload.head_commit.id,
          repoName = payload.repository.name,
          repoUrl = payload.repository.git_url,
          meteorUser = process.env.METEOR_USER,
          meteorPwd = <PASSWORD>.<PASSWORD>.METEOR_PWD;

        // console.log('  commit: ' + repoCommit);
        // console.log('  name  : ' + repoName);
        // console.log('  url   : ' + repoUrl);

        //publishPackage(repoUrl, repoName, repoCommit, meteorUser, meteorPwd);
      }
    }
  },
  republish: function(actionId) {
    // Checks the user is an admin
    if (Roles.userIsInRole(this.userId, ['admin'])) {
      var publishAction = AutoPublish.findOne(actionId);
      if (publishAction) {
        AutoPublish.update(actionId, {
          $set: {
            status: "queueing"
          }
        });
      }
    }
  }
{'fraction_non_alphanumeric': 0.11316657981587633, 'fraction_numerical': 0.001563314226159458, 'mean_word_length': 1.6028481012658229, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2728301', 'n_tokens_mistral': 3203, 'n_tokens_neox': 3137, 'n_words': 738}