{"action":"created","comment":{"url":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments/76256768","id":76256768,"diff_hunk":"@@ -2,22 +2,67 @@ module Commands\n   module V2\n     class Unpublish < BaseCommand\n       def call\n+        validate\n+        State.supersede(previous_item) if previous_item\n+        transition_state\n+        delete_linkable\n+\n+        after_transaction_commit do\n+          send_downstream\n+        end\n+\n+        Success.new(content_id: content_id)\n+      end\n+\n+    private\n+\n+      def transition_state\n+        raise_invalid_type unless valid_type?\n+\n+        begin\n+          unpublish\n+        rescue ActiveRecord::RecordInvalid => e\n+          raise_command_error(422, e.message, fields: {})\n+        end\n+      end\n+\n+      def valid_type?\n+        %w(withdrawal redirect gone vanish).include?(type)\n+      end\n+\n+      def type\n+        payload.fetch(:type)\n+      end\n+\n+      def raise_invalid_type\n+        message = \"#{type} is not a valid unpublishing type\"\n+        raise_command_error(422, message, fields: {})\n+      end\n+\n+      def content_item\n+        @content_item ||= find_unpublishable_content_item\n+      end\n+\n+      def content_id\n+        @content_id ||= payload.fetch(:content_id)\n+      end\n+\n+      def validate_allow_discard_draft!","path":"app/commands/v2/unpublish.rb","position":null,"original_position":49,"commit_id":"884c3eac69e95d85341f5bd8850867572e0d654a","original_commit_id":"1130d79a6f3254440c637516bbc7bffe5494a3ca","user":{"login":"dougdroper","id":288481,"avatar_url":"https://avatars.githubusercontent.com/u/288481?v=3","gravatar_id":"","url":"https://api.github.com/users/dougdroper","html_url":"https://github.com/dougdroper","followers_url":"https://api.github.com/users/dougdroper/followers","following_url":"https://api.github.com/users/dougdroper/following{/other_user}","gists_url":"https://api.github.com/users/dougdroper/gists{/gist_id}","starred_url":"https://api.github.com/users/dougdroper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougdroper/subscriptions","organizations_url":"https://api.github.com/users/dougdroper/orgs","repos_url":"https://api.github.com/users/dougdroper/repos","events_url":"https://api.github.com/users/dougdroper/events{/privacy}","received_events_url":"https://api.github.com/users/dougdroper/received_events","type":"User","site_admin":false},"body":"I was using it to indicate that it sends that error, but it is not consistent, and perhaps there should be a version without the bang too, so dropping. ","created_at":"2016-08-25T14:43:56Z","updated_at":"2016-08-25T14:43:56Z","html_url":"https://github.com/alphagov/publishing-api/pull/484#discussion_r76256768","pull_request_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/484","_links":{"self":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments/76256768"},"html":{"href":"https://github.com/alphagov/publishing-api/pull/484#discussion_r76256768"},"pull_request":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/484"}}},"pull_request":{"url":"https://api.github.com/repos/alphagov/publishing-api/pulls/484","id":82698221,"html_url":"https://github.com/alphagov/publishing-api/pull/484","diff_url":"https://github.com/alphagov/publishing-api/pull/484.diff","patch_url":"https://github.com/alphagov/publishing-api/pull/484.patch","issue_url":"https://api.github.com/repos/alphagov/publishing-api/issues/484","number":484,"state":"open","locked":false,"title":"Capture Unpublishing record invalid errors","user":{"login":"dougdroper","id":288481,"avatar_url":"https://avatars.githubusercontent.com/u/288481?v=3","gravatar_id":"","url":"https://api.github.com/users/dougdroper","html_url":"https://github.com/dougdroper","followers_url":"https://api.github.com/users/dougdroper/followers","following_url":"https://api.github.com/users/dougdroper/following{/other_user}","gists_url":"https://api.github.com/users/dougdroper/gists{/gist_id}","starred_url":"https://api.github.com/users/dougdroper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougdroper/subscriptions","organizations_url":"https://api.github.com/users/dougdroper/orgs","repos_url":"https://api.github.com/users/dougdroper/repos","events_url":"https://api.github.com/users/dougdroper/events{/privacy}","received_events_url":"https://api.github.com/users/dougdroper/received_events","type":"User","site_admin":false},"body":"Previously was returning a 500, now returns 422 with the validation message \r\n\r\nhttps://trello.com/c/EXyXItHs/784-provide-necessary-validations-for-required-fields-small\r\n\r\nalso includes some refactoring work","created_at":"2016-08-25T10:54:10Z","updated_at":"2016-08-25T14:43:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"91284079f5493896a71eff25c898a0d68da81c5a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/484/commits","review_comments_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/484/comments","review_comment_url":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/issues/484/comments","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/884c3eac69e95d85341f5bd8850867572e0d654a","head":{"label":"alphagov:validate_unpublish","ref":"validate_unpublish","sha":"884c3eac69e95d85341f5bd8850867572e0d654a","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":27546098,"name":"publishing-api","full_name":"alphagov/publishing-api","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/publishing-api","description":"Central store for all content on GOV.UK.","fork":false,"url":"https://api.github.com/repos/alphagov/publishing-api","forks_url":"https://api.github.com/repos/alphagov/publishing-api/forks","keys_url":"https://api.github.com/repos/alphagov/publishing-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/publishing-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/publishing-api/teams","hooks_url":"https://api.github.com/repos/alphagov/publishing-api/hooks","issue_events_url":"https://api.github.com/repos/alphagov/publishing-api/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/publishing-api/events","assignees_url":"https://api.github.com/repos/alphagov/publishing-api/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/publishing-api/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/publishing-api/tags","blobs_url":"https://api.github.com/repos/alphagov/publishing-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/publishing-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/publishing-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/publishing-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/publishing-api/languages","stargazers_url":"https://api.github.com/repos/alphagov/publishing-api/stargazers","contributors_url":"https://api.github.com/repos/alphagov/publishing-api/contributors","subscribers_url":"https://api.github.com/repos/alphagov/publishing-api/subscribers","subscription_url":"https://api.github.com/repos/alphagov/publishing-api/subscription","commits_url":"https://api.github.com/repos/alphagov/publishing-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/publishing-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/publishing-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/publishing-api/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/publishing-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/publishing-api/merges","archive_url":"https://api.github.com/repos/alphagov/publishing-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/publishing-api/downloads","issues_url":"https://api.github.com/repos/alphagov/publishing-api/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/publishing-api/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/publishing-api/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/publishing-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/publishing-api/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/publishing-api/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/publishing-api/deployments","created_at":"2014-12-04T15:15:43Z","updated_at":"2016-05-25T22:58:45Z","pushed_at":"2016-08-25T14:38:51Z","git_url":"git://github.com/alphagov/publishing-api.git","ssh_url":"git@github.com:alphagov/publishing-api.git","clone_url":"https://github.com/alphagov/publishing-api.git","svn_url":"https://github.com/alphagov/publishing-api","homepage":"","size":2604,"stargazers_count":7,"watchers_count":7,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":5,"forks":5,"open_issues":5,"watchers":7,"default_branch":"master"}},"base":{"label":"alphagov:master","ref":"master","sha":"a218fb0a918ed836e1ee41c18b1b0f943d05dd46","user":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":27546098,"name":"publishing-api","full_name":"alphagov/publishing-api","owner":{"login":"alphagov","id":596977,"avatar_url":"https://avatars.githubusercontent.com/u/596977?v=3","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/publishing-api","description":"Central store for all content on GOV.UK.","fork":false,"url":"https://api.github.com/repos/alphagov/publishing-api","forks_url":"https://api.github.com/repos/alphagov/publishing-api/forks","keys_url":"https://api.github.com/repos/alphagov/publishing-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/publishing-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/publishing-api/teams","hooks_url":"https://api.github.com/repos/alphagov/publishing-api/hooks","issue_events_url":"https://api.github.com/repos/alphagov/publishing-api/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/publishing-api/events","assignees_url":"https://api.github.com/repos/alphagov/publishing-api/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/publishing-api/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/publishing-api/tags","blobs_url":"https://api.github.com/repos/alphagov/publishing-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/publishing-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/publishing-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/publishing-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/publishing-api/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/publishing-api/languages","stargazers_url":"https://api.github.com/repos/alphagov/publishing-api/stargazers","contributors_url":"https://api.github.com/repos/alphagov/publishing-api/contributors","subscribers_url":"https://api.github.com/repos/alphagov/publishing-api/subscribers","subscription_url":"https://api.github.com/repos/alphagov/publishing-api/subscription","commits_url":"https://api.github.com/repos/alphagov/publishing-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/publishing-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/publishing-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/publishing-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/publishing-api/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/publishing-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/publishing-api/merges","archive_url":"https://api.github.com/repos/alphagov/publishing-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/publishing-api/downloads","issues_url":"https://api.github.com/repos/alphagov/publishing-api/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/publishing-api/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/publishing-api/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/publishing-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/publishing-api/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/publishing-api/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/publishing-api/deployments","created_at":"2014-12-04T15:15:43Z","updated_at":"2016-05-25T22:58:45Z","pushed_at":"2016-08-25T14:38:51Z","git_url":"git://github.com/alphagov/publishing-api.git","ssh_url":"git@github.com:alphagov/publishing-api.git","clone_url":"https://github.com/alphagov/publishing-api.git","svn_url":"https://github.com/alphagov/publishing-api","homepage":"","size":2604,"stargazers_count":7,"watchers_count":7,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":5,"forks":5,"open_issues":5,"watchers":7,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/484"},"html":{"href":"https://github.com/alphagov/publishing-api/pull/484"},"issue":{"href":"https://api.github.com/repos/alphagov/publishing-api/issues/484"},"comments":{"href":"https://api.github.com/repos/alphagov/publishing-api/issues/484/comments"},"review_comments":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/484/comments"},"review_comment":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alphagov/publishing-api/pulls/484/commits"},"statuses":{"href":"https://api.github.com/repos/alphagov/publishing-api/statuses/884c3eac69e95d85341f5bd8850867572e0d654a"}}}}
{"actor":{"display_login":"dougdroper"}}