

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立通知規則
<a name="notification-rule-create"></a>

您可以使用通知規則來通知使用者重要的變更，例如：在儲存庫中建立提取請求時。通知規則會同時指定用於傳送通知的事件和 Amazon SNS 主題。如需詳細資訊，請參閱[什麼是通知？](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/welcome.html)



**注意**  
此功能不適用於歐洲 （米蘭） 區域。若要了解如何在該區域中可用的體驗中設定通知，請參閱[設定儲存庫通知](https://github.com/awsdocs/aws-codecommit-user-guide/blob/master/doc_source/how-to-repository-email-create.2.md)。

您可以使用 主控台或 AWS CLI 來建立 的通知規則 AWS CodeCommit。<a name="notification-rule-create-console"></a>

# 建立通知規則 (主控台)
<a name="notification-rule-create-console"></a>

1. 登入 AWS 管理主控台 並開啟 CodeCommit 主控台，網址為 https：//[https://console.aws.amazon.com/codecommit/](https://console.aws.amazon.com/codecommit/)。

1. 選擇 **Repositories (儲存庫)**，然後選擇您要新增通知規則的儲存庫。

1. 在儲存庫頁面中，選擇 **Notify (通知)**，然後選擇 **Create notification rule (建立通知規則)**。您也可以移至儲存庫的 **Settings (設定)** 頁面，然後選擇 **Create notification rule (建立通知規則)**。

1. 在 **Notification name (通知名稱)** 中，輸入規則的名稱。

1. 如果您只希望提供給 Amazon EventBridge 的資訊包含在通知中，請在 **Detail type (詳細資訊類型)** 中，選擇 **Basic (基本)**。如果您想要包含提供給 Amazon EventBridge 的資訊，以及 CodeCommit 或通知管理員可能提供的資訊，請選擇**完整**。

   如需詳細資訊，請參閱[了解通知內容與安全性](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/security.html#security-notifications)。

1.  在 **Events that trigger notifications (觸發通知的事件)** 中，選取您要傳送通知的事件。如需詳細資訊，請參閱[儲存庫上通知規則的事件](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/concepts.html#events-ref-repositories)。

1. 在 **Targets (目標)** 中，執行下列其中一個動作：
   + 如果您已將資源設定為搭配通知使用，請在**選擇目標類型****中選擇聊天應用程式 (Slack) 中的 Amazon Q Developer** 或 **SNS 主題**。在**選擇目標**中，選擇用戶端的名稱 （適用於聊天應用程式中在 Amazon Q Developer 中設定的 Slack 用戶端） 或 Amazon SNS 主題的 Amazon Resource Name (ARN) （適用於已設定通知所需政策的 Amazon SNS 主題）。
   + 如果您尚未設定要與通知搭配使用的資源，請選擇 **Create target (建立目標)**，然後選擇 **SNS topic (SNS 主題)**。在 **codestar-notifications-** 之後，提供主題名稱，然後選擇 **Create (建立)**。
**注意**  
如果您在建立通知規則的過程中建立 Amazon SNS 主題，將會為您套用允許通知功能將事件發佈至主題的政策。使用針對通知規則建立的主題，有助於確保您只訂閱需要接收此資源相關通知的使用者。
您無法在聊天應用程式用戶端中建立 Amazon Q Developer，作為建立通知規則的一部分。如果您在聊天應用程式 (Slack) 中選擇 Amazon Q Developer，您會看到一個按鈕，指示您在聊天應用程式中設定 Amazon Q Developer 中的用戶端。選擇該選項會在聊天應用程式主控台中開啟 Amazon Q Developer。如需詳細資訊，請參閱[聊天應用程式中設定通知與 Amazon Q Developer 之間的整合](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notifications-chatbot.html)。
如果您想要使用現有的 Amazon SNS 主題做為目標，除了該主題可能存在的任何其他政策之外，還必須新增AWS CodeStar通知所需的政策。如需詳細資訊，請參閱[為通知設定 Amazon SNS 主題](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/set-up-sns.html)和[了解通知內容與安全性](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/security.html#security-notifications)。

1. 若要完成建立規則，請選擇 **Submit (提交)**。

1. 您必須為使用者訂閱規則的 Amazon SNS 主題，才能接收通知。如需詳細資訊，請參閱[訂閱使用者成為目標的 Amazon SNS 主題](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/subscribe-users-sns.html)。您也可以在聊天應用程式中設定通知與 Amazon Q Developer 之間的整合，將通知傳送至 Amazon Chime 聊天室。如需詳細資訊，請參閱[聊天應用程式中設定通知與 Amazon Q Developer 之間的整合](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notifications-chatbot.html)。<a name="notification-rule-create-cli"></a>

# 建立通知規則 (AWS CLI)
<a name="notification-rule-create-cli"></a>

1. 在終端機或命令提示字元中，執行 **create-notification rule** 命令以產生 JSON 架構：

   ```
   aws codestar-notifications create-notification-rule --generate-cli-skeleton > rule.json
   ```

   您可以將檔案命名為任何您想要的名稱。在此範例中，檔案命名為 *rule.json*。

1. 在純文字編輯器中開啟 JSON 檔案，並編輯成包含您想要用於規則的資源、事件類型和目標。下列範例顯示 ID **MyNotificationRule** 為 *123456789012* AWS 之帳戶中名為 *MyDemoRepo* 之儲存庫的通知規則。建立分支和標籤時，具有完整詳細資訊類型的通知會傳送至名為 *MyNotificationTopic* 的 Amazon SNS 主題：

   ```
   {
       "Name": "MyNotificationRule",
       "EventTypeIds": [
           "codecommit-repository-branches-and-tags-created"
       ],
       "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo",
       "Targets": [
           {
               "TargetType": "SNS",
               "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic"
           }
       ],
       "Status": "ENABLED",
       "DetailType": "FULL"
   }
   ```

   儲存檔案。

1. 在終端機或命令列中，再次執行 **create-notification-rule** 命令，使用您剛編輯的檔案建立通知規則：

   ```
   aws codestar-notifications create-notification-rule --cli-input-json  file://rule.json
   ```

1. 如果成功，此命令會傳回通知規則的 ARN，如下所示：

   ```
   {
       "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
   }
   ```